<?php
// Shared kunjungan form. Variables expected:
//   $is_edit (bool), $k (array|null), $ibu (array), $back (string)
$is_edit  = $is_edit ?? false;
$k        = $k ?? [];
$back     = $back ?? 'detail';
$back_url = isset($back_url) ? $back_url : (($back === 'kunjungan')
    ? BASE_URL . '/admin/kunjungan/?ibu_hamil_id=' . (int)$ibu['id']
    : BASE_URL . '/admin/ibu_hamil/detail.php?id=' . (int)$ibu['id']);
?>
<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="<?= $is_edit ? 'edit' : 'tambah' ?>">
    <?php if ($is_edit): ?>
        <input type="hidden" name="id" value="<?= (int)$k['id'] ?>">
    <?php endif; ?>
    <input type="hidden" name="ibu_hamil_id" value="<?= (int)$ibu['id'] ?>">
    <input type="hidden" name="back" value="<?= escape($back) ?>">

    <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"><?= $is_edit ? 'Edit Kunjungan' : 'Tambah Kunjungan' ?> &mdash; <?= escape($ibu['nama_lengkap']) ?></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="<?= escape($k['tanggal_kunjungan'] ?? 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" value="<?= escape($k['usia_kehamilan_minggu'] ?? '') ?>" 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" value="<?= escape($k['tekanan_darah'] ?? '') ?>" 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" value="<?= escape($k['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" value="<?= escape($k['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" value="<?= escape($k['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">
                <?php
                $opsi = ['', 'Kepala', 'Sungsang', 'Lintang', 'Belum Teraba'];
                $cur = $k['letak_janin'] ?? '';
                foreach ($opsi as $o):
                ?>
                    <option value="<?= escape($o) ?>" <?= $cur === $o ? 'selected' : '' ?>><?= $o === '' ? '— Pilih —' : escape($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."><?= escape($k['catatan'] ?? '') ?></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="<?= $back_url ?>" 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> <?= $is_edit ? 'Perbarui' : 'Simpan' ?>
        </button>
    </div>
</form>
