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

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

$start = $_GET['start'] ?? date('Y-m-01');
$end = $_GET['end'] ?? date('Y-m-t');

// Doctor Stats: Appointments, Prescriptions, Procedures
// We need to aggregate.
$sql = "SELECT 
            u.full_name,
            (SELECT COUNT(*) FROM appointments a WHERE a.doctor_id = u.user_id AND a.appointment_date BETWEEN ? AND ?) as appointments,
            (SELECT COUNT(*) FROM prescriptions p WHERE p.doctor_id = u.user_id AND p.prescribed_date BETWEEN ? AND ?) as prescriptions
        FROM users u 
        WHERE u.role_code IN ('ENT_DOCTOR', 'ENT_CONSULTANT')
        ORDER BY appointments DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss", $start, $end, $start, $end);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Doctor Performance')); ?>
    <?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">Doctor Performance</h4>
                                <div class="page-title-right">
                                    <form method="GET" class="d-flex gap-2">
                                        <input type="date" name="start" class="form-control form-control-sm"
                                            value="<?php echo $start; ?>">
                                        <input type="date" name="end" class="form-control form-control-sm"
                                            value="<?php echo $end; ?>">
                                        <button type="submit" class="btn btn-primary btn-sm">Update</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="perfTable" class="table table-bordered dt-responsive nowrap">
                                        <thead>
                                            <tr>
                                                <th>Doctor Name</th>
                                                <th>Appointments</th>
                                                <th>Prescriptions</th>
                                                <th>Efficiency Score</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()):
                                                // Dummy efficiency score logic for now
                                                $score = 'N/A';
                                                if ($row['appointments'] > 0)
                                                    $score = '98%';
                                                ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($row['full_name']); ?></td>
                                                    <td><?php echo $row['appointments']; ?></td>
                                                    <td><?php echo $row['prescriptions']; ?></td>
                                                    <td><span class="badge bg-success"><?php echo $score; ?></span></td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </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>$('#perfTable').DataTable();</script>
</body>

</html>