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

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

$bulan = $_GET['bulan'] ?? date('m');
$tahun = $_GET['tahun'] ?? date('Y');
$id_posyandu_filter = $_GET['id_posyandu'] ?? 'semua';

$nama_bulan = ["01"=>"Januari","02"=>"Februari","03"=>"Maret","04"=>"April","05"=>"Mei","06"=>"Juni","07"=>"Juli","08"=>"Agustus","09"=>"September","10"=>"Oktober","11"=>"November","12"=>"Desember"];
$list_posyandu = $pdo->query("SELECT * FROM posyandu ORDER BY nama_posyandu ASC")->fetchAll();

$sql_filter = ($id_posyandu_filter !== 'semua') ? " AND b.id_posyandu = ?" : "";
$params = ($id_posyandu_filter !== 'semua') ? [$bulan, $tahun, $id_posyandu_filter] : [$bulan, $tahun];

$query = "SELECT b.*, p.*, pos.nama_posyandu 
          FROM ibu_hamil b 
          JOIN pemeriksaan_bumil p ON b.id_bumil = p.id_bumil 
          JOIN posyandu pos ON b.id_posyandu = pos.id_posyandu
          WHERE MONTH(p.tgl_periksa) = ? AND YEAR(p.tgl_periksa) = ? $sql_filter";

$stmt = $pdo->prepare($query);
$stmt->execute($params);
$data_bumil = $stmt->fetchAll();

// --- LOGIKA HITUNG REKAPITULASI BUMIL ---
$rekap = [
    'total' => count($data_bumil),
    'k1' => 0, 'k4' => 0, 'kek' => 0, 'risti' => 0,
    'anemia' => 0 // Tambahan indikator HB < 11
];

foreach($data_bumil as $r) {
    if($r['status_k'] == 'K1') $rekap['k1']++;
    if($r['status_k'] == 'K4') $rekap['k4']++;
    if($r['lila'] < 23.5 && $r['lila'] > 0) $rekap['kek']++;
    if(stripos($r['keterangan'], 'Risti') !== false) $rekap['risti']++;
    if((float)($r['hb']??0) < 11 && (float)($r['hb']??0) > 0) $rekap['anemia']++;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rekapitulasi 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://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { background: #f4f7f6; font-family: 'Segoe UI', sans-serif; font-size: 11px; }
        .main-content { margin-left: 250px; padding: 20px; transition: 0.3s; }
        /* Warna Ungu sesuai permintaan */
        .table-custom thead th { background: #6f42c1 !important; color: white !important; text-align: center; vertical-align: middle; border: 1px solid #dee2e6; }
        .table-custom td { border: 1px solid #dee2e6; text-align: center; vertical-align: middle; }
        .rekap-header { background: #f3e5f5; color: #6f42c1; font-weight: bold; }
        @media (max-width: 768px) { .main-content { margin-left: 0; padding: 15px; } }
    </style>
</head>
<body>
    <?php include 'sidebar.php'; ?>
    <div class="main-content">
        <div class="container-fluid">
            <h4 class="fw-bold mb-4">REKAPITULASI DATA IBU HAMIL</h4>

            <div class="card p-3 mb-3 border-0 shadow-sm rounded-3">
                <form method="GET" class="row g-2 align-items-end">
                    <div class="col-md-3"><label class="small fw-bold mb-1">Bulan</label><select name="bulan" class="form-select form-select-sm"><?php foreach($nama_bulan as $m=>$n) echo "<option value='$m' ".($bulan==$m?'selected':'').">$n</option>"; ?></select></div>
                    <div class="col-md-2"><label class="small fw-bold mb-1">Tahun</label><input type="number" name="tahun" class="form-control form-select-sm" value="<?=$tahun?>"></div>
                    <div class="col-md-4"><label class="small fw-bold mb-1">Posyandu</label><select name="id_posyandu" class="form-select form-select-sm"><option value="semua">Semua Posyandu</option><?php foreach($list_posyandu as $p) echo "<option value='".$p['id_posyandu']."' ".($id_posyandu_filter==$p['id_posyandu']?'selected':'').">".$p['nama_posyandu']."</option>"; ?></select></div>
                    <div class="col-md-3"><button type="submit" class="btn btn-sm btn-primary w-100 shadow-none" style="background-color: #6f42c1; border: none;"><i class="fa fa-filter me-2"></i>Filter Data</button></div>
                </form>
            </div>

            <div class="mb-3">
                <a href="export_bumil.php?bulan=<?=$bulan?>&tahun=<?=$tahun?>&id_posyandu=<?=$id_posyandu_filter?>" class="btn btn-sm btn-outline-secondary shadow-none">
                    <i class="fa fa-file-excel me-2 text-success"></i>Export ke Excel (.xlsx)
                </a>
            </div>

            <div class="card border-0 shadow-sm rounded-3 mb-4">
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle m-0 table-custom">
                            <thead>
                                <tr>
                                    <th rowspan="2">No</th><th rowspan="2">Posyandu</th><th rowspan="2">Nama Bumil</th><th rowspan="2">NIK</th><th rowspan="2">Suami</th>
                                    <th colspan="3">Kesehatan</th><th rowspan="2">Status</th><th colspan="4">Pemeriksaan</th><th rowspan="2">Keterangan</th>
                                </tr>
                                <tr><th>HPHT</th><th>HPL</th><th>Usia Hamil</th><th>BB</th><th>Tensi</th><th>LILA</th><th>HB</th></tr>
                            </thead>
                            <tbody>
                                <?php $n=1; foreach($data_bumil as $r): ?>
                                <tr>
                                    <td><?=$n++?></td>
                                    <td class="text-purple fw-bold" style="color: #6f42c1;"><?=$r['nama_posyandu']?></td>
                                    <td class="text-start fw-bold"><?=strtoupper($r['nama_bumil'])?></td>
                                    <td>'<?=$r['nik_bumil']?></td><td><?=$r['nama_suami']?></td>
                                    <td><?=$r['hpht']?></td><td><?=$r['hpl']?></td><td><?=$r['usia_kehamilan']?></td>
                                    <td><span class="badge" style="background-color: #6f42c1;"><?=$r['status_k']?></span></td>
                                    <td><?=$r['bb']?></td><td><?=$r['tensi']?></td>
                                    <td class="<?=($r['lila'] < 23.5 && $r['lila'] > 0)?'text-danger fw-bold':''?>"><?=$r['lila']?></td>
                                    <td class="<?=((float)($r['hb']??0) < 11 && (float)($r['hb']??0) > 0)?'text-danger fw-bold':''?>"><?=$r['hb']?></td>
                                    <td class="text-start small"><?=$r['keterangan']?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="card border-0 shadow-sm rounded-3">
                        <div class="card-body p-0">
                            <table class="table table-bordered align-middle m-0 table-custom">
                                <thead>
                                    <tr><th colspan="2">RINGKASAN STATUS KUNJUNGAN</th></tr>
                                </thead>
                                <tbody>
                                    <tr><td class="text-start ps-3">Total Ibu Hamil Diperiksa</td><td class="fw-bold" style="color: #6f42c1;"><?=$rekap['total']?></td></tr>
                                    <tr><td class="text-start ps-3">Kunjungan Pertama (K1)</td><td class="fw-bold text-success"><?=$rekap['k1']?></td></tr>
                                    <tr><td class="text-start ps-3">Kunjungan Lengkap (K4)</td><td class="fw-bold text-primary"><?=$rekap['k4']?></td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card border-0 shadow-sm rounded-3">
                        <div class="card-body p-0">
                            <table class="table table-bordered align-middle m-0 table-custom">
                                <thead>
                                    <tr><th colspan="2">RINGKASAN RISIKO KESEHATAN</th></tr>
                                </thead>
                                <tbody>
                                    <tr><td class="text-start ps-3">Ibu Hamil KEK (LILA < 23.5)</td><td class="fw-bold text-danger"><?=$rekap['kek']?></td></tr>
                                    <tr><td class="text-start ps-3">Ibu Hamil Anemia (HB < 11)</td><td class="fw-bold text-danger"><?=$rekap['anemia']?></td></tr>
                                    <tr><td class="text-start ps-3">Ibu Hamil Risiko Tinggi (Risti)</td><td class="fw-bold text-danger"><?=$rekap['risti']?></td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</body>
</html>