<?php
// admin_cabang_lomba.php
require_once '../../config.php';
requireRole('admin');

$conn = connectDB();
$error = '';
$success = '';

?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="../../../assets/logoFLS3N.png" type="image/x-icon">
    <title>Kelola Cabang Lomba - Sistem Penilaian Karya Siswa</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .content {
            padding: 20px;
        }
    </style>
</head>
<body>
    <?php include '../sidebar.php'; ?>
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">Kelola Cabang Lomba</h1>
                </div>
                
                <?php if (!empty($error)): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if (!empty($success)): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Bidang Lomba</th>
                                        <th><img src="../../../assets/kota-salatiga.png" alt="Kota" style="height: 20px; width: 20px; margin-right: 4px; vertical-align: middle;"> Kota Salatiga</th>
                                        <th><img src="../../../assets/kabupaten-semarang.png" alt="Kab Semarang" style="height: 20px; width: 20px; margin-right: 4px; vertical-align: middle;"> Kabupaten Semarang</th>
                                        <th><img src="../../../assets/kabupaten-demak.png" alt="Kab Demak" style="height: 20px; width: 20px; margin-right: 4px; vertical-align: middle;"> Kabupaten Demak</th>
                                        <th>Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sql = "SELECT cl.ID, cl.Bidang_Lomba, 
                                           (SELECT COUNT(*) FROM domisilikota WHERE ID_Cabang_Lomba = cl.ID) as total_kota,
                                           (SELECT COUNT(*) FROM domisilikabupaten WHERE ID_Cabang_Lomba = cl.ID) as total_kabupaten_semarang,
                                           (SELECT COUNT(*) FROM domisilikabupaten2 WHERE ID_Cabang_Lomba = cl.ID) as total_kabupaten_demak
                                           FROM cabang_lomba cl
                                           ORDER BY cl.ID";
                                    $result = $conn->query($sql);
                                    
                                    if ($result->num_rows > 0) {
                                        while($row = $result->fetch_assoc()) {
                                            $total_peserta = $row["total_kota"] + $row["total_kabupaten_semarang"] + $row["total_kabupaten_demak"];
                                            echo "<tr>";
                                            echo "<td>" . $row["ID"] . "</td>";
                                            echo "<td>" . $row["Bidang_Lomba"] . "</td>";
                                            echo "<td>" . $row["total_kota"] . "</td>";
                                            echo "<td>" . $row["total_kabupaten_semarang"] . "</td>";
                                            echo "<td>" . $row["total_kabupaten_demak"] . "</td>";
                                            echo "<td><strong>" . $total_peserta . "</strong></td>";
                                            echo "</tr>";
                                        }
                                    } else {
                                        echo "<tr><td colspan='6' class='text-center'>Tidak ada data cabang lomba</td></tr>";
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

    <!-- Modal Tambah Cabang Lomba -->
    <div class="modal fade" id="addCabangModal" tabindex="-1" aria-labelledby="addCabangModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addCabangModalLabel">Tambah Cabang Lomba</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form method="post" action="">
                    <div class="modal-body">
                        <input type="hidden" name="action" value="add">
                        <div class="mb-3">
                            <label for="bidang_lomba" class="form-label">Bidang Lomba</label>
                            <input type="text" class="form-control" id="bidang_lomba" name="bidang_lomba" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- Modal Edit Cabang Lomba -->
    <div class="modal fade" id="editCabangModal" tabindex="-1" aria-labelledby="editCabangModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editCabangModalLabel">Edit Cabang Lomba</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form method="post" action="">
                    <div class="modal-body">
                        <input type="hidden" name="action" value="edit">
                        <input type="hidden" name="id" id="edit-id">
                        <div class="mb-3">
                            <label for="edit-bidang_lomba" class="form-label">Bidang Lomba</label>
                            <input type="text" class="form-control" id="edit-bidang_lomba" name="bidang_lomba" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- Modal Hapus Cabang Lomba -->
    <div class="modal fade" id="deleteCabangModal" tabindex="-1" aria-labelledby="deleteCabangModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteCabangModalLabel">Konfirmasi Hapus</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>Apakah Anda yakin ingin menghapus cabang lomba <span id="delete-bidang-lomba"></span>?</p>
                </div>
                <div class="modal-footer">
                    <form method="post" action="">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="id" id="delete-id">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-danger">Hapus</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // Script untuk mengisi modal edit
        document.getElementById('editCabangModal').addEventListener('show.bs.modal', function (event) {
            var button = event.relatedTarget;
            var id = button.getAttribute('data-id');
            var bidang = button.getAttribute('data-bidang');
            
            document.getElementById('edit-id').value = id;
            document.getElementById('edit-bidang_lomba').value = bidang;
        });
        
        // Script untuk mengisi modal hapus
        document.getElementById('deleteCabangModal').addEventListener('show.bs.modal', function (event) {
            var button = event.relatedTarget;
            var id = button.getAttribute('data-id');
            var bidang = button.getAttribute('data-bidang');
            
            document.getElementById('delete-id').value = id;
            document.getElementById('delete-bidang-lomba').textContent = bidang;
        });
    </script>

    <?php include '../sidebar-footer.php'; ?>

<?php $conn->close(); ?>