<?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 = (int)($_GET['id'] ?? 0);
if (!$id) {
    header('Location: ' . BASE_URL . '/admin/ibu_hamil/');
    exit;
}

$db = getDB();
$stmt = $db->prepare("SELECT * FROM kunjungan_posyandu WHERE id = ?");
$stmt->execute([$id]);
$k = $stmt->fetch();
if (!$k) {
    setFlash('error', 'Data kunjungan tidak ditemukan.');
    header('Location: ' . BASE_URL . '/admin/ibu_hamil/');
    exit;
}

$stmt = $db->prepare("SELECT * FROM ibu_hamil WHERE id = ?");
$stmt->execute([$k['ibu_hamil_id']]);
$ibu = $stmt->fetch();

$is_edit = true;
$back = $_GET['back'] ?? 'detail';
$back_url = ($back === 'kunjungan')
    ? BASE_URL . '/admin/kunjungan/?ibu_hamil_id=' . $k['ibu_hamil_id']
    : BASE_URL . '/admin/ibu_hamil/detail.php?id=' . $ibu['id'];
require_once __DIR__ . '/../../../includes/header_admin.php';
?>
<div class="flex items-center gap-4 mb-6">
    <a href="<?= $back_url ?>" class="w-10 h-10 bg-white border border-gray-200 rounded-xl flex items-center justify-center text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors shadow-sm">
        <i class="fas fa-arrow-left"></i>
    </a>
    <div>
        <h1 class="text-2xl font-bold text-gray-800">Edit Kunjungan Posyandu</h1>
        <p class="text-gray-500 text-sm mt-1">Perbarui data rekam medis kunjungan untuk <strong><?= escape($ibu['nama_lengkap']) ?></strong>.</p>
    </div>
</div>

<?php require __DIR__ . '/_form.php'; ?>

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