<?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_profil/aksi_profil.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 Profil</h4>
            <small style="color: var(--adm-text-sub);">Kelola halaman profil statis dan informasi perusahaan/sistem</small>
        </div>
        
    </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: 60px;">NO</th>
                        <th scope="col" class="py-3">JUDUL PROFIL</th>
                        
                        <th scope="col" class="py-3 text-center" style="width: 150px;">AKSI</th>
                    </tr>
                </thead>
                <tbody style="font-size: 0.88rem;">
                    <?php
                    $query  = "SELECT * FROM profil ORDER BY id_profil ASC";
                    $tampil = mysqli_query($konek, $query);
                    $no     = 1;

                    if (mysqli_num_rows($tampil) > 0) {
                        while ($r = mysqli_fetch_array($tampil)) {
                            $has_image = !empty($r['gambar']) && file_exists("../foto_profil/" . $r['gambar']);
                            $badge_img = $has_image 
                                         ? "<span class='badge bg-success-subtle text-success border border-success-subtle'><i class='fa fa-image me-1'></i> Ada Gambar</span>" 
                                         : "<span class='badge bg-secondary-subtle text-secondary border border-secondary-subtle'><i class='fa fa-minus me-1'></i> Tanpa Gambar</span>";

                            echo "
                            <tr style='border-bottom: 1px solid var(--adm-border);'>
                                <td class='py-3'>{$no}</td>
                                <td class='py-3 fw-semibold'>" . htmlspecialchars($r['judul_profil']) . "</td>
                              
                                <td class='py-3 text-center'>
                                    <div class='d-flex justify-content-center gap-2'>
                                        <a href='?module=profil&act=editprofil&id={$r['id_profil']}' 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>
                                        
                                    </div>
                                </td>
                            </tr>";
                            $no++;
                        }
                    } else {
                        echo "<tr><td colspan='4' class='text-center py-4' style='color: var(--adm-text-sub);'>Belum ada data profil.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    case "tambahprofil":
?>
    <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 Profil Baru</h4>
            <small style="color: var(--adm-text-sub);">Buat halaman profil atau informasi baru</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-10">
            <div class="adm-card p-4">
                <form method="POST" enctype="multipart/form-data" action="<?= $aksi ?>?module=profil&act=input">
                    <div class="mb-3">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Judul Profil</label>
                        <input type="text" name="judul_profil" class="form-control adm-input" placeholder="Contoh: Visi & Misi, Sejarah, dsb..." required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Isi Halaman Profil</label>
                        <textarea name="isi_profil" id="lokomedia" class="form-control adm-input" style="height: 320px;"></textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Gambar (Opsional)</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;">Format disarankan: JPG, PNG, atau 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
                        </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 "editprofil":
        $id     = mysqli_real_escape_string($konek, $_GET['id'] ?? '');
        $query  = "SELECT * FROM profil WHERE id_profil='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);

        // Cek ketersediaan gambar di database dan fisik file
        $has_image = !empty($r['gambar']) && file_exists("../foto_profil/" . $r['gambar']);
?>
    <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 Profil</h4>
            <small style="color: var(--adm-text-sub);">Perbarui informasi profil #<?= htmlspecialchars($r['id_profil']) ?></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-10">
            <div class="adm-card p-4">
                <form method="POST" enctype="multipart/form-data" action="<?= $aksi ?>?module=profil&act=update">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_profil']) ?>">
                    <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 Profil</label>
                        <input type="text" name="judul_profil" class="form-control adm-input" value="<?= htmlspecialchars($r['judul_profil']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold" style="color: var(--adm-text);">Isi Halaman Profil</label>
                        <textarea name="isi_profil" id="lokomedia" class="form-control adm-input" style="height: 320px;"><?= htmlspecialchars($r['isi_profil']) ?></textarea>
                    </div>

                    <?php if ($has_image): ?>
                        <!-- Section Gambar HANYA Muncul Jika Ada Gambar di Database -->
                        <div class="mb-3">
                            <label class="form-label fw-semibold d-block" style="color: var(--adm-text);">Gambar Saat Ini</label>
                            <img src="../foto_profil/<?= $r['gambar'] ?>" 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</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;">Kosongkan jika tidak ingin mengganti gambar saat ini.</small>
                        </div>
                    <?php endif; ?>

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