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

$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM lansia WHERE id_lansia = ?");
$stmt->execute([$id]);
$l = $stmt->fetch();

if (isset($_POST['update'])) {
    $nik = $_POST['nik'];
    $nama = $_POST['nama'];
    $tgl_lahir = $_POST['tgl_lahir'];
    $jk = $_POST['jk'];
    $rt = $_POST['rt'];
    $rw = $_POST['rw'];
    $dusun = $_POST['dusun'];

    try {
        $stmt = $pdo->prepare("UPDATE lansia SET nik_lansia=?, nama_lansia=?, tgl_lahir=?, jenis_kelamin=?, rt=?, rw=?, dusun=? WHERE id_lansia=?");
        $stmt->execute([$nik, $nama, $tgl_lahir, $jk, $rt, $rw, $dusun, $id]);
        echo "<script>alert('Data berhasil diperbarui!'); window.location='lansia_index.php';</script>";
    } catch (PDOException $e) { $error = "Gagal update data!"; }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit 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; margin-bottom: 20px; }
        .form-card { border: none; border-radius: 20px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); padding: 25px; background: white; }
        label { font-weight: 600; color: #444; font-size: 13px; margin-bottom: 5px; }
        .form-control, .form-select { border-radius: 10px; padding: 10px; margin-bottom: 15px; }
        .btn-update { background: #6f42c1; color: white; border-radius: 12px; padding: 15px; font-weight: bold; border: none; }
    </style>
</head>
<body>

<div class="header-purple d-flex align-items-center shadow">
    <a href="lansia_index.php" class="text-white me-3 fs-4"><i class="bi bi-arrow-left"></i></a>
    <h5 class="mb-0 fw-bold">Edit Data Lansia</h5>
</div>

<div class="container pb-5">
    <div class="card form-card">
        <form method="POST">
            <label>NIK Lansia</label>
            <input type="number" name="nik" class="form-control" value="<?= $l['nik_lansia'] ?>" required>
            <label>Nama Lengkap</label>
            <input type="text" name="nama" class="form-control" value="<?= $l['nama_lansia'] ?>" required>
            <div class="row">
                <div class="col-6">
                    <label>Tanggal Lahir</label>
                    <input type="date" name="tgl_lahir" class="form-control" value="<?= $l['tgl_lahir'] ?>" required>
                </div>
                <div class="col-6">
                    <label>Jenis Kelamin</label>
                    <select name="jk" class="form-select">
                        <option value="L" <?= $l['jenis_kelamin']=='L'?'selected':'' ?>>Laki-laki</option>
                        <option value="P" <?= $l['jenis_kelamin']=='P'?'selected':'' ?>>Perempuan</option>
                    </select>
                </div>
            </div>
            <h6 class="fw-bold mt-2 mb-3" style="color:#6f42c1;">Alamat Domisili</h6>
            <div class="row">
                <div class="col-4"><label>RT</label><input type="text" name="rt" class="form-control" value="<?= $l['rt'] ?>"></div>
                <div class="col-4"><label>RW</label><input type="text" name="rw" class="form-control" value="<?= $l['rw'] ?>"></div>
                <div class="col-4"><label>Dusun</label><input type="text" name="dusun" class="form-control" value="<?= $l['dusun'] ?>"></div>
            </div>
            <div class="d-grid mt-3">
                <button type="submit" name="update" class="btn btn-update shadow">SIMPAN PERUBAHAN</button>
            </div>
        </form>
    </div>
</div>
</body>
</html>