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

$id_bumil = $_GET['id'];

// Ambil data ibu
$stmt = $pdo->prepare("SELECT nama_bumil, hpht FROM ibu_hamil WHERE id_bumil = ?");
$stmt->execute([$id_bumil]);
$b = $stmt->fetch();

if (isset($_POST['simpan'])) {
    try {
        // Ambil data dari form
        $tgl = $_POST['tgl_periksa'];
        $bb = $_POST['bb'];
        $tensi = $_POST['tensi'];
        $lila = $_POST['lila'];
        $tfu = !empty($_POST['tfu']) ? $_POST['tfu'] : 0;
        $hb = !empty($_POST['hb']) ? $_POST['hb'] : 0;
        $uk = $_POST['usia_minggu'];
        $ttd = $_POST['ttd'];
        $status_k = $_POST['status_k'];
        $ket = $_POST['keterangan'];

        $sql = "INSERT INTO pemeriksaan_bumil 
                (id_bumil, tgl_periksa, bb, tensi, lila, tfu, hb, usia_kehamilan, tablet_tambah_darah, status_k, keterangan) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$id_bumil, $tgl, $bb, $tensi, $lila, $tfu, $hb, $uk, $ttd, $status_k, $ket]);

        header("Location: bumil_detail.php?id=$id_bumil"); 
        exit;
    } catch (PDOException $e) {
        // Jika Error, dia akan memunculkan tulisan di layar, bukan Error 500
        die("Terjadi Kesalahan Database: " . $e->getMessage());
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Input 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; border: none; font-weight: bold; color: #0d6efd; }
    </style>
</head>
<body>

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

<div class="container pb-5">
    <div class="text-center my-3">
        <h6 class="fw-bold text-dark mb-0"><?= strtoupper($b['nama_bumil']) ?></h6>
        <small class="text-muted">HPHT: <?= date('d/m/Y', strtotime($b['hpht'] ?? 'now')) ?></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="<?= date('Y-m-d') ?>" 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" 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" placeholder="65.5" required>
                </div>
                <div class="col-6">
                    <label>Tensi (mmHg)</label>
                    <input type="text" name="tensi" class="form-control" placeholder="120/80" required>
                </div>
                <div class="col-6">
                    <label>LILA (cm)</label>
                    <input type="number" step="0.1" name="lila" class="form-control" placeholder="24.5" required>
                </div>
                <div class="col-6">
                    <label>Status K</label>
                    <select name="status_k" class="form-select" required>
                        <option value="K1">K1</option><option value="K2">K2</option>
                        <option value="K3">K3</option><option value="K4">K4</option>
                        <option value="K5">K5</option><option value="K6">K6</option>
                    </select>
                </div>
                <div class="col-6">
                    <label>TFU (cm)</label>
                    <input type="number" step="0.1" name="tfu" class="form-control" placeholder="0">
                </div>
                <div class="col-6">
                    <label>HB (g/dL)</label>
                    <input type="number" step="0.1" name="hb" class="form-control" placeholder="0">
                </div>
            </div>

            <label>Tablet Tambah Darah</label>
            <select name="ttd" class="form-select">
                <option value="Ya">Ya (Diberikan)</option>
                <option value="Tidak">Tidak</option>
            </select>

            <label>Keterangan</label>
            <textarea name="keterangan" class="form-control" rows="2" placeholder="Keluhan jika ada..."></textarea>

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

<script>
const hphtIbu = "<?= $b['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;
}
window.onload = hitungMinggu;
</script>
</body>
</html>