<?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_galerifoto/aksi_galerifoto.php";
$act  = isset($_GET['act']) ? $_GET['act'] : '';

switch ($act) {
    // ==========================================
    // TAMPIL DATA GALERI FOTO (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 Foto</h4>
            <small style="color: var(--adm-text-sub);">Kelola koleksi foto dan dokumentasi kegiatan</small>
        </div>
        <a href="?module=galerifoto&act=tambahgalerifoto" class="btn btn-danger d-inline-flex align-items-center gap-2">
            <i class="fa fa-plus"></i> Tambah Foto
        </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: 160px;">FOTO</th>
                        <th scope="col" class="py-3">JUDUL FOTO</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 galeri ORDER BY id_galeri 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_galeri'] ?? '');
                            $gambar = !empty($r['gambar']) && file_exists("../foto_galeri/" . $r['gambar'])
                                      ? "../foto_galeri/" . $r['gambar']
                                      : "../foto_galeri/no-image.png";

                            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'>
                                    <img src='{$gambar}' alt='{$judul}' class='img-thumbnail rounded' style='max-width: 120px; max-height: 80px; object-fit: cover;'>
                                </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=galerifoto&act=editgalerifoto&id={$r['id_galeri']}' 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=galerifoto&act=hapus&id={$r['id_galeri']}' onclick=\"return confirm('Yakin ingin menghapus foto 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 foto galeri.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM TAMBAH GALERI FOTO
    // ==========================================
    case "tambahgalerifoto":
?>
    <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 Foto Galeri</h4>
            <small style="color: var(--adm-text-sub);">Unggah satu atau beberapa foto ke galeri</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" enctype="multipart/form-data" action="<?= $aksi ?>?module=galerifoto&act=input">
                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Judul Foto</label>
                        <div class="col-sm-9">
                            <input type="text" name="judul_galeri" class="form-control adm-input" placeholder="Masukkan judul foto..." required>
                        </div>
                    </div>

                    <div class="mb-4 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Pilih Foto</label>
                        <div class="col-sm-9">
                            <input type="file" name="foto[]" class="form-control adm-input" multiple accept="image/*" required>
                            <div class="form-text mt-2" style="color: var(--adm-text-sub);">
                                Format yang didukung: <code>JPG, JPEG, PNG, GIF, WEBP</code> (Maks. 3MB per file). Anda dapat memilih lebih dari satu file sekaligus.
                            </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 GALERI FOTO
    // ==========================================
    case "editgalerifoto":
        $id     = mysqli_real_escape_string($konek, $_GET['id'] ?? '');
        $query  = "SELECT * FROM galeri WHERE id_galeri='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);

        if (!$r) {
            echo "<script>alert('Data tidak ditemukan!'); window.location='?module=galerifoto';</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 Foto Galeri</h4>
            <small style="color: var(--adm-text-sub);">Perbarui informasi atau gambar galeri #<?= htmlspecialchars($r['id_galeri']) ?></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" enctype="multipart/form-data" action="<?= $aksi ?>?module=galerifoto&act=update">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_galeri']) ?>">
                    <input type="hidden" name="fotoku" value="<?= htmlspecialchars($r['gambar'] ?? '') ?>">

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

                    <div class="mb-3 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Foto saat ini</label>
                        <div class="col-sm-9">
                            <?php if (!empty($r['gambar']) && file_exists("../foto_galeri/" . $r['gambar'])): ?>
                                <img src="../foto_galeri/<?= htmlspecialchars($r['gambar']) ?>" class="img-thumbnail rounded" style="max-height: 180px;">
                            <?php else: ?>
                                <p class="text-muted mb-0">Belum ada foto.</p>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="mb-4 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Ganti Foto</label>
                        <div class="col-sm-9">
                            <input type="file" name="foto" class="form-control adm-input" accept="image/*">
                            <div class="form-text mt-2" style="color: var(--adm-text-sub);">
                                Biarkan kosong jika tidak ingin mengganti foto yang sudah ada.
                            </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;
}
?>