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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$token = $_GET['token'] ?? '';

if ($id <= 0 || !verifyCSRFToken($token)) {
    setFlash('error', 'Token tidak valid atau permintaan tidak sah.');
    header('Location: ' . BASE_URL . '/admin/jadwal/');
    exit;
}

try {
    $stmt = $db->prepare("DELETE FROM jadwal_posyandu WHERE id = ?");
    $stmt->execute([$id]);
    if ($stmt->rowCount() > 0) {
        setFlash('success', 'Jadwal berhasil dihapus.');
    } else {
        setFlash('error', 'Jadwal tidak ditemukan.');
    }
} catch (PDOException $e) {
    setFlash('error', 'Gagal menghapus jadwal: ' . $e->getMessage());
}

header('Location: ' . BASE_URL . '/admin/jadwal/');
exit;
