<?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.');
}

/*
    Tables
*/
$stmt = $db->prepare("
    SELECT *
    FROM tables
    ORDER BY table_name ASC
");
$stmt->execute();

$tables = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
    Totals
*/
$subtotal = 0;
$totalGst = 0;
$grandTotal = 0;

foreach ($cart as $item) {

    $qty = (float)$item['qty'];
    $price = (float)$item['price'];
    $gstPercent = (float)$item['gst'];

    $lineSubtotal = $qty * $price;
    $lineGst = ($lineSubtotal * $gstPercent) / 100;
    $lineTotal = $lineSubtotal + $lineGst;

    $subtotal += $lineSubtotal;
    $totalGst += $lineGst;
    $grandTotal += $lineTotal;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Checkout</title>

    <link
        href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
        rel="stylesheet"
    >
</head>
<body class="bg-light">

<div class="container mt-4">

    <div class="card shadow-sm">
        <div class="card-header bg-success text-white">
            Checkout
        </div>

        <div class="card-body">

            <form action="save_order.php" method="POST">

                <div class="mb-3">
                    <label class="form-label">Customer Name (optional)</label>

                    <input
                        type="text"
                        name="customer_name"
                        class="form-control"
                    >
                </div>

                <div class="mb-3">
                    <label class="form-label">Select Table</label>

                    <select
                        name="table_id"
                        class="form-select"
                        required
                    >
                        <option value="">Select Table</option>
                        <?php foreach ($tables as $table): ?>
                            <option value="<?= $table['id'] ?>">
                                <?= htmlspecialchars($table['table_name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="mb-3">
                    <label class="form-label">Payment Mode</label>

                    <select
                        name="payment_mode"
                        class="form-select"
                        required
                    >
                        <option value="CASH">Cash</option>
                        <option value="CARD">Card</option>
                        <option value="UPI">UPI</option>
                    </select>
                </div>

                <table class="table table-bordered">

                    <tr>
                        <th>Subtotal</th>
                        <td class="text-end">
                            ₹ <?= number_format($subtotal, 2) ?>
                        </td>
                    </tr>

                    <tr>
                        <th>GST</th>
                        <td class="text-end">
                            ₹ <?= number_format($totalGst, 2) ?>
                        </td>
                    </tr>

                    <tr class="table-success">
                        <th>Grand Total</th>
                        <td class="text-end">
                            ₹ <?= number_format($grandTotal, 2) ?>
                        </td>
                    </tr>

                </table>

                <button
                    type="submit"
                    class="btn btn-success w-100 btn-lg"
                >
                    Confirm Payment
                </button>

            </form>

        </div>
    </div>

</div>

<script>
document.addEventListener('keydown', function(e) {

    if (e.key === 'Enter') {
        let form = document.querySelector('form');

        if (form) {
            form.submit();
        }
    }

});
</script>

</body>
</html>