<?php
session_start();
require_once 'db.php';

function calculate_total($cart) {
    $total = 0;
    foreach ($cart as $item) {
        $total += $item['price'] * $item['qty'];
    }
    return number_format($total, 2);
}

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

if (empty($cart)) {
    echo "<p>No items in cart.</p>";
    return;
}

foreach ($cart as $item) {
    echo '<div class="d-flex justify-content-between border-bottom py-2">
            <div>
                <strong>' . htmlspecialchars($item['name']) . '</strong><br>
                ' . $item['qty'] . ' x ' . number_format($item['price'], 2) . '
            </div>
            <button class="btn btn-sm btn-danger remove-item" data-id="' . $item['id'] . '">&times;</button>
          </div>';
}

echo '<div class="text-end mt-3">
        <h5>Total: ' . calculate_total($cart) . '</h5>
      </div>
      <div class="d-grid gap-2 mt-3">
        <button id="clear-cart" class="btn btn-warning">Void</button>
        <button id="pay-now" class="btn btn-success">Payment</button>
      </div>';
?>
