<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

$cart = $_SESSION['pos_cart'] ?? [];

if (empty($cart)) {
    die('Cart is empty.');
}

/*
    Settings
*/
$stmt = $db->prepare("
    SELECT *
    FROM settings
    LIMIT 1
");
$stmt->execute();

$settings = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];

$paperWidth = (int)($settings['paper_width'] ?? 80);

if ($paperWidth <= 0) {
    $paperWidth = 80;
}

/*
    Optional table
*/
$tableName = 'Walk-in';

if (!empty($_GET['table_id'])) {

    $stmt = $db->prepare("
        SELECT table_name
        FROM tables
        WHERE id = ?
        LIMIT 1
    ");
    $stmt->execute([(int)$_GET['table_id']]);

    $table = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($table) {
        $tableName = $table['table_name'];
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>KOT Print</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            width: <?= $paperWidth ?>mm;
            margin: 0 auto;
            padding: 10px;
            font-size: 13px;
        }

        .center {
            text-align: center;
        }

        .divider {
            border-top: 1px dashed #000;
            margin: 8px 0;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 5px 0;
        }

        .right {
            text-align: right;
        }

        .title {
            font-size: 18px;
            font-weight: bold;
        }

        @media print {
            body {
                margin: 0;
                padding: 0;
            }

            .no-print {
                display: none;
            }
        }
    </style>
</head>
<body>

<div class="center">
    <div class="title">KITCHEN ORDER TICKET</div>

    <div>
        <?= htmlspecialchars($settings['business_name'] ?? 'Restaurant') ?>
    </div>
</div>

<div class="divider"></div>

<table>
    <tr>
        <td><strong>Table:</strong></td>
        <td class="right">
            <?= htmlspecialchars($tableName) ?>
        </td>
    </tr>

    <tr>
        <td><strong>Time:</strong></td>
        <td class="right">
            <?= date('d-m-Y h:i A') ?>
        </td>
    </tr>
</table>

<div class="divider"></div>

<table>

    <thead>
        <tr>
            <th>Item</th>
            <th class="right">Qty</th>
        </tr>
    </thead>

    <tbody>

    <?php foreach ($cart as $item): ?>
        <tr>
            <td><?= htmlspecialchars($item['name']) ?></td>
            <td class="right">
                <?= number_format($item['qty'], 2) ?>
            </td>
        </tr>
    <?php endforeach; ?>

    </tbody>

</table>

<div class="divider"></div>

<div class="center">
    SEND TO KITCHEN
</div>

<div class="no-print center" style="margin-top:15px;">
    <button onclick="window.print()">Print KOT</button>
</div>

<script>
window.onload = function () {
    window.print();
};
</script>

</body>
</html>