<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../dripend/includes/db.php';

// Fetch Today's Queue
$today = date('Y-m-d');

// We fetch visits scheduled for today that are not yet completed
$stmt = $conn->prepare("
    SELECT 
        vm.id as visit_id, 
        vm.appointment_no, 
        vm.patient_no, 
        vm.visit_time, 
        vm.status, 
        vm.payment_category,
        p.first_name, 
        p.last_name, 
        p.gender_id,
        g.gender_name,
        TIMESTAMPDIFF(YEAR, p.dob, CURDATE()) AS age
    FROM visit_master vm
    JOIN patient p ON vm.patient_no = p.patient_no
    LEFT JOIN gender g ON p.gender_id = g.id
    WHERE vm.visit_date = ? AND vm.status IN ('Scheduled', 'In Progress')
    ORDER BY vm.visit_time ASC
");
$stmt->bind_param("s", $today);
$stmt->execute();
$queue = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

// Quick KPIs
$total_waiting = count($queue);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Doctor's Queue | DripDental</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
    :root { --sidebar-width: 240px; --e-blue: #1e3a8a; --bg: #f8fafc; --border: #e2e8f0; --text: #0f172a; }
    body { font-family: 'Segoe UI', sans-serif; background: var(--bg); margin: 0; color: var(--text); }
    .page-wrapper { margin-left: var(--sidebar-width); padding: 30px; min-height: 100vh; box-sizing: border-box; }
    
    .header-bar { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 25px; border-bottom: 2px solid var(--border); padding-bottom: 15px; }
    .header-bar h1 { margin: 0; font-size: 24px; font-weight: 800; color: var(--e-blue); }
    
    .kpi-card { background: #fff; padding: 20px; border-radius: 12px; border: 1px solid var(--border); border-left: 5px solid #f59e0b; display: inline-block; min-width: 200px; box-shadow: 0 4px 6px rgba(0,0,0,0.02); margin-bottom: 25px; }
    .kpi-card h3 { margin: 0 0 5px 0; font-size: 13px; color: #64748b; text-transform: uppercase; }
    .kpi-card p { margin: 0; font-size: 28px; font-weight: 900; color: #b45309; }

    .queue-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 20px; }
    .patient-card { background: #fff; border-radius: 12px; border: 1px solid var(--border); padding: 20px; transition: 0.2s; box-shadow: 0 2px 4px rgba(0,0,0,0.02); display: flex; flex-direction: column; }
    .patient-card:hover { transform: translateY(-3px); box-shadow: 0 8px 15px rgba(0,0,0,0.05); border-color: #cbd5e1; }
    
    .card-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 15px; border-bottom: 1px solid var(--border); padding-bottom: 10px; }
    .p-time { font-size: 18px; font-weight: 900; color: var(--e-blue); background: #eff6ff; padding: 6px 12px; border-radius: 8px; }
    .p-status { font-size: 11px; font-weight: 800; text-transform: uppercase; padding: 4px 8px; border-radius: 4px; background: #fef3c7; color: #b45309; }
    
    .p-name { font-size: 18px; font-weight: 800; margin: 0 0 5px 0; }
    .p-meta { font-size: 13px; color: #64748b; font-weight: 600; margin: 0 0 15px 0; }
    
    .badge-ins { display: inline-block; padding: 4px 8px; background: #f1f5f9; border-radius: 4px; font-size: 11px; font-weight: 800; color: #475569; }
    
    .btn-attend { background: var(--e-blue); color: #fff; text-decoration: none; padding: 12px; text-align: center; border-radius: 8px; font-weight: 800; margin-top: auto; transition: 0.2s; }
    .btn-attend:hover { background: #1e293b; }
</style>
</head>
<body>

<?php include('../../dripend/statpages/sidebar.php'); ?>

<div class="page-wrapper">
    <div class="header-bar">
        <h1><i class="fa-solid fa-stethoscope"></i> Today's Consultation Queue</h1>
        <div style="font-weight: 700; color: #64748b;"><?= date('l, d M Y') ?></div>
    </div>

    <div class="kpi-card">
        <h3>Patients Waiting</h3>
        <p><?= $total_waiting ?></p>
    </div>

    <div class="queue-grid">
        <?php if(empty($queue)): ?>
            <div style="grid-column: 1 / -1; text-align:center; padding:50px; color:#94a3b8; background:#fff; border-radius:12px; border:1px dashed #cbd5e1;">
                <i class="fa-solid fa-mug-hot" style="font-size:40px; margin-bottom:15px; color:#cbd5e1;"></i>
                <h2>No patients waiting.</h2>
                <p>Enjoy your coffee, Doctor!</p>
            </div>
        <?php else: ?>
            <?php foreach($queue as $q): ?>
                <div class="patient-card">
                    <div class="card-header">
                        <div class="p-time"><i class="fa-regular fa-clock"></i> <?= date('H:i', strtotime($q['visit_time'])) ?></div>
                        <div class="p-status"><?= $q['status'] ?></div>
                    </div>
                    
                    <h3 class="p-name"><?= htmlspecialchars($q['first_name'] . ' ' . $q['last_name']) ?></h3>
                    <p class="p-meta">
                        #<?= $q['patient_no'] ?> &nbsp;|&nbsp; 
                        <?= $q['gender_name'] ?? 'U' ?> &nbsp;|&nbsp; 
                        <?= $q['age'] ?> Yrs
                    </p>
                    
                    <div style="margin-bottom: 20px;">
                        <span class="badge-ins"><i class="fa-solid fa-wallet"></i> <?= $q['payment_category'] ?></span>
                    </div>
                    
                    <a href="encounter.php?visit_id=<?= $q['visit_id'] ?>&patient_id=<?= urlencode($q['patient_no']) ?>" class="btn-attend">
                        <i class="fa-solid fa-user-doctor"></i> Attend to Patient
                    </a>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

</body>
</html>