<?php

// admin_status_lomba.php

require_once '../../config.php';

requireRole('admin');



$conn = connectDB();

$error = '';

$success = '';



// Process status update

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST['action'] == 'update_status') {

    $name = $_POST['name'];

    $status = $_POST['status'];

    $admin_id = $_SESSION['user_id']; // Assuming you store the admin's ID in session

    

    // Update the status

    $stmt = $conn->prepare("UPDATE status_lomba SET status = ?, updated_by = ? WHERE name = ?");

    $stmt->bind_param("sis", $status, $admin_id, $name);

    

    if ($stmt->execute()) {

        $success = "Status lomba berhasil diperbarui!";

    } else {

        $error = "Gagal memperbarui status lomba: " . $conn->error;

    }

}



// Fetch current status

$kota_salatiga_status = "open";

$kabupaten_semarang_status = "open";

$kabupaten_demak_status = "open";

$pendaftaran_sekolah_status = "open";

// Track which statuses were found

$found_regions = [];



$stmt = $conn->prepare("SELECT name, status FROM status_lomba WHERE name IN ('kota_salatiga', 'kabupaten_semarang', 'kabupaten_demak', 'pendaftaran_sekolah')");

$stmt->execute();

$result = $stmt->get_result();



while ($row = $result->fetch_assoc()) {

    $found_regions[] = $row['name'];

    if ($row['name'] == 'kota_salatiga') {

        $kota_salatiga_status = $row['status'];

    } elseif ($row['name'] == 'kabupaten_semarang') {

        $kabupaten_semarang_status = $row['status'];

    } elseif ($row['name'] == 'kabupaten_demak') {

        $kabupaten_demak_status = $row['status'];

    } elseif ($row['name'] == 'pendaftaran_sekolah') {

        $pendaftaran_sekolah_status = $row['status'];

    }

}



// Insert missing status records with default 'open' status

$regions_to_check = ['kota_salatiga', 'kabupaten_semarang', 'kabupaten_demak', 'pendaftaran_sekolah'];

foreach ($regions_to_check as $region) {

    if (!in_array($region, $found_regions)) {

        $stmt = $conn->prepare("INSERT INTO status_lomba (name, status) VALUES (?, ?)");

        $status_default = 'open';

        $stmt->bind_param("ss", $region, $status_default);

        $stmt->execute();

    }

}

?>



<!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 Status 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: 0px;
        max-width: 1200px;
        margin: 0 auto;

        }

        .status-card {

            border-radius: 10px;

            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);

            transition: all 0.3s ease;

            margin-bottom: 20px;

        }

        .status-card:hover {

            transform: translateY(-5px);

            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);

        }

        .status-header {

            padding: 15px 20px;

            border-radius: 10px 10px 0 0;

            color: white;

            font-weight: bold;

        }

        .status-header.kota {

            background: linear-gradient(90deg, #ef4444, #f87171);

        }

        .status-header.kabupaten {

            background: linear-gradient(90deg, #3b82f6, #60a5fa);

        }

        .status-body {

            padding: 20px;

        }

        .status-badge {

            font-size: 0.85rem;

            padding: 5px 12px;

            border-radius: 20px;

            font-weight: bold;

            display: inline-block;

            margin-bottom: 10px;

        }

        .status-badge.open {

            background-color: #dcfce7;

            color: #16a34a;

            border: 1px solid #86efac;

        }

        .status-badge.closed {

            background-color: #fee2e2;

            color: #ef4444;

            border: 1px solid #fecaca;

        }

        .status-description {

            margin-bottom: 20px;

            color: #6b7280;

        }

        .toggle-container {

            display: flex;

            align-items: center;

        }

        .toggle-switch {

            position: relative;

            display: inline-block;

            width: 60px;

            height: 34px;

            margin-right: 15px;

        }

        .toggle-switch input {

            opacity: 0;

            width: 0;

            height: 0;

        }

        .toggle-slider {

            position: absolute;

            cursor: pointer;

            top: 0;

            left: 0;

            right: 0;

            bottom: 0;

            background-color: #ccc;

            transition: .4s;

            border-radius: 34px;

        }

        .toggle-slider:before {

            position: absolute;

            content: "";

            height: 26px;

            width: 26px;

            left: 4px;

            bottom: 4px;

            background-color: white;

            transition: .4s;

            border-radius: 50%;

        }

        input:checked + .toggle-slider {

            background-color: #10b981;

        }

        input:focus + .toggle-slider {

            box-shadow: 0 0 1px #10b981;

        }

        input:checked + .toggle-slider:before {

            transform: translateX(26px);

        }

        .toggle-label {

            font-weight: 600;

        }

        .update-info {

            font-size: 0.85rem;

            color: #6b7280;

            margin-top: 10px;

            font-style: italic;

        }

    </style>

</head>

<body>

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

            <!-- 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 Status Lomba</h1>

                </div>

                

                <?php if (!empty($error)): ?>

                    <div class="alert alert-danger alert-dismissible fade show" role="alert">

                        <?php echo $error; ?>

                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

                    </div>

                <?php endif; ?>

                

                <?php if (!empty($success)): ?>

                    <div class="alert alert-success alert-dismissible fade show" role="alert">

                        <?php echo $success; ?>

                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

                    </div>

                <?php endif; ?>

                

                <div class="row">

                    <div class="col-lg-6">

                        <div class="card status-card">

                            <div class="status-header kota">

                                <i class="fas fa-city me-2"></i> Lomba FLS3N Kota Salatiga

                            </div>

                            <div class="status-body">

                                <div class="status-badge <?php echo $kota_salatiga_status; ?>">

                                    <?php echo ($kota_salatiga_status == 'open') ? 'DIBUKA' : 'DITUTUP'; ?>

                                </div>

                                <div class="status-description">

                                    <p>Mengontrol status pendaftaran dan akses lomba untuk peserta dari Kota Salatiga. 

                                    Ketika ditutup, peserta tidak dapat mendaftar atau mengakses lomba.</p>

                                </div>

                                

                                <form method="post" action="" id="form-kota-salatiga">

                                    <input type="hidden" name="action" value="update_status">

                                    <input type="hidden" name="name" value="kota_salatiga">

                                    <input type="hidden" name="status" id="status-kota-salatiga" value="<?php echo $kota_salatiga_status; ?>">

                                    

                                    <div class="toggle-container">

                                        <label class="toggle-switch">

                                            <input type="checkbox" id="toggle-kota-salatiga" <?php echo ($kota_salatiga_status == 'open') ? 'checked' : ''; ?>>

                                            <span class="toggle-slider"></span>

                                        </label>

                                        <span class="toggle-label" id="label-kota-salatiga">

                                            <?php echo ($kota_salatiga_status == 'open') ? 'Lomba Dibuka' : 'Lomba Ditutup'; ?>

                                        </span>

                                    </div>

                                    

                                    <?php

                                    // Get last update information

                                    $stmt = $conn->prepare("SELECT sl.updated_at, u.nama_lengkap FROM status_lomba sl 

                                                           LEFT JOIN users u ON sl.updated_by = u.id 

                                                           WHERE sl.name = 'kota_salatiga'");

                                    $stmt->execute();

                                    $result = $stmt->get_result();

                                    if ($row = $result->fetch_assoc()) {

                                        echo '<div class="update-info">Terakhir diperbarui: ' . date('d-m-Y H:i', strtotime($row['updated_at']));

                                        if (!empty($row['nama_lengkap'])) {

                                            echo ' oleh ' . $row['nama_lengkap'];

                                        }

                                        echo '</div>';

                                    }

                                    ?>

                                </form>

                            </div>

                        </div>

                    </div>

                    

                    <div class="col-lg-6">

                        <div class="card status-card">

                            <div class="status-header kabupaten">

                                <i class="fas fa-map-marked-alt me-2"></i> Lomba FLS3N Kabupaten Semarang

                            </div>

                            <div class="status-body">

                                <div class="status-badge <?php echo $kabupaten_semarang_status; ?>">

                                    <?php echo ($kabupaten_semarang_status == 'open') ? 'DIBUKA' : 'DITUTUP'; ?>

                                </div>

                                <div class="status-description">

                                    <p>Mengontrol status pendaftaran dan akses lomba untuk peserta dari Kabupaten Semarang. 

                                    Ketika ditutup, peserta tidak dapat mendaftar atau mengakses lomba.</p>

                                </div>

                                

                                <form method="post" action="" id="form-kabupaten-semarang">

                                    <input type="hidden" name="action" value="update_status">

                                    <input type="hidden" name="name" value="kabupaten_semarang">

                                    <input type="hidden" name="status" id="status-kabupaten-semarang" value="<?php echo $kabupaten_semarang_status; ?>">

                                    

                                    <div class="toggle-container">

                                        <label class="toggle-switch">

                                            <input type="checkbox" id="toggle-kabupaten-semarang" <?php echo ($kabupaten_semarang_status == 'open') ? 'checked' : ''; ?>>

                                            <span class="toggle-slider"></span>

                                        </label>

                                        <span class="toggle-label" id="label-kabupaten-semarang">

                                            <?php echo ($kabupaten_semarang_status == 'open') ? 'Lomba Dibuka' : 'Lomba Ditutup'; ?>

                                        </span>

                                    </div>

                                    

                                    <?php

                                    // Get last update information

                                    $stmt = $conn->prepare("SELECT sl.updated_at, u.nama_lengkap FROM status_lomba sl 

                                                           LEFT JOIN users u ON sl.updated_by = u.id 

                                                           WHERE sl.name = 'kabupaten_semarang'");

                                    $stmt->execute();

                                    $result = $stmt->get_result();

                                    if ($row = $result->fetch_assoc()) {

                                        echo '<div class="update-info">Terakhir diperbarui: ' . date('d-m-Y H:i', strtotime($row['updated_at']));

                                        if (!empty($row['nama_lengkap'])) {

                                            echo ' oleh ' . $row['nama_lengkap'];

                                        }

                                        echo '</div>';

                                    }

                                    ?>

                                </form>

                            </div>

                        </div>

                    </div>

                    

                    <div class="col-lg-6">

                        <div class="card status-card">

                            <div class="status-header kabupaten">

                                <i class="fas fa-map-marked-alt me-2"></i> Lomba FLS3N Kabupaten Demak

                            </div>

                            <div class="status-body">

                                <div class="status-badge <?php echo $kabupaten_demak_status; ?>">

                                    <?php echo ($kabupaten_demak_status == 'open') ? 'DIBUKA' : 'DITUTUP'; ?>

                                </div>

                                <div class="status-description">

                                    <p>Mengontrol status pendaftaran dan akses lomba untuk peserta dari Kabupaten Demak. 

                                    Ketika ditutup, peserta tidak dapat mendaftar atau mengakses lomba.</p>

                                </div>

                                

                                <form method="post" action="" id="form-kabupaten-demak">

                                    <input type="hidden" name="action" value="update_status">

                                    <input type="hidden" name="name" value="kabupaten_demak">

                                    <input type="hidden" name="status" id="status-kabupaten-demak" value="<?php echo $kabupaten_demak_status; ?>">

                                    

                                    <div class="toggle-container">

                                        <label class="toggle-switch">

                                            <input type="checkbox" id="toggle-kabupaten-demak" <?php echo ($kabupaten_demak_status == 'open') ? 'checked' : ''; ?>>

                                            <span class="toggle-slider"></span>

                                        </label>

                                        <span class="toggle-label" id="label-kabupaten-demak">

                                            <?php echo ($kabupaten_demak_status == 'open') ? 'Lomba Dibuka' : 'Lomba Ditutup'; ?>

                                        </span>

                                    </div>

                                    

                                    <?php

                                    // Get last update information

                                    $stmt = $conn->prepare("SELECT sl.updated_at, u.nama_lengkap FROM status_lomba sl 

                                                           LEFT JOIN users u ON sl.updated_by = u.id 

                                                           WHERE sl.name = 'kabupaten_demak'");

                                    $stmt->execute();

                                    $result = $stmt->get_result();

                                    if ($row = $result->fetch_assoc()) {

                                        echo '<div class="update-info">Terakhir diperbarui: ' . date('d-m-Y H:i', strtotime($row['updated_at']));

                                        if (!empty($row['nama_lengkap'])) {

                                            echo ' oleh ' . $row['nama_lengkap'];

                                        }

                                        echo '</div>';

                                    }

                                    ?>

                                </form>

                            </div>

                        </div>

                    </div>

              
                    <div class="col-lg-6">

                        <div class="card status-card">

                            <div class="status-header kota">

                                <i class="fas fa-school me-2"></i> Pendaftaran Sekolah

                            </div>

                            <div class="status-body">

                                <div class="status-badge <?php echo $pendaftaran_sekolah_status; ?>">

                                    <?php echo ($pendaftaran_sekolah_status == 'open') ? 'DIBUKA' : 'DITUTUP'; ?>

                                </div>

                                <div class="status-description">

                                    <p>Mengontrol status pendaftaran sekolah untuk mengikuti FLS3N. 

                                    Ketika ditutup, menu pendaftaran sekolah akan disembunyikan dari dashboard utama.</p>

                                </div>

                                

                                <form method="post" action="" id="form-pendaftaran-sekolah">

                                    <input type="hidden" name="action" value="update_status">

                                    <input type="hidden" name="name" value="pendaftaran_sekolah">

                                    <input type="hidden" name="status" id="status-pendaftaran-sekolah" value="<?php echo $pendaftaran_sekolah_status; ?>">

                                    

                                    <div class="toggle-container">

                                        <label class="toggle-switch">

                                            <input type="checkbox" id="toggle-pendaftaran-sekolah" <?php echo ($pendaftaran_sekolah_status == 'open') ? 'checked' : ''; ?>>

                                            <span class="toggle-slider"></span>

                                        </label>

                                        <span class="toggle-label" id="label-pendaftaran-sekolah">

                                            <?php echo ($pendaftaran_sekolah_status == 'open') ? 'Pendaftaran Dibuka' : 'Pendaftaran Ditutup'; ?>

                                        </span>

                                    </div>

                                    

                                    <?php

                                    // Get last update information

                                    $stmt = $conn->prepare("SELECT sl.updated_at, u.nama_lengkap FROM status_lomba sl 

                                                           LEFT JOIN users u ON sl.updated_by = u.id 

                                                           WHERE sl.name = 'pendaftaran_sekolah'");

                                    $stmt->execute();

                                    $result = $stmt->get_result();

                                    if ($row = $result->fetch_assoc()) {

                                        echo '<div class="update-info">Terakhir diperbarui: ' . date('d-m-Y H:i', strtotime($row['updated_at']));

                                        if (!empty($row['nama_lengkap'])) {

                                            echo ' oleh ' . $row['nama_lengkap'];

                                        }

                                        echo '</div>';

                                    }

                                    ?>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

                

                <div class="row mt-4">

                    <div class="col-12">

                        <div class="card">

                            <div class="card-header bg-info text-white">

                                <i class="fas fa-info-circle me-2"></i> Informasi Status Lomba

                            </div>

                            <div class="card-body">

                                <h5 class="card-title">Bagaimana cara kerja status lomba?</h5>

                                <ul>

                                    <li>Status lomba mengontrol apakah peserta dapat mendaftar dan mengakses lomba dari domisili tertentu.</li>

                                    <li>Ketika status lomba <strong>DIBUKA</strong>, peserta dapat mendaftar dan mengakses lomba seperti biasa.</li>

                                    <li>Ketika status lomba <strong>DITUTUP</strong>, tombol "Masuk Dashboard" pada halaman utama akan dinonaktifkan dan pesan akan ditampilkan menginformasikan bahwa pendaftaran telah ditutup.</li>

                                    <li>Perubahan status akan langsung terlihat pada halaman utama website.</li>

                                    <li>Menutup status lomba tidak akan menghapus data peserta atau lomba yang sudah ada.</li>

                                </ul>

                            </div>

                        </div>

                    </div>

                </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 for Kota Salatiga toggle

        document.getElementById('toggle-kota-salatiga').addEventListener('change', function() {

            const statusInput = document.getElementById('status-kota-salatiga');

            const label = document.getElementById('label-kota-salatiga');

            

            if (this.checked) {

                statusInput.value = 'open';

                label.textContent = 'Lomba Dibuka';

            } else {

                statusInput.value = 'closed';

                label.textContent = 'Lomba Ditutup';

            }

            

            // Submit the form

            document.getElementById('form-kota-salatiga').submit();

        });

        

        // Script for Kabupaten Semarang toggle

        document.getElementById('toggle-kabupaten-semarang').addEventListener('change', function() {

            const statusInput = document.getElementById('status-kabupaten-semarang');

            const label = document.getElementById('label-kabupaten-semarang');

            

            if (this.checked) {

                statusInput.value = 'open';

                label.textContent = 'Lomba Dibuka';

            } else {

                statusInput.value = 'closed';

                label.textContent = 'Lomba Ditutup';

            }

            

            // Submit the form

            document.getElementById('form-kabupaten-semarang').submit();

        });

        

        // Script for Kabupaten Demak toggle

        document.getElementById('toggle-kabupaten-demak').addEventListener('change', function() {

            const statusInput = document.getElementById('status-kabupaten-demak');

            const label = document.getElementById('label-kabupaten-demak');

            

            if (this.checked) {

                statusInput.value = 'open';

                label.textContent = 'Lomba Dibuka';

            } else {

                statusInput.value = 'closed';

                label.textContent = 'Lomba Ditutup';

            }

            

            // Submit the form

            document.getElementById('form-kabupaten-demak').submit();

        });

        

        // Script for Pendaftaran Sekolah toggle

        document.getElementById('toggle-pendaftaran-sekolah').addEventListener('change', function() {

            const statusInput = document.getElementById('status-pendaftaran-sekolah');

            const label = document.getElementById('label-pendaftaran-sekolah');

            

            if (this.checked) {

                statusInput.value = 'open';

                label.textContent = 'Pendaftaran Dibuka';

            } else {

                statusInput.value = 'closed';

                label.textContent = 'Pendaftaran Ditutup';

            }

            

            // Submit the form

            document.getElementById('form-pendaftaran-sekolah').submit();

        });

        

        // Auto-hide alerts after 5 seconds

        window.addEventListener('DOMContentLoaded', (event) => {

            setTimeout(() => {

                const alerts = document.querySelectorAll('.alert');

                alerts.forEach(alert => {

                    const bsAlert = new bootstrap.Alert(alert);

                    bsAlert.close();

                });

            }, 5000);

        });

    </script>

</body>

</html>



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