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

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

if (isset($_POST['simpan'])) {
    $nik = $_POST['nik'];
    $nama = $_POST['nama'];
    $tgl_lahir = $_POST['tgl_lahir'];
    $jk = $_POST['jk'];
    $rt = $_POST['rt'];
    $rw = $_POST['rw'];
    $dusun = $_POST['dusun'];
    $id_posyandu = $_SESSION['posyandu'];

    try {
        // Query disesuaikan dengan 3 kolom alamat baru
        $stmt = $pdo->prepare("INSERT INTO lansia (nik_lansia, nama_lansia, tgl_lahir, jenis_kelamin, rt, rw, dusun, id_posyandu) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$nik, $nama, $tgl_lahir, $jk, $rt, $rw, $dusun, $id_posyandu]);
        
        echo "<script>alert('Lansia berhasil didaftarkan!'); window.location='lansia_index.php';</script>";
    } catch (PDOException $e) {
        $error = "Gagal simpan! Periksa apakah NIK sudah pernah terdaftar.";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tambah 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; margin-bottom: 5px; font-size: 13px; }
        .form-control, .form-select { border-radius: 10px; padding: 10px; margin-bottom: 15px; }
        .btn-simpan { 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 mx-auto">Registrasi Lansia</h5>
</div>

<div class="container pb-5">
    <?php if(isset($error)): ?>
        <div class="alert alert-danger shadow-sm"><?= $error ?></div>
    <?php endif; ?>

    <div class="card form-card">
        <form method="POST">
            <label>NIK Lansia</label>
            <input type="number" name="nik" class="form-control" placeholder="16 Digit NIK" required>

            <label>Nama Lengkap</label>
            <input type="text" name="nama" class="form-control" placeholder="Sesuai KTP" required>

            <div class="row">
                <div class="col-6">
                    <label>Tanggal Lahir</label>
                    <input type="date" name="tgl_lahir" class="form-control" required>
                </div>
                <div class="col-6">
                    <label>Jenis Kelamin</label>
                    <select name="jk" class="form-select" required>
                        <option value="L">Laki-laki</option>
                        <option value="P">Perempuan</option>
                    </select>
                </div>
            </div>

            <h6 class="text-purple 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" placeholder="001">
                </div>
                <div class="col-4">
                    <label>RW</label>
                    <input type="text" name="rw" class="form-control" placeholder="002">
                </div>
                <div class="col-4">
                    <label>Dusun</label>
                    <input type="text" name="dusun" class="form-control" placeholder="Nama Dusun">
                </div>
            </div>

            <div class="d-grid mt-3">
                <button type="submit" name="simpan" class="btn btn-simpan shadow">
                    <i class="bi bi-save me-2"></i> SIMPAN DATA LANSIA
                </button>
            </div>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>