<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php
require 'includes/db_connect.php';
date_default_timezone_set('Africa/Lagos');

// Fetch staff list for filter dropdown
$staffList = $conn->query("SELECT id, fullname FROM staff ORDER BY fullname");

// Set default filter values
$filterDate = $_GET['date'] ?? date('Y-m-d');
$filterStaff = $_GET['staff_id'] ?? '';

// Build attendance query
$sql = "SELECT attendance.*, staff.fullname, staff.department 
        FROM attendance 
        JOIN staff ON attendance.staff_id = staff.id 
        WHERE 1";

$params = [];
$types = "";

if (!empty($filterDate)) {
    $sql .= " AND attendance.attendance_date = ?";
    $params[] = $filterDate;
    $types .= "s";
}

if (!empty($filterStaff)) {
    $sql .= " AND staff.id = ?";
    $params[] = $filterStaff;
    $types .= "i";
}

$sql .= " ORDER BY attendance.attendance_date DESC, staff.fullname ASC";

$stmt = $conn->prepare($sql);

if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}

$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Attendance History</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container py-5">
    <h2 class="mb-4">Attendance History</h2>

    <form class="row g-3 mb-4" method="GET">
        <div class="col-md-3">
            <label for="date" class="form-label">Filter by Date</label>
            <input type="date" name="date" id="date" class="form-control" value="<?= htmlspecialchars($filterDate) ?>">
        </div>
        <div class="col-md-4">
            <label for="staff_id" class="form-label">Filter by Staff</label>
            <select name="staff_id" id="staff_id" class="form-select">
                <option value="">-- All Staff --</option>
                <?php while ($staff = $staffList->fetch_assoc()): ?>
                    <option value="<?= $staff['id'] ?>" <?= ($staff['id'] == $filterStaff) ? 'selected' : '' ?>>
                        <?= htmlspecialchars($staff['fullname']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-2 align-self-end">
            <button type="submit" class="btn btn-primary w-100">Filter</button>
        </div>
    </form>

    <?php if ($result->num_rows > 0): ?>
        <table class="table table-bordered bg-white shadow">
            <thead class="table-dark">
                <tr>
                    <th>Date</th>
                    <th>Staff Name</th>
                    <th>Department</th>
                    <th>Time</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = $result->fetch_assoc()): ?>
                    <tr>
                        <td><?= htmlspecialchars($row['attendance_date']) ?></td>
                        <td><?= htmlspecialchars($row['fullname']) ?></td>
                        <td><?= htmlspecialchars($row['department']) ?></td>
                        <td><?= htmlspecialchars($row['attendance_time']) ?></td>
                        <td>
                            <span class="badge bg-<?= $row['status'] == 'Present' ? 'success' : 'danger' ?>">
                                <?= htmlspecialchars($row['status']) ?>
                            </span>
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    <?php else: ?>
        <div class="alert alert-warning">No attendance records found.</div>
    <?php endif; ?>
</div>
</body>
</html>
