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

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

$id_periksa = $_GET['id'];
$stmt = $pdo->prepare("SELECT p.*, l.nama_lansia, TIMESTAMPDIFF(YEAR, l.tgl_lahir, CURDATE()) AS umur 
                       FROM pemeriksaan_lansia p 
                       JOIN lansia l ON p.id_lansia = l.id_lansia 
                       WHERE p.id_periksa_lansia = ?");
$stmt->execute([$id_periksa]);
$p = $stmt->fetch();

if (isset($_POST['update'])) {
    $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 {
        $sql = "UPDATE pemeriksaan_lansia SET 
                tgl_periksa=?, bb=?, tb=?, tensi=?, lingkar_perut=?, imt=?, 
                gula_darah=?, asam_urat=?, kolesterol=?, skrining_mental=?, 
                catatan_obat=?, kemandirian=? 
                WHERE id_periksa_lansia=?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$tgl, $bb, $tb, $tensi, $lp, $imt, $gds, $asam, $kol, $mental, $obat, $mandiri, $id_periksa]);
        
        echo "<script>alert('Data berhasil diperbarui!'); window.location='lansia_detail.php?id=" . $p['id_lansia'] . "';</script>";
    } catch (PDOException $e) { $error = "Gagal simpan!"; }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Pemeriksaan</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=<?= $p['id_lansia'] ?>" 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>

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

        <form method="POST">
            <label>Tanggal Pemeriksaan</label>
            <input type="date" name="tgl_periksa" class="form-control" value="<?= $p['tgl_periksa'] ?>" 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" value="<?= $p['bb'] ?>" oninput="hitungIMT()"></div>
                <div class="col-6"><label>TB (cm)</label><input type="number" step="0.1" id="tb" name="tb" class="form-control" value="<?= $p['tb'] ?>" oninput="hitungIMT()"></div>
                <div class="col-6"><label>Lingkar Perut (cm)</label><input type="number" step="0.1" name="lingkar_perut" class="form-control" value="<?= $p['lingkar_perut'] ?>"></div>
                <div class="col-6"><label>IMT</label><input type="number" step="0.01" id="imt" name="imt" class="form-control" value="<?= $p['imt'] ?>" 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" value="<?= $p['tensi'] ?>" placeholder="Contoh: 120/80">

            <h6 class="section-title">Laboratorium Sederhana</h6>
            <div class="row">
                <div class="col-4"><label>GDS</label><input type="number" name="gula_darah" class="form-control" value="<?= $p['gula_darah'] ?>"></div>
                <div class="col-4"><label>Asam Urat</label><input type="number" step="0.1" name="asam_urat" class="form-control" value="<?= $p['asam_urat'] ?>"></div>
                <div class="col-4"><label>Kolesterol</label><input type="number" name="kolesterol" class="form-control" value="<?= $p['kolesterol'] ?>"></div>
            </div>

            <h6 class="section-title">Fungsional & Mental</h6>
            <label>Kemandirian</label>
            <select name="kemandirian" class="form-select">
                <option value="Mandiri (A)" <?= $p['kemandirian'] == 'Mandiri (A)' ? 'selected' : '' ?>>Mandiri (A)</option>
                <option value="Dibantu Sebagian (B)" <?= $p['kemandirian'] == 'Dibantu Sebagian (B)' ? 'selected' : '' ?>>Dibantu Sebagian (B)</option>
                <option value="Ketergantungan Total (C)" <?= $p['kemandirian'] == 'Ketergantungan Total (C)' ? 'selected' : '' ?>>Ketergantungan Total (C)</option>
            </select>

            <label>Skrining Mental</label>
            <select name="skrining_mental" class="form-select">
                <option value="Normal" <?= $p['skrining_mental'] == 'Normal' ? 'selected' : '' ?>>Normal</option>
                <option value="Ada Masalah" <?= $p['skrining_mental'] == 'Ada Masalah' ? 'selected' : '' ?>>Ada Masalah</option>
            </select>

            <h6 class="section-title">Catatan</h6>
            <label>Pemberian Obat / Keluhan</label>
            <textarea name="catatan_obat" class="form-control" rows="3"><?= $p['catatan_obat'] ?></textarea>

            <button type="submit" name="update" class="btn btn-success w-100 py-3 fw-bold mt-4 shadow-sm" style="border-radius:15px;">
                <i class="bi bi-check-circle me-2"></i> PERBARUI 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;
        document.getElementById('imt').value = (bb / (tbM * tbM)).toFixed(2);
    }
}
</script>
</body>
</html>