<?php
$page_title = "Pengaturan Halaman Depan";
include 'layout/header.php';
include 'layout/sidebar.php';
require_once '../config.php';

if ($_SESSION['level'] != 'administrator') {
    echo "<script>alert('Akses Ditolak!'); window.location.href='index.php';</script>";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 1. Update text variables
    $text_keys = ['app_title', 'app_desc', 'app_video_url', 'app_title_align', 'app_desc_align', 'tc_title_align', 'tc_desc_align', 'app_title_font', 'app_title_size', 'app_about_title', 'app_slider_duration'];
    foreach ($text_keys as $key) {
        if (isset($_POST[$key])) {
            $stmt = $pdo->prepare("UPDATE tbl_pengaturan SET meta_value = ? WHERE meta_key = ?");
            $stmt->execute([$_POST[$key], $key]);
        }
    }

    echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
            Swal.fire({
                icon: 'success',
                title: 'Berhasil!',
                text: 'Pengaturan Halaman Depan Berhasil Disimpan!',
                timer: 2000,
                showConfirmButton: false,
                position: 'center'
            }).then(function() {
                window.location.href = 'pengaturan_frontend.php';
            });
        });
    </script>";
}

// Function to generate select option quickly
function alignSelect($name, $selected) {
    $options = [
        'text-start' => 'Kiri (Left)',
        'text-center' => 'Tengah (Center)',
        'text-end' => 'Kanan (Right)',
        'text-justify' => 'Rata Penuh (Justify)'
    ];
    $html = "<select name='$name' class='form-control'>";
    foreach($options as $val => $label) {
        if ($val == 'text-justify' && $name == 'app_title_align') continue; // Title shouldn't be justified
        if ($val == 'text-justify' && $name == 'tc_title_align') continue; // Title shouldn't be justified
        $sel = ($val == $selected) ? 'selected' : '';
        $html .= "<option value='$val' $sel>$label</option>";
    }
    $html .= "</select>";
    return $html;
}

$logo_current = getSetting($pdo, 'app_logo');
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <h1 class="m-0">Pengaturan Halaman Depan (Frontend)</h1>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <form action="pengaturan_frontend.php" method="POST" enctype="multipart/form-data">
                <div class="row">
                    <!-- Pengaturan Logo & Judul -->
                    <div class="col-md-6">
                        <div class="card card-primary">
                            <div class="card-header"><h3 class="card-title">Judul Utama</h3></div>
                            <div class="card-body">
                                <p class="text-muted small">Logo kini diatur secara manual. Tempatkan gambar pada direktori <code>logo/logo.png</code>.</p>
                                <hr>
                                <div class="form-group">
                                    <label>Judul Aplikasi</label>
                                    <input type="text" name="app_title" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'app_title')) ?>">
                                </div>
                                <div class="form-group">
                                    <label>Perataan (Alignment) Judul Aplikasi</label>
                                    <?= alignSelect('app_title_align', getSetting($pdo, 'app_title_align')) ?>
                                </div>
                                <div class="row">
                                    <div class="col-6 form-group">
                                        <label>Ketebalan Font Judul</label>
                                        <select name="app_title_font" class="form-control">
                                            <option value="fw-bold" <?= getSetting($pdo, 'app_title_font') == 'fw-bold' ? 'selected' : '' ?>>Tebal (Bold)</option>
                                            <option value="fw-normal" <?= getSetting($pdo, 'app_title_font') == 'fw-normal' ? 'selected' : '' ?>>Biasa (Normal)</option>
                                        </select>
                                    </div>
                                    <div class="col-6 form-group">
                                        <label>Ukuran Font Judul</label>
                                        <select name="app_title_size" class="form-control">
                                            <option value="h1" <?= getSetting($pdo, 'app_title_size') == 'h1' ? 'selected' : '' ?>>Sangat Besar (H1)</option>
                                            <option value="h2" <?= getSetting($pdo, 'app_title_size') == 'h2' ? 'selected' : '' ?>>Besar (H2)</option>
                                            <option value="h3" <?= getSetting($pdo, 'app_title_size') == 'h3' ? 'selected' : '' ?>>Sedang (H3)</option>
                                            <option value="h4" <?= getSetting($pdo, 'app_title_size') == 'h4' ? 'selected' : '' ?>>Kecil (H4)</option>
                                            <option value="h5" <?= getSetting($pdo, 'app_title_size') == 'h5' ? 'selected' : '' ?>>Sangat Kecil (H5)</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group mt-4">
                                    <label>URL Video Panduan (YouTube Embed URL)</label>
                                    <input type="text" name="app_video_url" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'app_video_url')) ?>" placeholder="https://www.youtube.com/embed/...">
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Pengaturan Teks Lainnya -->
                    <div class="col-md-6">
                        <div class="card card-info">
                            <div class="card-header"><h3 class="card-title">Pengaturan Deskripsi & Tata Cara</h3></div>
                            <div class="card-body">
                                <div class="form-group">
                                    <label>Judul Bagian "Tentang Layanan"</label>
                                    <input type="text" name="app_about_title" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'app_about_title') ?: 'Tentang Layanan Kami') ?>">
                                </div>
                                <div class="form-group">
                                    <label>Deskripsi Singkat Halaman Depan</label>
                                    <textarea name="app_desc" class="form-control" rows="4"><?= htmlspecialchars(getSetting($pdo, 'app_desc')) ?></textarea>
                                </div>
                                <div class="form-group">
                                    <label>Durasi Slide Depan (detik)</label>
                                    <input type="number" name="app_slider_duration" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'app_slider_duration') ?: '10') ?>" min="3" max="60">
                                </div>
                                <div class="form-group">
                                    <label>Perataan (Alignment) Deskripsi Halaman Depan</label>
                                    <?= alignSelect('app_desc_align', getSetting($pdo, 'app_desc_align')) ?>
                                </div>
                                <hr>
                                <h5 class="fw-bold mb-3">Tampilan Tata Cara</h5>
                                <div class="row">
                                    <div class="col-6 form-group">
                                        <label>Perataan Judul Tata Cara</label>
                                        <?= alignSelect('tc_title_align', getSetting($pdo, 'tc_title_align')) ?>
                                    </div>
                                    <div class="col-6 form-group">
                                        <label>Perataan Teks (Deskripsi) Tata Cara</label>
                                        <?= alignSelect('tc_desc_align', getSetting($pdo, 'tc_desc_align')) ?>
                                    </div>
                                </div>
                                <p class="small text-muted mt-2"><i class="fas fa-info-circle"></i> Catatan: Pengaturan isi dari Tata Cara dapat diubah melalui menu "Tata Cara Pengajuan" di sidebar.</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="mb-4 text-center">
                    <button type="submit" class="btn btn-primary btn-lg w-50"><i class="fas fa-save"></i> Simpan Pengaturan Halaman Depan</button>
                </div>
            </form>
        </div>
    </section>
</div>

<?php include 'layout/footer.php'; ?>
