<?php
session_start();

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

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

$basePath = '../../';
include '../../includes/header.php';

/*
    Load HOLD orders
*/
$stmt = $db->prepare("
    SELECT
        o.*,
        t.table_name
    FROM orders o
    LEFT JOIN tables t ON o.table_id = t.id
    WHERE o.status = 'HOLD'
    ORDER BY o.order_date DESC
");
$stmt->execute();

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

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Held Orders</h3>

        <a href="index.php" class="btn btn-secondary">
            Back to POS
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-body table-responsive">

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

                <thead class="table-dark">
                    <tr>
                        <th>Order ID</th>
                        <th>Date</th>
                        <th>Table</th>
                        <th>Customer</th>
                        <th>Total</th>
                        <th width="220">Actions</th>
                    </tr>
                </thead>

                <tbody>

                <?php if (empty($orders)): ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted">
                            No held orders found.
                        </td>
                    </tr>
                <?php endif; ?>

                <?php foreach ($orders as $order): ?>
                    <tr>

                        <td>
                            #<?= $order['id'] ?>
                        </td>

                        <td>
                            <?= date(
                                'd-m-Y h:i A',
                                strtotime($order['order_date'])
                            ) ?>
                        </td>

                        <td>
                            <?= htmlspecialchars($order['table_name'] ?? 'N/A') ?>
                        </td>

                        <td>
                            <?= htmlspecialchars($order['customer_name'] ?? '') ?>
                        </td>

                        <td>
                            ₹ <?= number_format((float)$order['total'], 2) ?>
                        </td>

                        <td>

                            <a
                                href="load_held_order.php?id=<?= $order['id'] ?>"
                                class="btn btn-success btn-sm"
                            >
                                Resume
                            </a>

                            <a
                                href="cancel_held_order.php?id=<?= $order['id'] ?>"
                                class="btn btn-danger btn-sm"
                                onclick="return confirm('Cancel this held order?')"
                            >
                                Cancel
                            </a>

                        </td>

                    </tr>
                <?php endforeach; ?>

                </tbody>

            </table>

        </div>
    </div>

</div>

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