<?php
session_start();

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

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

if (empty($cart)) {
    ?>
    <div class="text-center text-muted py-5">
        No items in cart
    </div>
    <?php
    exit;
}

$subtotal = 0;
$totalGst = 0;
$grandTotal = 0;
?>

<div style="max-height:500px; overflow-y:auto;">

    <table class="table table-bordered table-hover align-middle">

        <thead class="table-dark">
            <tr>
                <th>Item</th>
                <th width="70">Qty</th>
                <th width="90">Rate</th>
                <th width="80">GST</th>
                <th width="100">Total</th>
                <th width="60"></th>
            </tr>
        </thead>

        <tbody>

        <?php foreach ($cart as $item): ?>

            <?php
            $qty = (float)$item['qty'];
            $rate = (float)$item['price'];
            $gstPercent = (float)$item['gst'];

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

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

            <tr data-item-id="<?= $item['item_id'] ?>">

                <td>
                    <?= htmlspecialchars($item['name']) ?>
                </td>

                <td>

                    <div class="d-flex align-items-center gap-1">

                        <button
                            class="btn btn-sm btn-outline-secondary"
                            onclick="updateCartQty(<?= $item['item_id'] ?>, -1)"
                        >
                            -
                        </button>

                        <span>
                            <?= number_format($qty, 2) ?>
                        </span>

                        <button
                            class="btn btn-sm btn-outline-secondary"
                            onclick="updateCartQty(<?= $item['item_id'] ?>, 1)"
                        >
                            +
                        </button>

                    </div>

                </td>

                <td>
                    ₹ <?= number_format($rate, 2) ?>
                </td>

                <td>
                    <?= number_format($gstPercent, 2) ?>%
                </td>

                <td>
                    ₹ <?= number_format($lineTotal, 2) ?>
                </td>

                <td>
                    <button
                        class="btn btn-sm btn-danger"
                        onclick="removeCartItem(<?= $item['item_id'] ?>)"
                    >
                        X
                    </button>
                </td>

            </tr>

        <?php endforeach; ?>

        </tbody>

    </table>

</div>

<hr>

<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 style="font-size:18px;">Grand Total</th>
        <td class="text-end" style="font-size:18px;">
            ₹ <?= number_format($grandTotal, 2) ?>
        </td>
    </tr>

</table>