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

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

// Fetch Waiting Queue
// Assuming queue_status ENUM('WAITING','IN_PROGRESS','COMPLETED','SKIPPED')
$sql = "SELECT q.*, 
               p.first_name, p.last_name, 
               d.department_name,
               TIMESTAMPDIFF(MINUTE, q.queue_entry_time, NOW()) as wait_time
        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 = 'WAITING'
        ORDER BY q.priority_level = 'EMERGENCY' DESC, q.priority_level = 'URGENT' DESC, q.queue_entry_time ASC";
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Waiting Room Operations')); ?>
    <?php include 'layouts/head-css.php'; ?>
</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">Waiting Room / Live Queue</h4>
                                <div class="page-title-right">
                                    <a href="patient_checkin.php" class="btn btn-primary">Check-in Patient</a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['success'])): ?>
                        <div class="alert alert-success"><?php echo $_SESSION['success'];
                        unset($_SESSION['success']); ?>
                        </div>
                    <?php endif; ?>

                    <div class="row">
                        <?php
                        if ($result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                                $bg_class = "bg-white";
                                $border_class = "border-light";
                                if ($row['priority_level'] == 'EMERGENCY') {
                                    $bg_class = "bg-danger-subtle";
                                    $border_class = "border-danger";
                                } elseif ($row['priority_level'] == 'URGENT') {
                                    $bg_class = "bg-warning-subtle";
                                    $border_class = "border-warning";
                                }
                                ?>
                                <div class="col-xl-3 col-md-6">
                                    <div class="card <?php echo $bg_class; ?> <?php echo $border_class; ?> border">
                                        <div class="card-body">
                                            <div class="d-flex justify-content-between align-items-center mb-3">
                                                <h5 class="card-title mb-0">#<?php echo $row['queue_number']; ?></h5>
                                                <span class="badge bg-secondary"><?php echo $row['department_name']; ?></span>
                                            </div>
                                            <h6 class="mb-1">
                                                <?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                            </h6>
                                            <p class="text-muted mb-2"><i class="ri-time-line"></i> Waiting:
                                                <?php echo $row['wait_time']; ?> mins</p>
                                            <div class="d-flex gap-2 mt-3">
                                                <button class="btn btn-sm btn-success w-50"><i class="ri-play-line"></i>
                                                    Call</button>
                                                <button class="btn btn-sm btn-outline-danger w-50"><i class="ri-close-line"></i>
                                                    No Show</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php
                            }
                        } else {
                            echo '<div class="col-12"><div class="alert alert-info">No patients currently waiting.</div></div>';
                        }
                        ?>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script>
        // Auto-refresh every 30 seconds
        setTimeout(function () {
            location.reload();
        }, 30000);
    </script>
</body>

</html>