<?php
session_start();
require_once '../config.php';

// Cek apakah user sudah login dan role-nya admin
if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

$current_user = $_SESSION['user'];
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data Kehadiran - Admin Dashboard</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">
    <style>
        .navbar-brand {
            font-weight: bold;
        }
        .card {
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            border: none;
        }
        .user-info {
            background: linear-gradient(135deg, #dc3545, #c82333);
            color: white;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 20px;
        }
        .table-responsive {
            border-radius: 8px;
            overflow: hidden;
        }
        .table th {
            background: #343a40;
            color: white;
            border: none;
            font-weight: 600;
        }
        .filter-card {
            background: #f8f9fa;
            border-left: 4px solid #007bff;
        }
        .badge-kehadiran {
            font-size: 0.8rem;
            padding: 6px 12px;
        }
    </style>
</head>
<body>
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="dashboard.php">
                <i class="fas fa-tachometer-alt me-2"></i>Admin Dashboard
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="dashboard.php">
                            <i class="fas fa-home me-1"></i>Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="data_kehadiran.php">
                            <i class="fas fa-database me-1"></i>Data Kehadiran
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="kelola_siswa.php">
                            <i class="fas fa-users me-1"></i>Kelola Siswa
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="generate_qr.php">
                            <i class="fas fa-qrcode me-1"></i>Generate QR
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="../logout.php">
                            <i class="fas fa-sign-out-alt me-1"></i>Logout
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <!-- Info Pengguna -->
        <div class="user-info">
            <h5><i class="fas fa-user-shield me-2"></i>Selamat Datang, <?= htmlspecialchars($current_user['nama_lengkap']); ?></h5>
            <p class="mb-0">Anda login sebagai <strong>Administrator</strong> - Halaman Data Kehadiran</p>
        </div>

        <h2 class="mb-4"><i class="fas fa-list me-2"></i>Data Kehadiran Ujian</h2>
        
        <!-- Filter Data -->
        <div class="card filter-card mb-4">
            <div class="card-header bg-transparent">
                <h5 class="mb-0"><i class="fas fa-filter me-2"></i>Filter Data</h5>
            </div>
            <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 ujn_mata_pelajaran";
                            $result = $conn->query($sql);
                            if ($result && $result->num_rows > 0) {
                                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 ujn_siswa ORDER BY kelas";
                            $result = $conn->query($sql);
                            if ($result && $result->num_rows > 0) {
                                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 me-2">
                            <i class="fas fa-filter me-2"></i>Filter
                        </button>
                        <a href="data_kehadiran.php" class="btn btn-outline-secondary">
                            <i class="fas fa-refresh"></i>
                        </a>
                    </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-hover">
                        <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>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            // Build query dengan filter
                            $sql = "SELECT k.*, s.nama, s.kelas, m.nama_mapel 
                                    FROM ujn_kehadiran k 
                                    JOIN ujn_siswa s ON k.nis = s.nis 
                                    JOIN ujn_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 && $result->num_rows > 0) {
                                while($row = $result->fetch_assoc()) {
                                    echo "<tr>
                                            <td>{$no}</td>
                                            <td>
                                                <span class='badge bg-secondary'>{$row['hari']}</span><br>
                                                <small>" . date('d-m-Y', strtotime($row['tanggal'])) . "</small>
                                            </td>
                                            <td><span class='badge bg-success badge-kehadiran'>{$row['jam']}</span></td>
                                            <td><strong>{$row['nis']}</strong></td>
                                            <td>{$row['nama']}</td>
                                            <td><span class='badge bg-primary'>{$row['kelas']}</span></td>
                                            <td><span class='badge bg-info'>{$row['nama_mapel']}</span></td>
                                            <td><span class='badge bg-success'><i class='fas fa-check me-1'></i>Hadir</span></td>
                                        </tr>";
                                    $no++;
                                }
                            } else {
                                echo "<tr>
                                        <td colspan='8' class='text-center py-4'>
                                            <i class='fas fa-inbox fa-2x text-muted mb-3'></i><br>
                                            <span class='text-muted'>Tidak ada data kehadiran</span>
                                        </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(); ?>