<?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();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && verifyCSRFToken($_POST['csrf_token'] ?? '')) {
    $tanggal = trim($_POST['tanggal'] ?? '');
    $waktu_mulai = trim($_POST['waktu_mulai'] ?? '');
    $waktu_selesai = trim($_POST['waktu_selesai'] ?? '');
    $lokasi = trim($_POST['lokasi'] ?? '');
    $kegiatan = trim($_POST['kegiatan'] ?? '');
    $keterangan = trim($_POST['keterangan'] ?? '');
    $created_by = $_SESSION['user_id'];

    try {
        if (empty($tanggal) || empty($waktu_mulai) || empty($waktu_selesai) || empty($lokasi) || empty($kegiatan)) {
            setFlash('error', 'Semua field wajib kecuali keterangan.');
        } else {
            $stmt = $db->prepare("INSERT INTO jadwal_posyandu (tanggal, waktu_mulai, waktu_selesai, lokasi, kegiatan, keterangan, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([$tanggal, $waktu_mulai, $waktu_selesai, $lokasi, $kegiatan, $keterangan, $created_by]);
            setFlash('success', 'Jadwal berhasil ditambahkan.');
            header('Location: ' . BASE_URL . '/admin/jadwal/');
            exit;
        }
    } catch (PDOException $e) {
        setFlash('error', 'Error: ' . $e->getMessage());
    }
}

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 Jadwal Baru</h1>
        <p class="text-gray-500 text-sm mt-1">Buat agenda kegiatan posyandu baru.</p>
    </div>
    <a href="./" class="inline-flex items-center gap-2 px-4 py-2 bg-white border border-gray-200 rounded-xl text-sm font-medium text-gray-600 hover:bg-gray-50 hover:text-gray-900 transition-colors shadow-sm">
        <i class="fas fa-arrow-left"></i> Kembali
    </a>
</div>

<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50/50 flex items-center gap-2">
        <i class="fas fa-calendar-plus text-primary"></i>
        <h3 class="font-bold text-gray-800">Form Tambah Jadwal</h3>
    </div>
    <div class="p-6">
        <form action="tambah.php" method="POST" class="space-y-5">
            <?= csrfField() ?>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Nama Kegiatan <span class="text-red-500">*</span></label>
                <input type="text" name="kegiatan" 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" required placeholder="Contoh: Posyandu Balita & Ibu Hamil">
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Tanggal Pelaksanaan <span class="text-red-500">*</span></label>
                <input type="date" name="tanggal" 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" required>
            </div>
            
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Waktu Mulai <span class="text-red-500">*</span></label>
                    <input type="time" name="waktu_mulai" 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" required>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Waktu Selesai <span class="text-red-500">*</span></label>
                    <input type="time" name="waktu_selesai" 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" required>
                </div>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Lokasi <span class="text-red-500">*</span></label>
                <input type="text" name="lokasi" 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" value="Balai Posyandu Tunas Bangsa" required>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Keterangan (Opsional)</label>
                <textarea name="keterangan" rows="3" 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" placeholder="Catatan tambahan..."></textarea>
            </div>
            
            <div class="pt-4 flex items-center justify-end gap-3 border-t border-gray-100 mt-6">
                <a href="./" class="px-6 py-2.5 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-xl transition-colors">Batal</a>
                <button type="submit" class="px-6 py-2.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 gap-2">
                    <i class="fas fa-save"></i> Simpan Jadwal
                </button>
            </div>
        </form>
    </div>
</div>

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