<?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>
        .sidebar {
            min-height: 100vh;
            background-color: #212529;
            color: white;
            position: sticky;
            top: 0;
        }
        .sidebar a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: color 0.3s;
        }
        .sidebar a:hover {
            color: white;
        }
        .sidebar .nav-link.active {
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
        }
        .sidebar .nav-item {
            margin-bottom: 5px;
        }
        .content {
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 px-0 sidebar">
                <div class="d-flex flex-column p-3">
                    <a href="../../admin_dashboard" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
                        <span class="fs-4">Admin Panel</span>
                    </a>
                    <hr>
                    <ul class="nav nav-pills flex-column mb-auto">
                        <li class="nav-item">
                            <a href="../../admin_dashboard" class="nav-link">
                                <i class="fas fa-home me-2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_cabang_lomba" class="nav-link active">
                                <i class="fas fa-trophy me-2"></i> Cabang Lomba
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_kelola_peserta" class="nav-link">
                                <i class="fas fa-user-tie me-2"></i> Kelola Peserta
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_peserta_kota" class="nav-link">
                                <i class="fas fa-users me-2"></i> Peserta Kota Salatiga
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_peserta_kabupaten" class="nav-link">
                                <i class="fas fa-users me-2"></i> Peserta Kabupaten Semarang
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_peserta_kabupaten_2" class="nav-link">
                                <i class="fas fa-users me-2"></i> Peserta Kabupaten Demak
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_admin" class="nav-link">
                                <i class="fas fa-user-shield me-2"></i> Kelola Admin
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_juri" class="nav-link">
                                <i class="fas fa-user-tie me-2"></i> Kelola Juri
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_penilaian" class="nav-link">
                                <i class="fas fa-star me-2"></i> Hasil Penilaian
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="../admin_status_lomba" class="nav-link">
                                <i class="fas fa-toggle-on me-2"></i> Status Lomba
                            </a>
                        </li>
                    </ul>
                    <hr>
                    <div class="dropdown">
                        <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fas fa-user-circle me-2 fs-5"></i>
                            <strong><?php echo $_SESSION['nama']; ?></strong>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
                            
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="../../logout.php">Logout</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <!-- Content -->
            <div class="col-md-9 col-lg-10 content">
                <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>Jumlah Peserta Kota</th>
                                        <th>Jumlah Peserta Kabupaten</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
                                           FROM cabang_lomba cl
                                           ORDER BY cl.ID";
                                    $result = $conn->query($sql);
                                    
                                    if ($result->num_rows > 0) {
                                        while($row = $result->fetch_assoc()) {
                                            echo "<tr>";
                                            echo "<td>" . $row["ID"] . "</td>";
                                            echo "<td>" . $row["Bidang_Lomba"] . "</td>";
                                            echo "<td>" . $row["total_kota"] . "</td>";
                                            echo "<td>" . $row["total_kabupaten"] . "</td>";
                                            echo "<td>";
                                            echo "</td>";
                                            echo "</tr>";
                                        }
                                    } else {
                                        echo "<tr><td colspan='5' class='text-center'>Tidak ada data cabang lomba</td></tr>";
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </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 src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <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>
</body>
</html>

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