<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once '../includes/db.php';
include '../includes/header_admin.php';

$date = $_GET['date'] ?? date('Y-m-d');
$bus_id = $_GET['bus_id'] ?? '';

// Fetch active buses for filter
$buses = $pdo->query("SELECT * FROM buses WHERE status = 'active'")->fetchAll();

// Fetch attendance logs
$query = "SELECT al.*, s.name as student_name, s.grade, b.bus_number, bs.stop_name 
          FROM attendance_logs al
          JOIN students s ON al.student_id = s.id
          JOIN buses b ON al.bus_id = b.id
          LEFT JOIN bus_stops bs ON s.stop_id = bs.id
          WHERE DATE(al.log_time) = ?";
$params = [$date];

if ($bus_id) {
    $query .= " AND al.bus_id = ?";
    $params[] = $bus_id;
}

$query .= " ORDER BY al.log_time DESC";
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$logs = $stmt->fetchAll();
?>

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
    <h2>Daily Attendance Logs</h2>
    <div style="display: flex; gap: 1rem;">
        <form method="GET" style="display: flex; gap: 1rem; align-items: center;">
            <input type="date" name="date" class="form-control" value="<?= $date ?>" onchange="this.form.submit()">
            <select name="bus_id" class="form-control" onchange="this.form.submit()">
                <option value="">-- All Buses --</option>
                <?php foreach ($buses as $bus): ?>
                    <option value="<?= $bus['id'] ?>" <?= $bus_id == $bus['id'] ? 'selected' : '' ?>>
                        <?= $bus['bus_number'] ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </form>
    </div>
</div>

<div class="card">
    <table style="width: 100%; border-collapse: collapse;">
        <thead>
            <tr style="text-align: left; border-bottom: 2px solid #eee;">
                <th style="padding: 1rem;">Time</th>
                <th style="padding: 1rem;">Student</th>
                <th style="padding: 1rem;">Class</th>
                <th style="padding: 1rem;">Bus</th>
                <th style="padding: 1rem;">Stop</th>
                <th style="padding: 1rem;">Status</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($logs)): ?>
                <tr>
                    <td colspan="6" style="padding: 2rem; text-align: center; color: var(--dark-gray);">No attendance records found for this selection.</td>
                </tr>
            <?php else: ?>
                <?php foreach ($logs as $log): ?>
                <tr style="border-bottom: 1px solid #eee;">
                    <td style="padding: 1rem;"><?= date('H:i:s', strtotime($log['log_time'])) ?></td>
                    <td style="padding: 1rem;"><strong><?= htmlspecialchars($log['student_name']) ?></strong></td>
                    <td style="padding: 1rem;"><?= htmlspecialchars($log['grade']) ?></td>
                    <td style="padding: 1rem;"><?= htmlspecialchars($log['bus_number']) ?></td>
                    <td style="padding: 1rem;"><?= htmlspecialchars($log['stop_name'] ?: 'N/A') ?></td>
                    <td style="padding: 1rem;">
                        <span style="padding: 0.3rem 0.8rem; border-radius: 1rem; font-size: 0.75rem; font-weight: bold; background: <?= $log['action'] == 'pickup' ? '#e1f7f0; color: #1dbf73' : '#ffeaea; color: #f94144' ?>">
                            <?= strtoupper($log['action']) ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php include '../includes/footer_admin.php'; ?>
