<?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';

$db = getDB();
$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">Tambah Artikel Edukasi</h1>
        <p class="text-gray-500 text-sm mt-1">Tulis dan bagikan informasi seputar kesehatan.</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="tambah">
    
    <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" 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 placeholder="Masukkan judul yang menarik">
                    </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-[300px]" required placeholder="Tulis artikel di sini (Gunakan enter untuk paragraf baru)"></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" placeholder="Akan ditampilkan pada daftar artikel (cuplikan)"></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>
                            <option value="" disabled selected>Pilih Kategori...</option>
                            <?php foreach($kategori as $k): ?>
                                <option value="<?= $k['id'] ?>"><?= 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">Published (Tampil untuk Umum)</option>
                                <option value="0">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>
                
                <div class="space-y-4">
                    <label class="block text-sm font-medium text-gray-700">Gambar Cover</label>
                    <div class="mt-1 flex justify-center px-6 pt-5 pb-6 border-2 border-gray-300 border-dashed rounded-xl bg-gray-50 hover:bg-gray-100 transition-colors relative">
                        <div class="space-y-2 text-center relative z-10">
                            <i class="fas fa-cloud-upload-alt text-3xl text-gray-400"></i>
                            <div class="flex text-sm text-gray-600 justify-center">
                                <label for="file-upload" class="relative cursor-pointer bg-white rounded-md font-medium text-primary hover:text-primary-dark focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-primary px-2">
                                    <span>Pilih File</span>
                                    <input id="file-upload" name="gambar" type="file" class="sr-only" accept="image/jpeg, image/png, image/webp">
                                </label>
                            </div>
                            <p class="text-xs text-gray-500">PNG, JPG, WEBP up to 2MB</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <button type="submit" class="w-full py-3.5 bg-primary hover:bg-primary-dark text-white font-bold rounded-xl shadow-lg shadow-primary/30 transition-all transform hover:-translate-y-0.5 focus:ring-2 focus:ring-offset-2 focus:ring-primary flex items-center justify-center gap-2">
                <i class="fas fa-paper-plane"></i> Publish Artikel
            </button>
        </div>
    </div>
</form>

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