<?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');

// Staff Performance: Doctors, Nurses, Lab Techs, Receptionists
// Aggregating activity from activity_log or specific tables.
// Simple approach: Count logins or specific actions.
// Better: Use role-based queries.

$sql = "SELECT 
            u.full_name,
            u.role_code,
            COUNT(al.log_id) as activity_count,
            MAX(al.logged_at) as last_active
        FROM users u
        LEFT JOIN activity_log al ON u.user_id = al.user_id AND al.logged_at BETWEEN ? AND ?
        WHERE u.is_active = 1
        GROUP BY u.user_id
        ORDER BY activity_count DESC";

$stmt = $conn->prepare($sql);
$start_ts = $start . " 00:00:00";
$end_ts = $end . " 23:59:59";
$stmt->bind_param("ss", $start_ts, $end_ts);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Staff 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">Staff 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="staffPerfTable"
                                        class="table table-bordered dt-responsive nowrap table-striped">
                                        <thead>
                                            <tr>
                                                <th>Staff Name</th>
                                                <th>Role</th>
                                                <th>Activity Count</th>
                                                <th>Last Active</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($row['full_name']); ?></td>
                                                    <td><span
                                                            class="badge bg-primary"><?php echo $row['role_code']; ?></span>
                                                    </td>
                                                    <td><?php echo number_format($row['activity_count']); ?></td>
                                                    <td><?php echo $row['last_active'] ? date('M d, H:i', strtotime($row['last_active'])) : 'Never'; ?>
                                                    </td>
                                                    <td>
                                                        <?php if ($row['activity_count'] > 50): ?>
                                                            <span class="text-success"><i class="ri-arrow-up-line"></i>
                                                                High</span>
                                                        <?php elseif ($row['activity_count'] > 10): ?>
                                                            <span class="text-warning"><i class="ri-arrow-right-line"></i>
                                                                Moderate</span>
                                                        <?php else: ?>
                                                            <span class="text-secondary"><i class="ri-arrow-down-line"></i>
                                                                Low</span>
                                                        <?php endif; ?>
                                                    </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>$('#staffPerfTable').DataTable();</script>
</body>

</html>