<?php
// Session Guard: Keamanan akses halaman
if (empty($_SESSION['namauser']) && empty($_SESSION['passuser'])) {
    echo "
    <div class='card bg-dark text-light border-secondary shadow-lg text-center py-5 mx-auto my-5' style='max-width: 420px; border-radius: 16px; background-color: #1e293b !important;'>
        <div class='card-body p-4'>
            <div class='mb-3 text-danger'>
                <i class='fa fa-lock fa-3x'></i>
            </div>
            <h4 class='fw-bold mb-2 text-white'>Akses Terbatas</h4>
            <p class='text-secondary small mb-4'>
                Silakan login terlebih dahulu untuk mengakses modul ini.
            </p>
            <a href='../../index.php' class='btn btn-danger w-100 py-2 fw-semibold shadow-sm'>
                <i class='fa fa-sign-in-alt me-1'></i> Login Sekarang
            </a>
        </div>
    </div>";
    exit();
}

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

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

switch ($act) {
    // ==========================================
    // TAMPIL DETAIL IDENTITAS WEB (DEFAULT)
    // ==========================================
    default:
        $query = "SELECT * FROM identitas LIMIT 1";
        $hasil = mysqli_query($konek, $query);
        $r     = mysqli_fetch_array($hasil);

        $id_identitas   = $r['id_identitas'] ?? '';
        $nama_pemilik   = htmlspecialchars($r['nama_pemilik'] ?? '-');
        $judul_website  = htmlspecialchars($r['judul_website'] ?? '-');
        $alamat_website = htmlspecialchars($r['alamat_website'] ?? '-');
        $meta_deskripsi = htmlspecialchars($r['meta_deskripsi'] ?? '-');
        $meta_keyword   = htmlspecialchars($r['meta_keyword'] ?? '-');
        $favicon        = !empty($r['favicon']) ? "../foto_icon/" . htmlspecialchars($r['favicon']) : '';
?>
    <!-- Custom Dark Inline Styles -->
    <style>
        .dark-card {
            background-color: #1e293b !important;
            border: 1px solid rgba(255, 255, 255, 0.08) !important;
            border-radius: 16px !important;
        }
        .dark-input {
            background-color: #0f172a !important;
            border: 1px solid #334155 !important;
            color: #f8fafc !important;
            border-radius: 10px !important;
        }
        .dark-input:focus {
            background-color: #0f172a !important;
            border-color: #f43f5e !important;
            color: #ffffff !important;
            box-shadow: 0 0 0 0.25rem rgba(244, 63, 94, 0.15) !important;
        }
        .text-subtle { color: #94a3b8 !important; }
        .icon-box {
            width: 36px;
            height: 36px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 10px;
            background: rgba(255, 255, 255, 0.05);
        }
    </style>

    <!-- Header Section -->
    <div class="row align-items-center gy-3 mb-4">
        <div class="col-12 col-md">
            <h4 class="fw-bold mb-1 text-white">Identitas Website</h4>
            <p class="text-subtle small mb-0">Kelola informasi dasar, meta SEO, dan konfigurasi skrip sistem</p>
        </div>
        <div class="col-12 col-md-auto">
            <a href="?module=identitas&act=editidentitas&id=<?= $id_identitas ?>" class="btn btn-danger w-100 w-md-auto d-inline-flex align-items-center justify-content-center gap-2 py-2 px-4 fw-semibold shadow-sm" style="border-radius: 10px;">
                <i class="fa fa-edit"></i> <span>Edit Identitas</span>
            </a>
        </div>
    </div>

    <div class="row g-4">
        <!-- Main Detail Card -->
        <div class="col-12 col-xl-8">
            <div class="card dark-card shadow-lg h-100">
                <div class="card-body p-3 p-md-4">
                    <h5 class="fw-bold mb-4 pb-3 border-bottom border-secondary border-opacity-25 d-flex align-items-center text-white fs-6">
                        <span class="icon-box me-2 text-danger"><i class="fa fa-globe"></i></span> Detail Informasi Situs
                    </h5>

                    <div class="table-responsive">
                        <table class="table table-borderless align-middle mb-0 text-white text-break">
                            <tbody>
                                <tr>
                                    <td style="width: 150px;" class="text-subtle fw-medium py-2 ps-0"><i class="fa fa-user me-2 text-danger"></i> Nama Pemilik</td>
                                    <td class="py-2 pe-0">: <?= $nama_pemilik ?></td>
                                </tr>
                                <tr>
                                    <td class="text-subtle fw-medium py-2 ps-0"><i class="fa fa-heading me-2 text-info"></i> Judul Website</td>
                                    <td class="py-2 pe-0">: <?= $judul_website ?></td>
                                </tr>
                                <tr>
                                    <td class="text-subtle fw-medium py-2 ps-0"><i class="fa fa-link me-2 text-primary"></i> Alamat Website</td>
                                    <td class="py-2 pe-0">: <a href="<?= $alamat_website ?>" target="_blank" class="text-info text-decoration-none"><?= $alamat_website ?></a></td>
                                </tr>
                                <tr>
                                    <td class="text-subtle fw-medium py-2 ps-0 align-top"><i class="fa fa-file-alt me-2 text-warning"></i> Meta Deskripsi</td>
                                    <td class="py-2 pe-0">: <?= $meta_deskripsi ?></td>
                                </tr>
                                <tr>
                                    <td class="text-subtle fw-medium py-2 ps-0 align-top"><i class="fa fa-tags me-2 text-success"></i> Meta Keyword</td>
                                    <td class="py-2 pe-0">: <?= $meta_keyword ?></td>
                                </tr>
                                <tr>
                                    <td class="text-subtle fw-medium py-2 ps-0 align-top"><i class="fab fa-google me-2 text-danger"></i> Google Tag</td>
                                    <td class="py-2 pe-0">: <span class="badge bg-secondary bg-opacity-25 text-subtle font-monospace fw-normal">Sembunyi (Lihat di Mode Edit)</span></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Sidebar Logo Card -->
        <div class="col-12 col-xl-4">
            <div class="card dark-card shadow-lg h-100">
                <div class="card-body p-3 p-md-4 text-center d-flex flex-column justify-content-center">
                    <h5 class="fw-bold mb-4 pb-3 border-bottom border-secondary border-opacity-25 d-flex align-items-center text-white fs-6 text-start">
                        <span class="icon-box me-2 text-danger"><i class="fa fa-image"></i></span> Logo Website
                    </h5>

                    <div class="my-auto py-3">
                        <?php if ($favicon && file_exists($favicon)): ?>
                            <div class="p-4 rounded-3 border border-secondary border-opacity-25 d-inline-block mw-100 mb-3" style="background-color: #0f172a;">
                                <img src="<?= $favicon ?>" alt="Logo Website" class="img-fluid" style="max-height: 120px; object-fit: contain;">
                            </div>
                            <span class="d-block text-subtle small">Logo/Icon Aktif saat ini</span>
                        <?php else: ?>
                            <div class="p-4 rounded-3 border border-secondary border-opacity-25 text-subtle small" style="background-color: #0f172a;">
                                <i class="fa fa-image fa-2x mb-2 d-block"></i> Belum ada logo terpasang
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM EDIT IDENTITAS WEB
    // ==========================================
    case "editidentitas":
        $id    = mysqli_real_escape_string($konek, $_GET['id'] ?? '');
        $query = "SELECT * FROM identitas WHERE id_identitas='$id'";
        $hasil = mysqli_query($konek, $query);
        $r     = mysqli_fetch_array($hasil);

        if (!$r) {
            echo "<script>alert('Data identitas tidak ditemukan!'); window.location='?module=identitas';</script>";
            exit();
        }

        $favicon = !empty($r['favicon']) ? "../foto_icon/" . htmlspecialchars($r['favicon']) : '';
?>
    <style>
        .dark-card {
            background-color: #1e293b !important;
            border: 1px solid rgba(255, 255, 255, 0.08) !important;
            border-radius: 16px !important;
        }
        .dark-input {
            background-color: #0f172a !important;
            border: 1px solid #334155 !important;
            color: #f8fafc !important;
            border-radius: 10px !important;
        }
        .dark-input:focus {
            background-color: #0f172a !important;
            border-color: #f43f5e !important;
            color: #ffffff !important;
            box-shadow: 0 0 0 0.25rem rgba(244, 63, 94, 0.15) !important;
        }
        .text-subtle { color: #94a3b8 !important; }
    </style>

    <!-- Header Section -->
    <div class="row align-items-center gy-3 mb-4">
        <div class="col-12 col-md">
            <h4 class="fw-bold mb-1 text-white">Edit Identitas Website</h4>
            <p class="text-subtle small mb-0">Perbarui judul situs, meta tag SEO, dan logo pendukung</p>
        </div>
        <div class="col-12 col-md-auto">
            <button onclick="self.history.back()" class="btn btn-outline-light w-100 w-md-auto d-inline-flex align-items-center justify-content-center gap-2 py-2 px-3" style="border-radius: 10px; border-color: #334155;">
                <i class="fa fa-arrow-left"></i> <span>Kembali</span>
            </button>
        </div>
    </div>

    <div class="row">
        <div class="col-12 col-xl-10">
            <div class="card dark-card shadow-lg">
                <div class="card-body p-3 p-md-4">
                    <form method="POST" enctype="multipart/form-data" action="<?= $aksi ?>?module=identitas">
                        <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_identitas']) ?>">

                        <!-- Section 1: Profil Utama -->
                        <h6 class="fw-bold mb-4 text-danger d-flex align-items-center">
                            <i class="fa fa-globe me-2"></i> Profil & Informasi Umum
                        </h6>

                        <!-- Nama Pemilik -->
                        <div class="row mb-3">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Nama Pemilik</label>
                            <div class="col-12 col-md-9">
                                <input type="text" name="nama_pemilik" class="form-control dark-input" value="<?= htmlspecialchars($r['nama_pemilik'] ?? '') ?>" placeholder="Nama pemilik / perusahaan">
                            </div>
                        </div>

                        <!-- Judul Website -->
                        <div class="row mb-3">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Judul Website</label>
                            <div class="col-12 col-md-9">
                                <input type="text" name="judul_website" class="form-control dark-input" value="<?= htmlspecialchars($r['judul_website'] ?? '') ?>" placeholder="Judul utama website">
                            </div>
                        </div>

                        <!-- Alamat Website -->
                        <div class="row mb-4">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Alamat Website (URL)</label>
                            <div class="col-12 col-md-9">
                                <input type="text" name="alamat_website" class="form-control dark-input" value="<?= htmlspecialchars($r['alamat_website'] ?? '') ?>" placeholder="https://domainanda.com">
                            </div>
                        </div>

                        <!-- Section 2: SEO & Script -->
                        <h6 class="fw-bold mb-4 pt-3 border-top border-secondary border-opacity-25 text-danger d-flex align-items-center">
                            <i class="fa fa-search me-2"></i> Pengaturan SEO & Embed Script
                        </h6>

                        <!-- Meta Deskripsi -->
                        <div class="row mb-3">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Meta Deskripsi</label>
                            <div class="col-12 col-md-9">
                                <textarea name="meta_deskripsi" rows="2" class="form-control dark-input" placeholder="Ringkasan singkat isi website untuk mesin pencari..."><?= htmlspecialchars($r['meta_deskripsi'] ?? '') ?></textarea>
                            </div>
                        </div>

                        <!-- Meta Keyword -->
                        <div class="row mb-3">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Meta Keyword</label>
                            <div class="col-12 col-md-9">
                                <input type="text" name="meta_keyword" class="form-control dark-input" value="<?= htmlspecialchars($r['meta_keyword'] ?? '') ?>" placeholder="Kata kunci dipisahkan koma (contoh: toko, baju, murah)">
                            </div>
                        </div>

                        <!-- Google Tag / Code -->
                        <div class="row mb-4">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Google Tag / Embed Script</label>
                            <div class="col-12 col-md-9">
                                <textarea name="googleplus" rows="3" class="form-control dark-input font-monospace" style="font-size: 0.85rem;" placeholder="Sematkan Google Analytics, Meta Pixel, atau tag skrip lainnya..."><?= htmlspecialchars($r['googleplus'] ?? '') ?></textarea>
                            </div>
                        </div>

                        <!-- Section 3: Branding & Image -->
                        <h6 class="fw-bold mb-4 pt-3 border-top border-secondary border-opacity-25 text-danger d-flex align-items-center">
                            <i class="fa fa-image me-2"></i> Branding Logo
                        </h6>

                        <!-- Preview & File Input Logo -->
                        <div class="row mb-4">
                            <label class="col-12 col-md-3 col-form-label fw-medium text-subtle">Logo Website</label>
                            <div class="col-12 col-md-9">
                                <?php if ($favicon && file_exists($favicon)): ?>
                                    <div class="mb-3 p-3 rounded-3 border border-secondary border-opacity-25 d-inline-block mw-100" style="background-color: #0f172a;">
                                        <img src="<?= $favicon ?>" alt="Logo Saat Ini" class="img-fluid" style="max-height: 80px; object-fit: contain;">
                                    </div>
                                <?php endif; ?>
                                <input type="file" name="foto" class="form-control dark-input">
                                <span class="text-subtle d-block mt-1" style="font-size: 0.8rem;">
                                    Biarkan kosong jika tidak ingin mengubah logo (Format: PNG, JPG, WEBP, ICO).
                                </span>
                            </div>
                        </div>

                        <!-- Submit Buttons -->
                        <div class="row pt-3 border-top border-secondary border-opacity-25">
                            <div class="col-12 col-md-9 offset-md-3">
                                <div class="d-grid d-md-flex gap-2">
                                    <button type="submit" class="btn btn-danger px-4 py-2 fw-semibold d-inline-flex align-items-center justify-content-center gap-2" style="border-radius: 10px;">
                                        <i class="fa fa-save"></i> Perbarui Data
                                    </button>
                                    <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4 py-2 text-white" style="border-radius: 10px; border-color: #334155;">
                                        Batal
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php
    break;
}
?>