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

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

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

// Query ambil data ibu hamil
$query = "SELECT * FROM ibu_hamil WHERE id_posyandu = ? AND nama_bumil LIKE ? ORDER BY nama_bumil ASC";
$stmt = $pdo->prepare($query);
$stmt->execute([$id_posyandu, "%$search%"]);
$bumils = $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 Ibu Hamil</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-blue { background: #0d6efd; color: white; padding: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; margin-bottom: 20px; }
        .search-box { border-radius: 10px; border: none; padding: 12px 20px; box-shadow: 0 2px 5px 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; display: block; text-align: center; text-decoration: none; }
        .card-bumil { border: none; border-radius: 15px; margin-bottom: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); padding: 15px; position: relative; }
        .btn-periksa { border-radius: 20px; border: 1px solid #0d6efd; color: #0d6efd; font-size: 13px; padding: 5px 15px; text-decoration: none; font-weight: 500; }
        .dropdown-toggle::after { display: none; }
        .btn-more { color: #ccc; padding: 0 5px; }
    </style>
</head>
<body>

<div class="header-blue 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 Ibu Hamil</h5>
    <div style="width: 30px;"></div> </div>

<div class="container pb-5">
    <form action="" method="GET" class="mb-3">
        <div class="input-group">
            <input type="text" name="search" class="form-control search-box" placeholder="Ketik nama ibu hamil..." value="<?= htmlspecialchars($search) ?>">
            <button class="btn btn-primary rounded-end" type="submit"><i class="bi bi-search"></i></button>
        </div>
    </form>

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

    <?php if(empty($bumils)): ?>
        <div class="text-center py-5 text-muted">
            <i class="bi bi-person-x fs-1 opacity-25"></i>
            <p>Data tidak ditemukan</p>
        </div>
    <?php endif; ?>

    <?php foreach($bumils as $b): ?>
    <div class="card card-bumil bg-white border-0 shadow-sm">
        <div class="d-flex justify-content-between align-items-start">
            <div class="d-flex align-items-center">
                <div class="bg-primary-subtle p-2 rounded-circle me-3 text-primary d-none d-sm-block">
                    <i class="bi bi-person-heart fs-4"></i>
                </div>
                <div>
                    <h6 class="fw-bold mb-1 text-dark"><?= strtoupper($b['nama_bumil']) ?></h6>
                    <div class="small text-muted mb-1">
                        <i class="bi bi-calendar2-check text-danger me-1"></i> HPL: <?= date('d/m/Y', strtotime($b['hpl'])) ?>
                    </div>
                    <div class="small text-muted">
                        <i class="bi bi-person text-primary me-1"></i> Suami: <?= $b['nama_suami'] ?>
                    </div>
                </div>
            </div>

            <div class="dropdown">
                <button class="btn btn-more dropdown-toggle shadow-none" type="button" data-bs-toggle="dropdown">
                    <i class="bi bi-three-dots-vertical fs-5 text-secondary"></i>
                </button>
                <ul class="dropdown-menu dropdown-menu-end border-0 shadow rounded-3">
                    <li><a class="dropdown-item py-2" href="bumil_edit.php?id=<?= $b['id_bumil'] ?>"><i class="bi bi-pencil me-2 text-warning"></i> Edit Profil</a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item py-2 text-danger" href="bumil_hapus.php?id=<?= $b['id_bumil'] ?>" onclick="return confirm('Hapus data <?= $b['nama_bumil'] ?>?\nSemua riwayat periksa juga akan terhapus.')"><i class="bi bi-trash me-2"></i> Hapus Permanen</a></li>
                </ul>
            </div>
        </div>

        <div class="d-flex justify-content-end mt-2">
            <a href="bumil_detail.php?id=<?= $b['id_bumil'] ?>" class="btn-periksa">
                Periksa <i class="bi bi-chevron-right ms-1"></i>
            </a>
        </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>