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

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

$allowed_roles = ['SUPER_ADMIN', 'HOSPITAL_ADMIN', 'HEAD_NURSE'];
if (!in_array($_SESSION['role_code'], $allowed_roles)) {
    header('Location: dashboard.php');
    exit();
}

// Fetch all staff (Doctors, Nurses, Technicians, Pharmacists)
$sql = "SELECT u.user_id, u.full_name, u.role_code, u.phone_primary, d.specialization
        FROM users u
        LEFT JOIN doctors d ON u.user_id = d.user_id
        WHERE u.role_code IN ('ENT_DOCTOR', 'ENT_CONSULTANT', 'NURSE', 'LAB_TECHNICIAN', 'PHARMACIST') AND u.status='ACTIVE'
        ORDER BY u.role_code, u.full_name";
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Staff Schedules')); ?>
    <link href="assets/libs/fullcalendar/main.min.css" rel="stylesheet" type="text/css" />
    <?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">Staff Schedules / Roster</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-3">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="mb-3">Staff List</h5>
                                    <div style="max-height: 600px; overflow-y: auto;">
                                        <?php if ($result->num_rows > 0): ?>
                                            <ul class="list-group">
                                                <?php while ($row = $result->fetch_assoc()):
                                                    $badge_class = match ($row['role_code']) {
                                                        'ENT_DOCTOR' => 'bg-primary',
                                                        'ENT_CONSULTANT' => 'bg-info',
                                                        'NURSE' => 'bg-success',
                                                        'LAB_TECHNICIAN' => 'bg-warning',
                                                        'PHARMACIST' => 'bg-danger',
                                                        default => 'bg-secondary'
                                                    };
                                                    ?>
                                                    <li class="list-group-item">
                                                        <div class="d-flex justify-content-between align-items-center">
                                                            <div>
                                                                <h6 class="mb-1">
                                                                    <?php echo htmlspecialchars($row['full_name']); ?></h6>
                                                                <span
                                                                    class="badge <?php echo $badge_class; ?>"><?php echo str_replace('_', ' ', $row['role_code']); ?></span>
                                                            </div>
                                                            <a href="edit_schedule.php?user_id=<?php echo $row['user_id']; ?>"
                                                                class="btn btn-sm btn-soft-primary"><i
                                                                    class="ri-edit-line"></i></a>
                                                        </div>
                                                    </li>
                                                <?php endwhile; ?>
                                            </ul>
                                        <?php else: ?>
                                            <p class="text-muted">No staff found.</p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-9">
                            <div class="card">
                                <div class="card-body">
                                    <div id="calendar"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/fullcalendar/main.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var calendarEl = document.getElementById('calendar');
            var calendar = new FullCalendar.Calendar(calendarEl, {
                initialView: 'timeGridWeek',
                headerToolbar: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'dayGridMonth,timeGridWeek,timeGridDay'
                },
                // Simulating Roster Events for visualization
                events: [
                    // This would normally fetch from a 'staff_roster' table
                    {
                        title: 'Nurse Shift: Morning',
                        start: '<?php echo date('Y-m-d'); ?>T07:00:00',
                        end: '<?php echo date('Y-m-d'); ?>T15:00:00',
                        backgroundColor: '#198754'
                    },
                    {
                        title: 'Dr. Consultation',
                        start: '<?php echo date('Y-m-d'); ?>T09:00:00',
                        end: '<?php echo date('Y-m-d'); ?>T17:00:00',
                        backgroundColor: '#0d6efd'
                    }
                ]
            });
            calendar.render();
        });
    </script>
</body>

</html>