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

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

$id_posyandu = $_SESSION['posyandu'];
$search = $_GET['search'] ?? '';

$query = "SELECT *, TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS umur 
          FROM lansia 
          WHERE id_posyandu = ? AND nama_lansia LIKE ? 
          ORDER BY nama_lansia ASC";
$stmt = $pdo->prepare($query);
$stmt->execute([$id_posyandu, "%$search%"]);
$lansias = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data 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; }
        .search-box { border-radius: 12px; border: none; padding: 12px 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .btn-tambah { background: #198754; color: white; border-radius: 15px; padding: 12px; font-weight: bold; width: 100%; border: none; margin-bottom: 20px; }
        .card-lansia { border: none; border-radius: 15px; margin-bottom: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); padding: 18px; }
        .btn-periksa { border-radius: 20px; border: 1px solid #6f42c1; color: #6f42c1; font-size: 13px; padding: 5px 15px; text-decoration: none; font-weight: 600; }
        .badge-umur { background: #f3f0ff; color: #6f42c1; border-radius: 6px; padding: 4px 10px; font-size: 12px; font-weight: bold; }
        .btn-option { color: #ccc; border: none; background: none; }
    </style>
</head>
<body>

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

<div class="container pb-5">
    <form action="" method="GET" class="mb-3">
        <input type="text" name="search" class="form-control search-box" placeholder="Cari nama lansia..." value="<?= htmlspecialchars($search) ?>">
    </form>

    <a href="lansia_tambah.php" class="btn btn-tambah shadow-sm">
        <i class="bi bi-plus-lg me-1"></i> Tambah Lansia Baru
    </a>

    <?php foreach($lansias as $l): ?>
    <div class="card card-lansia">
        <div class="d-flex justify-content-between">
            <div>
                <h6 class="fw-bold mb-1 text-dark" style="font-size: 16px;"><?= strtoupper($l['nama_lansia']) ?></h6>
                <div class="mb-2">
                    <span class="badge-umur"><?= $l['umur'] ?> Tahun</span>
                    <small class="text-muted ms-2"><?= $l['jenis_kelamin'] == 'L' ? 'Laki-laki' : 'Perempuan' ?></small>
                </div>
                <div class="small text-muted">
                    <i class="bi bi-geo-alt"></i> Dusun <?= $l['dusun'] ?>, RT <?= $l['rt'] ?>/<?= $l['rw'] ?>
                </div>
            </div>
            <div class="text-end d-flex flex-column justify-content-between align-items-end">
                <div class="dropdown">
                    <button class="btn-option" data-bs-toggle="dropdown"><i class="bi bi-three-dots-vertical fs-5"></i></button>
                    <ul class="dropdown-menu dropdown-menu-end shadow border-0">
                        <li><a class="dropdown-item" href="lansia_edit.php?id=<?= $l['id_lansia'] ?>"><i class="bi bi-pencil me-2 text-warning"></i> Edit Profil</a></li>
                        <li><a class="dropdown-item text-danger" href="lansia_hapus.php?id=<?= $l['id_lansia'] ?>" onclick="return confirm('Hapus permanen lansia ini?')"><i class="bi bi-trash me-2"></i> Hapus</a></li>
                    </ul>
                </div>
                <a href="lansia_detail.php?id=<?= $l['id_lansia'] ?>" class="btn-periksa">Detail <i class="bi bi-chevron-right"></i></a>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>

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