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

$ibu_hamil_id = (int)($_GET['ibu_hamil_id'] ?? 0);
$ibu = null;

if ($ibu_hamil_id) {
    $stmt = $db->prepare("SELECT * FROM ibu_hamil WHERE id = ?");
    $stmt->execute([$ibu_hamil_id]);
    $ibu = $stmt->fetch();
    if (!$ibu) {
        setFlash('error', 'Data ibu hamil tidak ditemukan.');
        $ibu_hamil_id = 0;
    }
}

$ibu_list = $db->query("SELECT id, nama_lengkap, nik FROM ibu_hamil ORDER BY nama_lengkap ASC")->fetchAll();

require_once '../../includes/header_admin.php';
?>

<div class="flex items-center gap-4 mb-6">
    <a href="./" 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">Tambah Kunjungan Posyandu</h1>
        <p class="text-gray-500 text-sm mt-1">
            <?= $ibu ? 'Isi data rekam medis untuk <strong>' . escape($ibu['nama_lengkap']) . '</strong>.' : 'Pilih ibu hamil terlebih dahulu.' ?>
        </p>
    </div>
</div>

<?php if (!$ibu): ?>
<!-- Step 1: Pilih Ibu Hamil -->
<div class="max-w-lg">
    <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-user-circle text-primary"></i>
            <h3 class="font-bold text-gray-800">Pilih Ibu Hamil</h3>
        </div>
        <div class="p-6">
            <form method="GET" action="tambah.php" class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Nama / NIK Ibu Hamil <span class="text-red-500">*</span></label>
                    <select name="ibu_hamil_id" required
                            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">
                        <option value="">— Pilih ibu hamil —</option>
                        <?php foreach ($ibu_list as $ib): ?>
                            <option value="<?= $ib['id'] ?>"><?= escape($ib['nama_lengkap']) ?> &mdash; <?= escape($ib['nik']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <button type="submit"
                        class="w-full px-6 py-2.5 bg-primary hover:bg-primary-dark text-white font-bold rounded-xl shadow-lg shadow-primary/30 transition-all flex items-center justify-center gap-2">
                    Lanjut Isi Data <i class="fas fa-arrow-right"></i>
                </button>
            </form>
        </div>
    </div>
</div>

<?php else: ?>
<!-- Step 2: Form Kunjungan -->

<!-- Info ibu hamil terpilih -->
<div class="flex items-center gap-3 bg-primary/5 border border-primary/20 rounded-xl px-4 py-3 mb-6 w-fit">
    <div class="w-8 h-8 rounded-full bg-primary/10 text-primary flex items-center justify-center text-sm shrink-0">
        <i class="fas fa-user-circle"></i>
    </div>
    <div class="min-w-0">
        <p class="font-bold text-gray-800 text-sm"><?= escape($ibu['nama_lengkap']) ?></p>
        <p class="text-xs text-gray-500">NIK: <?= escape($ibu['nik']) ?></p>
    </div>
    <a href="tambah.php" class="ml-3 text-xs text-primary hover:underline shrink-0">Ganti</a>
</div>

<form action="<?= BASE_URL ?>/admin/ibu_hamil/kunjungan/simpan.php" method="POST"
      class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden mb-8">
    <?= csrfField() ?>
    <input type="hidden" name="action" value="tambah">
    <input type="hidden" name="ibu_hamil_id" value="<?= $ibu_hamil_id ?>">
    <input type="hidden" name="back" value="kunjungan">

    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50/50 flex items-center gap-2">
        <i class="fas fa-notes-medical text-primary"></i>
        <h3 class="font-bold text-gray-800">Data Rekam Medis Kunjungan</h3>
    </div>

    <div class="p-6 space-y-5">
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Tanggal Kunjungan <span class="text-red-500">*</span></label>
                <input type="date" name="tanggal_kunjungan" value="<?= date('Y-m-d') ?>" max="<?= date('Y-m-d') ?>"
                       class="w-full px-4 py-2 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">Usia Kehamilan (Minggu)</label>
                <input type="number" name="usia_kehamilan_minggu" min="0" max="50"
                       class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"
                       placeholder="cth: 24">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Tekanan Darah (mmHg)</label>
                <input type="text" name="tekanan_darah" maxlength="15"
                       class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"
                       placeholder="cth: 120/80">
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Berat Badan (kg)</label>
                <input type="number" step="0.1" min="0" name="berat_badan"
                       class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"
                       placeholder="cth: 58.5">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Tinggi Fundus (cm)</label>
                <input type="number" step="0.1" min="0" name="tinggi_fundus"
                       class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"
                       placeholder="cth: 24.0">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">DJJ (/menit)</label>
                <input type="number" min="0" max="300" name="denyut_jantung_janin"
                       class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"
                       placeholder="cth: 140">
            </div>
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">Letak Janin</label>
            <select name="letak_janin" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm bg-white">
                <option value="">— Pilih —</option>
                <?php foreach (['Kepala', 'Sungsang', 'Lintang', 'Belum Teraba'] as $o): ?>
                    <option value="<?= $o ?>"><?= $o ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">Catatan</label>
            <textarea name="catatan" rows="3"
                      class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"
                      placeholder="Keluhan, anjuran, dll."></textarea>
        </div>
    </div>

    <div class="bg-gray-50 px-6 py-4 border-t border-gray-100 flex items-center justify-end gap-3">
        <a href="./" class="px-5 py-2 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 flex items-center gap-2">
            <i class="fas fa-save"></i> Simpan Kunjungan
        </button>
    </div>
</form>

<?php endif; ?>

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