<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Fetch patients ready for checkout (e.g., Doctors mark them as 'COMPLETED' or 'BILLING_PENDING')
$sql = "SELECT q.*, 
               p.first_name, p.last_name, 
               d.department_name
        FROM patient_queue q
        JOIN patients p ON q.patient_id = p.patient_id
        JOIN departments d ON q.department_id = d.department_id
        WHERE q.queue_status = 'BILLING' OR (q.queue_status = 'COMPLETED' AND q.checkout_time IS NULL)
        ORDER BY q.queue_entry_time DESC";
// Assuming status logic.
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Patient Checkout')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Patient Checkout</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="checkoutTable"
                                        class="table table-bordered dt-responsive nowrap table-striped">
                                        <thead>
                                            <tr>
                                                <th>Queue #</th>
                                                <th>Patient</th>
                                                <th>Department</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            if ($result && $result->num_rows > 0) {
                                                while ($row = $result->fetch_assoc()):
                                                    ?>
                                                    <tr>
                                                        <td><?php echo $row['queue_number']; ?></td>
                                                        <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                                        </td>
                                                        <td><?php echo $row['department_name']; ?></td>
                                                        <td><span
                                                                class="badge bg-warning"><?php echo $row['queue_status']; ?></span>
                                                        </td>
                                                        <td>
                                                            <a href="create_invoice.php?patient_id=<?php echo $row['patient_id']; ?>&queue_id=<?php echo $row['queue_id']; ?>"
                                                                class="btn btn-sm btn-primary">
                                                                <i class="ri-money-dollar-circle-line"></i> Bill & Checkout
                                                            </a>
                                                            <a href="checkout_process.php?queue_id=<?php echo $row['queue_id']; ?>"
                                                                class="btn btn-sm btn-secondary">
                                                                <i class="ri-logout-box-line"></i> Clear
                                                            </a>
                                                        </td>
                                                    </tr>
                                                <?php endwhile;
                                            } else { ?>
                                                <tr>
                                                    <td colspan="5" class="text-center">No patients waiting for checkout.
                                                    </td>
                                                </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#checkoutTable').DataTable();</script>
</body>

</html>