<?php
// sales/checkout.php
session_start();
$pageTitle = 'Checkout';
require_once('./config/db.php');
require_once("./includes/auth.php");
include('./includes/header.php');

$cart = $_SESSION['cart'] ?? [];
if (empty($cart)) {
    echo '<div class="alert alert-warning">Cart is empty. <a href="sale.php">Add items</a></div>';
    include('./includes/footer.php');
    exit;
}

$total = 0;
foreach ($cart as $c) $total += (float)$c['Total'];
?>
<div class="d-flex justify-content-between mb-3">
    <h3><i class="bi bi-credit-card"></i> Checkout</h3>
    <a href="cart.php" class="btn btn-outline-secondary">Back to cart</a>
</div>

<div class="card shadow-sm">
    <div class="card-body">
        <h5>Order Summary</h5>

        <div class="table-responsive mb-3">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Price</th>
                        <th>Qty</th>
                        <th>Total</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($cart as $c): ?>
                        <tr>
                            <td><?= htmlspecialchars($c['Name']) ?></td>
                            <td><?= number_format($c['Price'], 2) ?></td>
                            <td><?= (int)$c['Qty'] ?></td>
                            <td><?= number_format($c['Total'], 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div class="d-flex justify-content-end mb-3">
            <div class="text-end">
                <div class="fs-5 fw-bold">Total: <?= number_format($total, 2) ?></div>
            </div>
        </div>

        <!-- Trigger Modal Instead of Direct Submit -->
        <button class="btn btn-success btn-lg w-100" data-bs-toggle="modal" data-bs-target="#customerModal">
            Confirm Sale & Print Receipt
        </button>

        <!-- Customer Info Modal -->
        <div class="modal fade" id="customerModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header">
                        <h5 class="modal-title">Customer Information</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body">
                        <form id="customerForm" method="post" action="./actions/checkout.php">

                            <div class="mb-3">
                                <label class="form-label">Customer Name</label>
                                <input type="text" class="form-control" name="customer_name" required>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">Phone Number</label>
                                <input type="text" class="form-control" name="customer_phone" required>
                            </div>

                            <input type="hidden" name="auto_checkout" value="1">

                        </form>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary" form="customerForm">
                            Complete Checkout & Print
                        </button>
                    </div>

                </div>
            </div>
        </div>


    </div>
</div>

<?php include('./includes/footer.php'); ?>