<?php
// Koneksi ke database
include "config.php";

// Query untuk mendapatkan daftar kelas
$queryKelas = "SELECT DISTINCT k.kode_kelas, k.nama_kelas, k.wali_kelas 
               FROM ctkpoin_pelanggaran_siswa ps
               JOIN ctkpoin_kelas k ON ps.kode_kelas = k.kode_kelas
               ORDER BY k.nama_kelas";
$resultKelas = mysqli_query($conn, $queryKelas);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Pelanggaran per Kelas</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Select2 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>
        body {
            background-color: #f8f9fa;
            padding: 20px;
        }
        .card {
            max-width: 600px;
            margin: 50px auto;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .btn-print {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
        }
        .select2-container--default .select2-selection--single {
            height: 38px;
            padding: 5px;
        }
        .select2-container--default .select2-selection--single .select2-selection__arrow {
            height: 36px;
        }
        .form-control[readonly] {
            background-color: #e9ecef;
            opacity: 1;
        }
        .hide-semua-siswa {
            display: none;
        }

    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0"><i class="fas fa-users me-2"></i>Cetak Laporan Pelanggaran per Kelas</h4>
            </div>
            <div class="card-body">
                <form action="cetak_kelas_pdf.php" method="POST" target="_blank">
                    <div class="mb-3">
                        <label for="kode_kelas" class="form-label">Pilih Kelas <span class="text-danger">*</span></label>
                        <select class="form-select select2-kelas" id="kode_kelas" name="kode_kelas" required style="width: 100%;">
                            <option value="">-- Pilih Kelas --</option>
                            <?php
                            if (mysqli_num_rows($resultKelas) > 0) {
                                while ($row = mysqli_fetch_assoc($resultKelas)) {
                                    echo "<option value='" . htmlspecialchars($row['kode_kelas']) . "'>" . 
                                         htmlspecialchars($row['nama_kelas']) . " - Wali Kelas: " . 
                                         htmlspecialchars($row['wali_kelas']) . "</option>";
                                }
                            }
                            ?>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label for="tahun_ajaran" class="form-label">Tahun Ajaran</label>
                        <input type="text" class="form-control" id="tahun_ajaran" 
                               value="2025/2026 - Semester Ganjil" readonly>
                        <input type="hidden" name="kode_tahun_ajaran" value="2025-ganjil">
                    </div>
                    
                    <div class="mb-3 form-check hide-semua-siswa">
                        <input type="checkbox" class="form-check-input" id="semua_siswa" name="semua_siswa" value="1" checked>
                        <label class="form-check-label" for="semua_siswa">Cetak semua siswa dalam kelas</label>
                    </div>
                    
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-print">
                            <i class="fas fa-file-pdf me-2"></i>Cetak PDF
                        </button>
                        <a href="index.php" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Kembali ke Dashboard
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Select2 JS -->
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    
    <script>
        $(document).ready(function() {
            // Inisialisasi Select2
            $('.select2-kelas').select2({
                placeholder: "Pilih kelas",
                allowClear: true,
                width: '100%'
            });
        });
    </script>
</body>
</html>

<?php
// Tutup koneksi
mysqli_close($conn);
?>