<?php
require_once __DIR__ . '/../../config/session.php';
requireAdmin();
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../functions/helper.php';
require_once __DIR__ . '/../../functions/csrf.php';

$id = $_GET['id'] ?? null;
if (!$id) {
    header('Location: ./');
    exit;
}

$db = getDB();
$stmt = $db->prepare("SELECT * FROM edukasi WHERE id = ?");
$stmt->execute([$id]);
$edu = $stmt->fetch();

if (!$edu) {
    setFlash('error', 'Artikel tidak ditemukan.');
    header('Location: ./');
    exit;
}

$kategori = $db->query("SELECT * FROM kategori_edukasi")->fetchAll();
?>
<?php require_once '../../includes/header_admin.php'; ?>

<div class="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-800">Edit Artikel Edukasi</h1>
        <p class="text-gray-500 text-sm mt-1">Perbarui konten artikel dan pengaturannya.</p>
    </div>
    <div>
        <a href="./" class="inline-flex items-center justify-center px-4 py-2 bg-white border border-gray-300 hover:bg-gray-50 text-gray-700 font-medium rounded-lg transition-colors shadow-sm focus:ring-2 focus:ring-offset-2 focus:ring-gray-200">
            <i class="fas fa-arrow-left mr-2"></i> Kembali
        </a>
    </div>
</div>

<form action="simpan" method="POST" enctype="multipart/form-data">
    <?= csrfField() ?>
    <input type="hidden" name="action" value="edit">
    <input type="hidden" name="id" value="<?= $edu['id'] ?>">
    
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Kolom Kiri: Konten Utama -->
        <div class="lg:col-span-2 space-y-6">
            <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-6 md:p-8">
                <div class="space-y-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Judul Artikel <span class="text-red-500">*</span></label>
                        <input type="text" name="judul" value="<?= escape($edu['judul']) ?>" class="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-base font-medium" required>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Isi Konten <span class="text-red-500">*</span></label>
                        <textarea name="isi_konten" class="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm min-h-[400px] leading-relaxed" required><?= escape($edu['isi_konten']) ?></textarea>
                    </div>
                    
                    <div>
                        <div class="flex justify-between mb-2">
                            <label class="block text-sm font-medium text-gray-700">Ringkasan Singkat</label>
                            <span class="text-xs text-gray-400">Maksimal 500 karakter</span>
                        </div>
                        <textarea name="ringkasan" rows="3" maxlength="500" class="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"><?= escape($edu['ringkasan']) ?></textarea>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Kolom Kanan: Pengaturan -->
        <div class="lg:col-span-1 space-y-6">
            <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-6">
                <h3 class="font-bold text-gray-800 mb-4 pb-2 border-b border-gray-100"><i class="fas fa-cogs text-gray-400 mr-2"></i>Pengaturan Publikasi</h3>
                
                <div class="space-y-5">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Kategori <span class="text-red-500">*</span></label>
                        <select name="kategori_id" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm bg-white" required>
                            <?php foreach($kategori as $k): ?>
                                <option value="<?= $k['id'] ?>" <?= $k['id'] == $edu['kategori_id'] ? 'selected' : '' ?>><?= escape($k['nama']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Status Publikasi</label>
                        <div class="relative">
                            <select name="is_published" class="w-full px-4 py-2.5 pl-10 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm bg-white appearance-none">
                                <option value="1" <?= $edu['is_published'] ? 'selected' : '' ?>>Published (Tampil untuk Umum)</option>
                                <option value="0" <?= !$edu['is_published'] ? 'selected' : '' ?>>Draft (Sembunyikan)</option>
                            </select>
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fas fa-globe-asia text-gray-400"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-6">
                <h3 class="font-bold text-gray-800 mb-4 pb-2 border-b border-gray-100"><i class="fas fa-image text-gray-400 mr-2"></i>Media Utama</h3>
                
                <?php if($edu['gambar']): ?>
                    <div class="mb-4 rounded-xl overflow-hidden border border-gray-200">
                        <img src="<?= BASE_URL ?>/assets/uploads/edukasi/<?= escape($edu['gambar']) ?>" class="w-full h-auto object-cover max-h-48">
                    </div>
                <?php endif; ?>

                <div class="space-y-2">
                    <label class="block text-sm font-medium text-gray-700">Ganti Gambar Baru (Opsional)</label>
                    <input type="file" name="gambar" class="w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-primary/10 file:text-primary hover:file:bg-primary/20 transition-colors" accept="image/jpeg, image/png, image/webp">
                </div>
            </div>
            
            <button type="submit" class="w-full py-3.5 bg-yellow-500 hover:bg-yellow-600 text-white font-bold rounded-xl shadow-lg shadow-yellow-500/30 transition-all transform hover:-translate-y-0.5 focus:ring-2 focus:ring-offset-2 focus:ring-yellow-500 flex items-center justify-center gap-2">
                <i class="fas fa-save"></i> Perbarui Artikel
            </button>
        </div>
    </div>
</form>

<?php require_once '../../includes/footer_admin.php'; ?>
