<?php
// Session Guard
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='adm-nav-item active d-inline-flex justify-content-center' style='width: auto; padding: 10px 24px;'>
            <i class='fa fa-sign-in-alt me-2'></i> Login Sekarang
        </a>
    </div>";
    exit();
}

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

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

switch ($act) {
    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);">Manajemen Banner</h4>
            <small style="color: var(--adm-text-sub);">Kelola gambar banner dan teks promosi beranda</small>
        </div>
        <a href="?module=banner&act=tambahbanner" class="btn btn-danger d-inline-flex align-items-center gap-2">
            <i class="fa fa-plus"></i> Tambah Banner
        </a>
    </div>

    <div class="adm-card p-4">
        <div class="table-responsive">
            <table class="table align-middle text-nowrap 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" style="width: 50px;">NO</th>
                        <th scope="col" class="py-3" style="width: 180px;">GAMBAR</th>
                        <th scope="col" class="py-3">JUDUL BANNER</th>
                        <th scope="col" class="py-3">ISI BANNER</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 banner ORDER BY id_banner DESC";
                    $tampil = mysqli_query($konek, $query);
                    $no     = 1;

                    if (mysqli_num_rows($tampil) > 0) {
                        while ($r = mysqli_fetch_array($tampil)) {
                            $gambar_path = !empty($r['gambar']) && file_exists("../foto_banner/" . $r['gambar']) 
                                           ? "../foto_banner/" . $r['gambar'] 
                                           : "https://via.placeholder.com/300x120?text=No+Image";
                            echo "
                            <tr style='border-bottom: 1px solid var(--adm-border);'>
                                <td class='py-3'>{$no}</td>
                                <td class='py-3'>
                                    <img src='{$gambar_path}' class='rounded border' style='width: 140px; height: 70px; object-fit: cover; border-color: var(--adm-border) !important;'>
                                </td>
                                <td class='py-3 fw-semibold'>" . htmlspecialchars($r['judul']) . "</td>
                                <td class='py-3' style='color: var(--adm-text-sub); max-width: 300px; white-space: normal;'>" . htmlspecialchars($r['isi_banner']) . "</td>
                                <td class='py-3 text-center'>
                                    <div class='d-flex justify-content-center gap-2'>
                                        <a href='?module=banner&act=editbanner&id={$r['id_banner']}' 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=banner&act=hapus&id={$r['id_banner']}' onclick=\"return confirm('Yakin ingin menghapus banner 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='5' class='text-center py-4' style='color: var(--adm-text-sub);'>Belum ada data banner.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    case "tambahbanner":
?>
    <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 Banner Baru</h4>
            <small style="color: var(--adm-text-sub);">Isi formulir untuk menambahkan banner promosi</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-8">
            <div class="adm-card p-4">
                <form method="POST" enctype="multipart/form-data" action="<?= $aksi ?>?module=banner&act=input">
                    <div class="mb-3">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Judul Banner</label>
                        <input type="text" name="judul" class="form-control adm-input" placeholder="Masukkan judul banner..." required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Isi / Deskripsi Banner</label>
                        <textarea name="isi_banner" class="form-control adm-input" rows="3" placeholder="Masukkan ringkasan atau keterangan banner..."></textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Gambar Banner</label>
                        <input type="file" name="foto" class="form-control adm-input" accept="image/*" required>
                        <small class="text-muted mt-1 d-block" style="font-size: 0.75rem;">Format yang diperbolehkan: JPG, JPEG, PNG, WEBP.</small>
                    </div>

                    <div class="d-flex gap-2 pt-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 Banner
                        </button>
                        <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                            Batal
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php
    break;

    case "editbanner":
        $id     = mysqli_real_escape_string($konek, $_GET['id']);
        $query  = "SELECT * FROM banner WHERE id_banner='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);

        $gambar_path = !empty($r['gambar']) && file_exists("../foto_banner/" . $r['gambar']) 
                       ? "../foto_banner/" . $r['gambar'] 
                       : "https://via.placeholder.com/300x120?text=No+Image";
?>
    <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 Banner</h4>
            <small style="color: var(--adm-text-sub);">Perbarui data banner #<?= htmlspecialchars($r['id_banner']) ?></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-8">
            <div class="adm-card p-4">
                <form method="POST" enctype="multipart/form-data" action="<?= $aksi ?>?module=banner&act=update">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_banner']) ?>">
                    <input type="hidden" name="fotoku" value="<?= htmlspecialchars($r['gambar']) ?>">

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

                    <div class="mb-3">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Isi / Deskripsi Banner</label>
                        <textarea name="isi_banner" class="form-control adm-input" rows="3"><?= htmlspecialchars($r['isi_banner']) ?></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold d-block" style="color: var(--adm-text);">Gambar Saat Ini</label>
                        <img src="<?= $gambar_path ?>" class="rounded border mb-2" style="max-width: 240px; height: auto; border-color: var(--adm-border) !important;">
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Ganti Gambar Banner</label>
                        <input type="file" name="foto" class="form-control adm-input" accept="image/*">
                        <small class="text-muted mt-1 d-block" style="font-size: 0.75rem;">Biarkan kosong jika tidak ingin mengganti gambar saat ini.</small>
                    </div>

                    <div class="d-flex gap-2 pt-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 Banner
                        </button>
                        <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                            Batal
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php
    break;
}
?>