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

$db = getDB();
$stmt = $db->prepare("SELECT * FROM ibu_hamil WHERE id = ?");
$stmt->execute([$id]);
$ih = $stmt->fetch();

if (!$ih) {
    setFlash('error', 'Data tidak ditemukan.');
    header('Location: ./');
    exit;
}

$stmt = $db->query("SELECT id, nama_lengkap FROM users WHERE role = 'admin' AND is_active=1");
$bidan_list = $stmt->fetchAll();

// Hitung umur
$dob = new DateTime($ih['tanggal_lahir']);
$today = new DateTime();
$umur = $today->diff($dob)->y;
?>
<?php 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">Edit Data Ibu Hamil</h1>
        <p class="text-gray-500 text-sm mt-1">Perbarui data kehamilan dan identitas ibu hamil.</p>
    </div>
    <div>
        <a href="./" class="inline-flex items-center justify-center px-4 py-2 bg-white border border-gray-300 hover:bg-gray-50 text-gray-700 font-medium rounded-lg transition-colors shadow-sm focus:ring-2 focus:ring-offset-2 focus:ring-primary">
            <i class="fas fa-arrow-left mr-2"></i> Kembali
        </a>
    </div>
</div>

<form action="simpan" method="POST" class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden mb-8">
    <?= csrfField() ?>
    <input type="hidden" name="action" value="edit">
    <input type="hidden" name="id" value="<?= $ih['id'] ?>">
    
    <div class="grid grid-cols-1 md:grid-cols-2 gap-0 divide-y md:divide-y-0 md:divide-x divide-gray-100">
        <!-- Kolom Kiri: Identitas -->
        <div class="p-6 md:p-8">
            <div class="flex items-center gap-3 mb-6 pb-4 border-b border-gray-100">
                <div class="w-10 h-10 rounded-lg bg-primary/10 text-primary flex items-center justify-center text-lg">
                    <i class="fas fa-address-card"></i>
                </div>
                <h3 class="text-lg font-bold text-gray-800">Data Identitas Pribadi</h3>
            </div>
            
            <div class="space-y-5">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">NIK <span class="text-red-500">*</span></label>
                    <input type="text" name="nik" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" maxlength="16" pattern="\d{16}" value="<?= escape($ih['nik']) ?>" required>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap <span class="text-red-500">*</span></label>
                    <input type="text" name="nama_lengkap" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" maxlength="100" value="<?= escape($ih['nama_lengkap']) ?>" required>
                </div>
                
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Tanggal Lahir <span class="text-red-500">*</span></label>
                        <input type="date" name="tanggal_lahir" id="tanggal_lahir" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($ih['tanggal_lahir']) ?>" required max="<?= date('Y-m-d') ?>">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Umur</label>
                        <input type="text" id="umur" class="w-full px-4 py-2 border border-gray-200 bg-gray-50 rounded-xl text-sm font-medium text-gray-600" value="<?= $umur ?> Tahun" readonly>
                    </div>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Alamat Lengkap <span class="text-red-500">*</span></label>
                    <textarea name="alamat" rows="2" 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><?= escape($ih['alamat']) ?></textarea>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">No. HP / WA Ibu <span class="text-red-500">*</span></label>
                    <input type="tel" name="no_hp" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($ih['no_hp']) ?>" required>
                </div>
            </div>
        </div>
        
        <!-- Kolom Kanan: Kehamilan -->
        <div class="p-6 md:p-8 bg-gray-50/30">
            <div class="flex items-center gap-3 mb-6 pb-4 border-b border-gray-100">
                <div class="w-10 h-10 rounded-lg bg-accent/10 text-accent flex items-center justify-center text-lg">
                    <i class="fas fa-stethoscope"></i>
                </div>
                <h3 class="text-lg font-bold text-gray-800">Data Medis Kehamilan</h3>
            </div>
            
            <div class="space-y-5">
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Nama Suami <span class="text-red-500">*</span></label>
                        <input type="text" name="nama_suami" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($ih['nama_suami']) ?>" required>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">No. HP Suami</label>
                        <input type="tel" name="no_hp_suami" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($ih['no_hp_suami']) ?>">
                    </div>
                </div>
                
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Golongan Darah <span class="text-red-500">*</span></label>
                        <select name="golongan_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 bg-white" required>
                            <option value="Tidak Tahu" <?= $ih['golongan_darah']=='Tidak Tahu'?'selected':'' ?>>Tidak Tahu</option>
                            <option value="A" <?= $ih['golongan_darah']=='A'?'selected':'' ?>>A</option>
                            <option value="B" <?= $ih['golongan_darah']=='B'?'selected':'' ?>>B</option>
                            <option value="AB" <?= $ih['golongan_darah']=='AB'?'selected':'' ?>>AB</option>
                            <option value="O" <?= $ih['golongan_darah']=='O'?'selected':'' ?>>O</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Bidan PJ <span class="text-red-500">*</span></label>
                        <select name="bidan_id" 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" required>
                            <?php foreach($bidan_list as $bidan): ?>
                                <option value="<?= $bidan['id'] ?>" <?= $ih['bidan_id']==$bidan['id']?'selected':'' ?>><?= escape($bidan['nama_lengkap']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                
                <div class="grid grid-cols-3 gap-4">
                    <div class="col-span-1">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Kehamilan Ke- <span class="text-red-500">*</span></label>
                        <input type="number" name="kehamilan_ke" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" min="1" max="15" value="<?= escape($ih['kehamilan_ke']) ?>" required>
                    </div>
                    <div class="col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-1">HPHT <span class="text-red-500">*</span></label>
                        <input type="date" name="hpht" id="hpht" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($ih['hpht']) ?>" required max="<?= date('Y-m-d') ?>">
                    </div>
                </div>
                
                <!-- HPL Auto Calc Box -->
                <div class="bg-primary/5 border border-primary/20 rounded-xl p-4">
                    <div class="grid grid-cols-2 gap-4 mb-3">
                        <div>
                            <span class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1">Perkiraan Lahir</span>
                            <input type="text" id="hpl_display" class="w-full bg-white border border-primary/30 rounded-lg px-3 py-1.5 text-sm font-bold text-primary" readonly>
                            <input type="hidden" name="hpl" id="hpl_hidden" value="<?= escape($ih['hpl']) ?>">
                        </div>
                        <div>
                            <span class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1">Usia Kehamilan</span>
                            <input type="text" id="usia_kehamilan" class="w-full bg-white border border-primary/30 rounded-lg px-3 py-1.5 text-sm font-bold text-gray-800" readonly>
                        </div>
                    </div>
                    <div>
                        <span class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1">Trimester</span>
                        <input type="text" id="trimester" class="w-full bg-white border border-primary/30 rounded-lg px-3 py-1.5 text-sm font-bold text-accent" readonly>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Status <span class="text-red-500">*</span></label>
                        <select name="status" 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" required>
                            <option value="Aktif" <?= $ih['status']=='Aktif'?'selected':'' ?>>Aktif</option>
                            <option value="Melahirkan" <?= $ih['status']=='Melahirkan'?'selected':'' ?>>Melahirkan</option>
                            <option value="Keguguran" <?= $ih['status']=='Keguguran'?'selected':'' ?>>Keguguran</option>
                            <option value="Pindah" <?= $ih['status']=='Pindah'?'selected':'' ?>>Pindah</option>
                        </select>
                    </div>
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Catatan Khusus</label>
                    <textarea name="catatan" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm"><?= escape($ih['catatan']) ?></textarea>
                </div>
            </div>
        </div>
    </div>
    
    <div class="bg-gray-50 px-6 py-4 border-t border-gray-100 flex items-center justify-end">
        <button type="submit" class="px-6 py-2.5 bg-yellow-500 hover:bg-yellow-600 text-white font-medium rounded-xl shadow-lg shadow-yellow-500/30 transition-all transform hover:-translate-y-0.5 focus:ring-2 focus:ring-offset-2 focus:ring-yellow-500 flex items-center gap-2">
            <i class="fas fa-save"></i> Perbarui Data Ibu Hamil
        </button>
    </div>
</form>

<script src="<?= BASE_URL ?>/assets/js/hpl_calculator.js"></script>
<script>
document.getElementById('tanggal_lahir').addEventListener('change', function() {
    if(!this.value) return;
    const dob = new Date(this.value);
    const today = new Date();
    let age = today.getFullYear() - dob.getFullYear();
    const m = today.getMonth() - dob.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
        age--;
    }
    document.getElementById('umur').value = age + ' Tahun';
});

// Trigger JS hpl calculator on load
window.onload = function() {
    if(typeof hitungDariHPHT === 'function') {
        hitungDariHPHT();
    }
};
</script>
<?php require_once '../../includes/footer_admin.php'; ?>
