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

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

$id_lansia = $_GET['id'];

// Ambil data dasar lansia
$stmt = $pdo->prepare("SELECT nama_lansia, TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS umur FROM lansia WHERE id_lansia = ?");
$stmt->execute([$id_lansia]);
$l = $stmt->fetch();

if (isset($_POST['simpan'])) {
    // 1. Ambil data dari FORM
    $tgl = $_POST['tgl_periksa'];
    $bb = !empty($_POST['bb']) ? $_POST['bb'] : 0;
    $tb = !empty($_POST['tb']) ? $_POST['tb'] : 0;
    $lp = !empty($_POST['lingkar_perut']) ? $_POST['lingkar_perut'] : 0;
    $imt = !empty($_POST['imt']) ? $_POST['imt'] : 0;
    $tensi = $_POST['tensi'];
    $gds = !empty($_POST['gula_darah']) ? $_POST['gula_darah'] : 0;
    $asam = !empty($_POST['asam_urat']) ? $_POST['asam_urat'] : 0;
    $kol = !empty($_POST['kolesterol']) ? $_POST['kolesterol'] : 0;
    $mental = $_POST['skrining_mental'];
    $obat = $_POST['catatan_obat'];
    $mandiri = $_POST['kemandirian'];

    try {
        // 2. Query INSERT disesuaikan dengan urutan SS4.JPG
        // id_lansia(2), tgl_periksa(3), bb(4), tb(5), tensi(6), lingkar_perut(7), imt(8), gula_darah(9), asam_urat(10), kolesterol(11), skrining_mental(12), catatan_obat(13), kemandirian(14)
        $sql = "INSERT INTO pemeriksaan_lansia 
                (id_lansia, tgl_periksa, bb, tb, tensi, lingkar_perut, imt, gula_darah, asam_urat, kolesterol, skrining_mental, catatan_obat, kemandirian) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            $id_lansia, $tgl, $bb, $tb, $tensi, $lp, $imt, $gds, $asam, $kol, $mental, $obat, $mandiri
        ]);

        echo "<script>alert('Data Berhasil Disimpan!'); window.location='lansia_detail.php?id=$id_lansia';</script>";
        exit;
    } catch (PDOException $e) {
        // Jika gagal, tampilkan pesan error agar kita tahu salahnya di mana
        die("Gagal menyimpan data ke 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>Periksa Lansia</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-purple { background: #6f42c1; color: white; padding: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; }
        .form-card { border: none; border-radius: 20px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); padding: 20px; background: white; margin-top: -10px; }
        .section-title { color: #6f42c1; font-weight: bold; border-left: 4px solid #6f42c1; padding-left: 10px; margin-top: 20px; margin-bottom: 10px; font-size: 14px; }
        label { font-weight: 600; color: #444; font-size: 13px; margin-top: 10px; }
        .form-control, .form-select { border-radius: 10px; padding: 10px; }
    </style>
</head>
<body>

<div class="header-purple shadow-sm">
    <div class="container d-flex align-items-center">
        <a href="lansia_detail.php?id=<?= $id_lansia ?>" class="text-white me-3 fs-4"><i class="bi bi-arrow-left"></i></a>
        <h5 class="mb-0 fw-bold">Input Pemeriksaan</h5>
    </div>
</div>

<div class="container mt-3 pb-5">
    <div class="card form-card">
        <div class="mb-3 text-center">
            <h5 class="fw-bold mb-0 text-purple" style="color:#6f42c1;"><?= strtoupper($l['nama_lansia']) ?></h5>
            <small class="text-muted"><?= $l['umur'] ?> Tahun</small>
        </div>

        <form method="POST">
            <label>Tanggal Pemeriksaan</label>
            <input type="date" name="tgl_periksa" class="form-control" value="<?= date('Y-m-d') ?>" required>

            <h6 class="section-title">Antropometri</h6>
            <div class="row">
                <div class="col-6">
                    <label>BB (kg)</label>
                    <input type="number" step="0.1" id="bb" name="bb" class="form-control" placeholder="0.0" oninput="hitungIMT()">
                </div>
                <div class="col-6">
                    <label>TB (cm)</label>
                    <input type="number" step="0.1" id="tb" name="tb" class="form-control" placeholder="0" oninput="hitungIMT()">
                </div>
                <div class="col-6">
                    <label>Lingkar Perut (cm)</label>
                    <input type="number" step="0.1" name="lingkar_perut" class="form-control" placeholder="0">
                </div>
                <div class="col-6">
                    <label>IMT</label>
                    <input type="number" step="0.01" id="imt" name="imt" class="form-control" readonly style="background:#eee;">
                </div>
            </div>

            <h6 class="section-title">Tanda Vital</h6>
            <label>Tekanan Darah (Tensi)</label>
            <input type="text" name="tensi" class="form-control" placeholder="Contoh: 120/80">

            <h6 class="section-title">Laboratorium</h6>
            <div class="row">
                <div class="col-4">
                    <label>GDS</label>
                    <input type="number" name="gula_darah" class="form-control" placeholder="0">
                </div>
                <div class="col-4">
                    <label>Asam Urat</label>
                    <input type="number" step="0.1" name="asam_urat" class="form-control" placeholder="0">
                </div>
                <div class="col-4">
                    <label>Kolesterol</label>
                    <input type="number" name="kolesterol" class="form-control" placeholder="0">
                </div>
            </div>

            <h6 class="section-title">Fungsional & Mental</h6>
            <label>Kemandirian</label>
            <select name="kemandirian" class="form-select">
                <option value="Mandiri (A)">Mandiri (A)</option>
                <option value="Dibantu Sebagian (B)">Dibantu Sebagian (B)</option>
                <option value="Ketergantungan Total (C)">Ketergantungan Total (C)</option>
            </select>

            <label>Skrining Mental</label>
            <select name="skrining_mental" class="form-select">
                <option value="Normal">Normal</option>
                <option value="Ada Masalah">Ada Masalah</option>
            </select>

            <h6 class="section-title">Catatan</h6>
            <label>Pemberian Obat / Keluhan</label>
            <textarea name="catatan_obat" class="form-control" rows="3" placeholder="Tulis catatan di sini..."></textarea>

            <button type="submit" name="simpan" class="btn btn-success w-100 py-3 fw-bold mt-4 shadow-sm" style="border-radius:15px;">
                <i class="bi bi-save me-2"></i> SIMPAN DATA
            </button>
        </form>
    </div>
</div>

<script>
function hitungIMT() {
    let bb = document.getElementById('bb').value;
    let tb = document.getElementById('tb').value;
    if (bb > 0 && tb > 0) {
        let tbM = tb / 100;
        let imt = bb / (tbM * tbM);
        document.getElementById('imt').value = imt.toFixed(2);
    }
}
</script>
</body>
</html>