<?php
include 'config.php';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data Kehadiran - Aplikasi Kehadiran Ujian</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="index.php">
                <i class="fas fa-qrcode me-2"></i>Kehadiran Ujian
            </a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">Scan QR</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="data_kehadiran.php">Data Kehadiran</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="kelola_siswa.php">Kelola Siswa</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="generate_qr.php">Generate QR</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <h2 class="mb-4"><i class="fas fa-list me-2"></i>Data Kehadiran Ujian</h2>
        
        <!-- Filter Data -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="GET" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">Tanggal</label>
                        <input type="date" class="form-control" name="tanggal" value="<?= isset($_GET['tanggal']) ? $_GET['tanggal'] : '' ?>">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Mata Pelajaran</label>
                        <select class="form-select" name="mapel">
                            <option value="">Semua</option>
                            <?php
                            $sql = "SELECT * FROM mata_pelajaran";
                            $result = $conn->query($sql);
                            while($row = $result->fetch_assoc()) {
                                $selected = (isset($_GET['mapel']) && $_GET['mapel'] == $row['kode_mapel']) ? 'selected' : '';
                                echo "<option value='{$row['kode_mapel']}' $selected>{$row['nama_mapel']}</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Kelas</label>
                        <select class="form-select" name="kelas">
                            <option value="">Semua</option>
                            <?php
                            $sql = "SELECT DISTINCT kelas FROM siswa ORDER BY kelas";
                            $result = $conn->query($sql);
                            while($row = $result->fetch_assoc()) {
                                $selected = (isset($_GET['kelas']) && $_GET['kelas'] == $row['kelas']) ? 'selected' : '';
                                echo "<option value='{$row['kelas']}' $selected>{$row['kelas']}</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col-md-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-filter me-2"></i>Filter
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Tabel Data -->
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0"><i class="fas fa-table me-2"></i>Daftar Kehadiran</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>Hari/Tanggal</th>
                                <th>Jam</th>
                                <th>NIS</th>
                                <th>Nama Siswa</th>
                                <th>Kelas</th>
                                <th>Mata Pelajaran</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            // Build query dengan filter
                            $sql = "SELECT k.*, s.nama, s.kelas, m.nama_mapel 
                                    FROM kehadiran k 
                                    JOIN siswa s ON k.nis = s.nis 
                                    JOIN mata_pelajaran m ON k.kode_mapel = m.kode_mapel";
                            
                            $where = [];
                            if (isset($_GET['tanggal']) && !empty($_GET['tanggal'])) {
                                $where[] = "k.tanggal = '" . $conn->real_escape_string($_GET['tanggal']) . "'";
                            }
                            if (isset($_GET['mapel']) && !empty($_GET['mapel'])) {
                                $where[] = "k.kode_mapel = '" . $conn->real_escape_string($_GET['mapel']) . "'";
                            }
                            if (isset($_GET['kelas']) && !empty($_GET['kelas'])) {
                                $where[] = "s.kelas = '" . $conn->real_escape_string($_GET['kelas']) . "'";
                            }
                            
                            if (!empty($where)) {
                                $sql .= " WHERE " . implode(" AND ", $where);
                            }
                            
                            $sql .= " ORDER BY k.tanggal DESC, k.jam DESC";
                            
                            $result = $conn->query($sql);
                            $no = 1;
                            
                            if ($result->num_rows > 0) {
                                while($row = $result->fetch_assoc()) {
                                    echo "<tr>
                                            <td>{$no}</td>
                                            <td>{$row['hari']}, " . date('d-m-Y', strtotime($row['tanggal'])) . "</td>
                                            <td>{$row['jam']}</td>
                                            <td>{$row['nis']}</td>
                                            <td>{$row['nama']}</td>
                                            <td>{$row['kelas']}</td>
                                            <td>{$row['nama_mapel']}</td>
                                        </tr>";
                                    $no++;
                                }
                            } else {
                                echo "<tr><td colspan='7' class='text-center'>Tidak ada data kehadiran</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php $conn->close(); ?>