<?php
// Session Guard: Keamanan akses halaman
if (empty($_SESSION['namauser']) && empty($_SESSION['passuser'])) {
    echo "
    <div class='adm-card text-center py-5' style='max-width: 480px; margin: 40px auto;'>
        <div class='mb-3' style='font-size: 3rem; color: var(--adm-primary);'>
            <i class='fa fa-lock'></i>
        </div>
        <h4 style='color: var(--adm-text); font-weight: 700;' class='mb-2'>Akses Terbatas</h4>
        <p style='color: var(--adm-text-sub); font-size: 0.9rem;' class='mb-4'>
            Silahkan login terlebih dahulu untuk mengakses modul ini.
        </p>
        <a href='../../index.php' class='btn btn-danger d-inline-flex align-items-center gap-2 px-4 py-2'>
            <i class='fa fa-sign-in-alt'></i> Login Sekarang
        </a>
    </div>";
    exit();
}

include "../config/koneksi.php";

$aksi = "modul/mod_video/aksi_video.php";
$act  = isset($_GET['act']) ? $_GET['act'] : '';

switch ($act) {
    // ==========================================
    // TAMPIL DATA GALERI VIDEO (DEFAULT)
    // ==========================================
    default:
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Galeri Video</h4>
            <small style="color: var(--adm-text-sub);">Kelola daftar video YouTube yang ditampilkan</small>
        </div>
        <a href="?module=video&act=tambahvideo" class="btn btn-danger d-inline-flex align-items-center gap-2">
            <i class="fa fa-plus"></i> Tambah Video
        </a>
    </div>

    <div class="adm-card p-4">
        <div class="table-responsive">
            <table class="table align-middle mb-0" style="color: var(--adm-text); border-color: var(--adm-border);">
                <thead>
                    <tr style="border-bottom: 2px solid var(--adm-border-active); color: var(--adm-text-sub); font-size: 0.8rem; text-transform: uppercase;">
                        <th scope="col" class="py-3 text-center" style="width: 50px;">NO</th>
                        <th scope="col" class="py-3 text-center" style="width: 320px;">PREVIEW VIDEO</th>
                        <th scope="col" class="py-3">JUDUL VIDEO</th>
                        <th scope="col" class="py-3 text-center" style="width: 140px;">AKSI</th>
                    </tr>
                </thead>
                <tbody style="font-size: 0.88rem;">
                    <?php
                    $query  = "SELECT * FROM video ORDER BY id_video DESC";
                    $tampil = mysqli_query($konek, $query);
                    $no     = 1;

                    if ($tampil && mysqli_num_rows($tampil) > 0) {
                        while ($r = mysqli_fetch_array($tampil)) {
                            $judul = htmlspecialchars($r['judul_video'] ?? '');
                            $embed_id = htmlspecialchars($r['link_youtube'] ?? '');

                            echo "
                            <tr style='border-bottom: 1px solid var(--adm-border);'>
                                <td class='py-3 text-center align-middle'>{$no}</td>
                                <td class='py-3 text-center align-middle'>
                                    <div class='ratio ratio-16x9 rounded overflow-hidden shadow-sm' style='max-width: 280px; margin: 0 auto;'>
                                        <iframe src='https://www.youtube.com/embed/{$embed_id}' title='{$judul}' allowfullscreen></iframe>
                                    </div>
                                </td>
                                <td class='py-3 align-middle fw-semibold'>{$judul}</td>
                                <td class='py-3 text-center align-middle'>
                                    <div class='d-flex justify-content-center gap-2'>
                                        <a href='?module=video&act=editvideo&id={$r['id_video']}' class='btn btn-sm btn-outline-primary d-inline-flex align-items-center gap-1' title='Edit'>
                                            <i class='fa fa-edit'></i> Edit
                                        </a>
                                        <a href='{$aksi}?module=video&act=hapus&id={$r['id_video']}' onclick=\"return confirm('Yakin ingin menghapus video ini?')\" class='btn btn-sm btn-outline-danger d-inline-flex align-items-center gap-1' title='Hapus'>
                                            <i class='fa fa-trash'></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>";
                            $no++;
                        }
                    } else {
                        echo "<tr><td colspan='4' class='text-center py-4' style='color: var(--adm-text-sub);'>Belum ada data video.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM TAMBAH VIDEO
    // ==========================================
    case "tambahvideo":
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Tambah Video Baru</h4>
            <small style="color: var(--adm-text-sub);">Masukkan tautan video dari YouTube</small>
        </div>
        <button onclick="self.history.back()" class="btn btn-sm btn-secondary d-inline-flex align-items-center gap-2">
            <i class="fa fa-arrow-left"></i> Kembali
        </button>
    </div>

    <div class="row">
        <div class="col-12 col-lg-9">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=video&act=input">
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Judul Video</label>
                        <div class="col-sm-9">
                            <input type="text" name="judul_video" class="form-control adm-input" placeholder="Masukkan judul video..." required>
                        </div>
                    </div>

                    <div class="mb-4 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Link YouTube</label>
                        <div class="col-sm-9">
                            <input type="text" name="link_youtube" class="form-control adm-input" placeholder="https://www.youtube.com/watch?v=XXXXXX" required>
                            <div class="form-text mt-2 p-3 rounded" style="background-color: rgba(0, 0, 0, 0.03); border: 1px solid var(--adm-border); color: var(--adm-text-sub); font-size: 0.825rem;">
                                <i class="fa fa-info-circle me-1 text-primary"></i> <strong>Petunjuk Penautan:</strong><br>
                                Anda bisa menyalin (copy) link penuh dari browser atau tombol share YouTube. Format yang didukung:
                                <ul class="mb-0 mt-1 ps-3">
                                    <li><code>https://www.youtube.com/watch?v=<b>dQw4w9WgXcQ</b></code></li>
                                    <li><code>https://youtu.be/<b>dQw4w9WgXcQ</b></code></li>
                                    <li>Atau cukup masukkan ID Video-nya saja: <code><b>dQw4w9WgXcQ</b></code></li>
                                </ul>
                            </div>
                        </div>
                    </div>

                    <div class="row pt-2">
                        <div class="col-sm-9 offset-sm-3 d-flex gap-2">
                            <button type="submit" class="btn btn-danger px-4 d-inline-flex align-items-center gap-2">
                                <i class="fa fa-save"></i> Simpan
                            </button>
                            <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                                Batal
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM EDIT VIDEO
    // ==========================================
    case "editvideo":
        $id     = mysqli_real_escape_string($konek, $_GET['id'] ?? '');
        $query  = "SELECT * FROM video WHERE id_video='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);

        if (!$r) {
            echo "<script>alert('Data video tidak ditemukan!'); window.location='?module=video';</script>";
            exit();
        }
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Edit Video</h4>
            <small style="color: var(--adm-text-sub);">Perbarui informasi video #<?= htmlspecialchars($r['id_video']) ?></small>
        </div>
        <button onclick="self.history.back()" class="btn btn-sm btn-secondary d-inline-flex align-items-center gap-2">
            <i class="fa fa-arrow-left"></i> Kembali
        </button>
    </div>

    <div class="row">
        <div class="col-12 col-lg-9">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=video&act=update">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_video']) ?>">

                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Judul Video</label>
                        <div class="col-sm-9">
                            <input type="text" name="judul_video" class="form-control adm-input" value="<?= htmlspecialchars($r['judul_video'] ?? '') ?>" required>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Video Saat Ini</label>
                        <div class="col-sm-9">
                            <div class="ratio ratio-16x9 rounded overflow-hidden shadow-sm" style="max-width: 320px;">
                                <iframe src="https://www.youtube.com/embed/<?= htmlspecialchars($r['link_youtube'] ?? '') ?>" allowfullscreen></iframe>
                            </div>
                        </div>
                    </div>

                    <div class="mb-4 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Link YouTube Baru</label>
                        <div class="col-sm-9">
                            <input type="text" name="link_youtube" class="form-control adm-input" value="https://www.youtube.com/watch?v=<?= htmlspecialchars($r['link_youtube'] ?? '') ?>" required>
                            <div class="form-text mt-2 p-3 rounded" style="background-color: rgba(0, 0, 0, 0.03); border: 1px solid var(--adm-border); color: var(--adm-text-sub); font-size: 0.825rem;">
                                <i class="fa fa-info-circle me-1 text-primary"></i> <strong>Petunjuk Penautan:</strong><br>
                                Tempelkan tautan penuh dari YouTube (misal: <code>https://www.youtube.com/watch?v=dQw4w9WgXcQ</code> atau <code>https://youtu.be/dQw4w9WgXcQ</code>).
                            </div>
                        </div>
                    </div>

                    <div class="row pt-2">
                        <div class="col-sm-9 offset-sm-3 d-flex gap-2">
                            <button type="submit" class="btn btn-danger px-4 d-inline-flex align-items-center gap-2">
                                <i class="fa fa-save"></i> Perbarui
                            </button>
                            <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                                Batal
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php
    break;
}
?>