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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    exit("Akses ditolak");
}

$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"];

// HEADER UNTUK EXCEL
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=Laporan_Ibu_Hamil_".$bulan."_".$tahun.".xls");

// --- QUERY DATA ---
$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 ---
$rekap = [
    'total' => count($data_bumil),
    'k1' => 0, 'k4' => 0, 'kek' => 0, 'risti' => 0, 'anemia' => 0
];

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']++;
}
?>

<h3>LAPORAN REKAPITULASI IBU HAMIL - <?=$nama_bulan[$bulan]?> <?=$tahun?></h3>

<table border="1">
    <thead>
        <tr style="background-color: #6f42c1; color: white;">
            <th rowspan="2">No</th><th rowspan="2">Posyandu</th><th rowspan="2">Nama Ibu Hamil</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><?=$r['nama_posyandu']?></td>
            <td><?=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><?=$r['status_k']?></td>
            <td><?=$r['bb']?></td><td><?=$r['tensi']?></td><td><?=$r['lila']?></td><td><?=$r['hb']?></td>
            <td><?=$r['keterangan']?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<br>

<table border="1">
    <thead>
        <tr style="background-color: #6f42c1; color: white;">
            <th colspan="2">RINGKASAN STATUS KUNJUNGAN</th>
            <th colspan="2">RINGKASAN RISIKO KESEHATAN</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Total Ibu Hamil Diperiksa</td><td><?=$rekap['total']?></td>
            <td>Ibu Hamil KEK (LILA < 23.5)</td><td><?=$rekap['kek']?></td>
        </tr>
        <tr>
            <td>Kunjungan Pertama (K1)</td><td><?=$rekap['k1']?></td>
            <td>Ibu Hamil Anemia (HB < 11)</td><td><?=$rekap['anemia']?></td>
        </tr>
        <tr>
            <td>Kunjungan Lengkap (K4)</td><td><?=$rekap['k4']?></td>
            <td>Ibu Hamil Risiko Tinggi (Risti)</td><td><?=$rekap['risti']?></td>
        </tr>
    </tbody>
</table>