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

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

$id_periksa = $_GET['id_periksa'];

// 1. Ambil data pemeriksaan lama & identitas bumil
$stmt = $pdo->prepare("SELECT p.*, b.nama_bumil, b.hpht FROM pemeriksaan_bumil p 
                        JOIN ibu_hamil b ON p.id_bumil = b.id_bumil 
                        WHERE p.id_periksa_bumil = ?");
$stmt->execute([$id_periksa]);
$p = $stmt->fetch();

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

// 2. Proses Update
if (isset($_POST['update'])) {
    try {
        $sql = "UPDATE pemeriksaan_bumil SET 
                tgl_periksa=?, bb=?, tensi=?, lila=?, tfu=?, hb=?, 
                usia_kehamilan=?, tablet_tambah_darah=?, status_k=?, keterangan=? 
                WHERE id_periksa_bumil=?";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            $_POST['tgl_periksa'], $_POST['bb'], $_POST['tensi'], $_POST['lila'], 
            $_POST['tfu'], $_POST['hb'], $_POST['usia_minggu'], 
            $_POST['ttd'], $_POST['status_k'], $_POST['keterangan'], $id_periksa
        ]);

        header("Location: bumil_detail.php?id=" . $p['id_bumil'] . "&status=updated"); 
        exit;
    } catch (PDOException $e) {
        die("Gagal update 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 Pemeriksaan Bumil</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; }
        .form-card { border-radius: 20px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.05); padding: 25px; margin-top: -10px; }
        .section-title { color: #0d6efd; font-weight: bold; margin-bottom: 15px; font-size: 14px; border-left: 4px solid #0d6efd; padding-left: 10px; }
        label { font-weight: 500; color: #444; margin-bottom: 5px; font-size: 13px; }
        .form-control, .form-select { border-radius: 10px; padding: 10px; margin-bottom: 15px; font-size: 14px; }
        .bg-info-light { background: #e7f1ff !important; font-weight: bold; color: #0d6efd; }
    </style>
</head>
<body>

<div class="header-blue d-flex align-items-center shadow">
    <a href="bumil_detail.php?id=<?= $p['id_bumil'] ?>" class="text-white me-3 fs-4"><i class="bi bi-arrow-left"></i></a>
    <h5 class="mb-0 fw-bold">Edit Pemeriksaan</h5>
</div>

<div class="container pb-5">
    <div class="text-center my-3">
        <h6 class="fw-bold text-dark mb-0"><?= strtoupper($p['nama_bumil']) ?></h6>
        <small class="text-muted">Mengedit data tanggal: <?= date('d/m/Y', strtotime($p['tgl_periksa'])) ?></small>
    </div>

    <div class="card form-card bg-white shadow-sm">
        <form method="POST">
            <h6 class="section-title">Waktu & Usia Kandungan</h6>
            <div class="row">
                <div class="col-6">
                    <label>Tgl Periksa</label>
                    <input type="date" name="tgl_periksa" id="tgl_periksa" class="form-control" value="<?= $p['tgl_periksa'] ?>" required onchange="hitungMinggu()">
                </div>
                <div class="col-6">
                    <label>UK (Minggu)</label>
                    <input type="number" name="usia_minggu" id="usia_minggu" class="form-control bg-info-light" value="<?= $p['usia_kehamilan'] ?>" readonly>
                </div>
            </div>

            <h6 class="section-title mt-2">Hasil Pemeriksaan</h6>
            <div class="row">
                <div class="col-6">
                    <label>BB (kg)</label>
                    <input type="number" step="0.01" name="bb" class="form-control" value="<?= $p['bb'] ?>" required>
                </div>
                <div class="col-6">
                    <label>Tensi (mmHg)</label>
                    <input type="text" name="tensi" class="form-control" value="<?= $p['tensi'] ?>" required>
                </div>
                <div class="col-6">
                    <label>LILA (cm)</label>
                    <input type="number" step="0.1" name="lila" class="form-control" value="<?= $p['lila'] ?>" required>
                </div>
                <div class="col-6">
                    <label>Status K</label>
                    <select name="status_k" class="form-select" required>
                        <?php foreach(['K1','K2','K3','K4','K5','K6'] as $k): ?>
                            <option value="<?= $k ?>" <?= $p['status_k'] == $k ? 'selected' : '' ?>><?= $k ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-6">
                    <label>TFU (cm)</label>
                    <input type="number" step="0.1" name="tfu" class="form-control" value="<?= $p['tfu'] ?>">
                </div>
                <div class="col-6">
                    <label>HB (g/dL)</label>
                    <input type="number" step="0.1" name="hb" class="form-control" value="<?= $p['hb'] ?>">
                </div>
            </div>

            <label>Tablet Tambah Darah</label>
            <select name="ttd" class="form-select">
                <option value="Ya" <?= $p['tablet_tambah_darah'] == 'Ya' ? 'selected' : '' ?>>Ya (Diberikan)</option>
                <option value="Tidak" <?= $p['tablet_tambah_darah'] == 'Tidak' ? 'selected' : '' ?>>Tidak</option>
            </select>

            <label>Keterangan</label>
            <textarea name="keterangan" class="form-control" rows="2"><?= $p['keterangan'] ?></textarea>

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

<script>
const hphtIbu = "<?= $p['hpht'] ?>";
function hitungMinggu() {
    const tglPeriksa = document.getElementById('tgl_periksa').value;
    if (!tglPeriksa || !hphtIbu) return;
    const d1 = new Date(hphtIbu);
    const d2 = new Date(tglPeriksa);
    const diffTime = Math.abs(d2 - d1);
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
    const weeks = Math.floor(diffDays / 7);
    document.getElementById('usia_minggu').value = weeks;
}
</script>

</body>
</html>