<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'kader') { 
    header("Location: ../login.php"); 
    exit; 
}

$id = $_GET['id'];

// 1. Ambil Data Lama
$stmt = $pdo->prepare("SELECT * FROM ibu_hamil WHERE id_bumil = ?");
$stmt->execute([$id]);
$b = $stmt->fetch();

if (!$b) { die("Data tidak ditemukan"); }

// 2. Proses Simpan Perubahan
if (isset($_POST['update'])) {
    try {
        $stmt = $pdo->prepare("UPDATE ibu_hamil SET nik_bumil=?, nama_bumil=?, tgl_lahir=?, nama_suami=?, hpht=?, hpl=? WHERE id_bumil=?");
        $stmt->execute([
            $_POST['nik'], $_POST['nama'], $_POST['tgl_lahir'], 
            $_POST['suami'], $_POST['hpht'], $_POST['hpl'], $id
        ]);
        header("Location: bumil_index.php?status=updated"); 
        exit;
    } catch (PDOException $e) {
        die("Gagal memperbarui data: " . $e->getMessage());
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Profil Ibu Hamil</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        body { background: #f8f9fa; font-family: 'Segoe UI', sans-serif; }
        .header-blue { background: #0d6efd; color: white; padding: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; margin-bottom: 20px; }
        .form-card { border-radius: 20px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.05); padding: 25px; }
        .section-title { color: #0d6efd; font-weight: bold; margin-bottom: 20px; border-left: 4px solid #0d6efd; padding-left: 10px; }
        label { font-weight: 500; color: #444; margin-bottom: 3px; font-size: 13px; }
        .form-control { border-radius: 10px; padding: 10px; margin-bottom: 5px; }
        .bg-readonly { background-color: #f1f3f5 !important; font-weight: bold; color: #198754; }
        .hint { font-size: 11px; color: #6c757d; margin-bottom: 15px; display: block; }
    </style>
</head>
<body>

<div class="header-blue d-flex align-items-center shadow">
    <a href="bumil_index.php" class="text-white me-3 fs-4"><i class="bi bi-arrow-left"></i></a>
    <h5 class="mb-0 fw-bold">Edit Profil Ibu Hamil</h5>
</div>

<div class="container pb-5">
    <div class="card form-card bg-white shadow-sm">
        <form method="POST">
            <h6 class="section-title">Identitas Ibu Hamil</h6>
            
            <label>Nama Lengkap Ibu</label>
            <input type="text" name="nama" class="form-control" value="<?= $b['nama_bumil'] ?>" required>
            <span class="hint">Gunakan nama sesuai KTP</span>
            
            <label>NIK Ibu Hamil</label>
            <input type="number" name="nik" class="form-control" value="<?= $b['nik_bumil'] ?>" required>
            
            <label>Tanggal Lahir Ibu</label>
            <input type="date" name="tgl_lahir" class="form-control" value="<?= $b['tgl_lahir'] ?>" required>

            <h6 class="section-title mt-3">Detail Kehamilan</h6>
            
            <label>Nama Suami</label>
            <input type="text" name="suami" class="form-control" value="<?= $b['nama_suami'] ?>">

            <div class="row">
                <div class="col-6">
                    <label class="text-danger fw-bold">HPHT</label>
                    <input type="date" name="hpht" id="hpht" class="form-control border-danger" value="<?= $b['hpht'] ?>" required oninput="hitungHPL()">
                    <span class="hint">haid terakhir</span>
                </div>
                <div class="col-6">
                    <label class="text-success fw-bold">HPL</label>
                    <input type="date" name="hpl" id="hpl" class="form-control bg-readonly" value="<?= $b['hpl'] ?>" required>
                    <span class="hint">perkiraan lahir</span>
                </div>
            </div>

            <button type="submit" name="update" class="btn btn-primary w-100 py-3 fw-bold rounded-pill mt-4 shadow">
                <i class="bi bi-check-circle me-2"></i> SIMPAN PERUBAHAN PROFIL
            </button>
        </form>
    </div>
</div>

<script>
function hitungHPL() {
    const inputHPHT = document.getElementById('hpht').value;
    if (!inputHPHT) return;

    let hphtDate = new Date(inputHPHT);
    hphtDate.setMonth(hphtDate.getMonth() + 9);
    hphtDate.setDate(hphtDate.getDate() + 7);

    let yyyy = hphtDate.getFullYear();
    let mm = String(hphtDate.getMonth() + 1).padStart(2, '0');
    let dd = String(hphtDate.getDate()).padStart(2, '0');

    document.getElementById('hpl').value = `${yyyy}-${mm}-${dd}`;
}
</script>

</body>
</html>