<?php
// PASTIKAN TIDAK ADA SPASI ATAU BARIS KOSONG DI ATAS TAG PHP INI
session_start();
require_once '../config.php';

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'kader') { exit; }

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

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

// Ambil Nama Posyandu
$stmt_p = $pdo->prepare("SELECT nama_posyandu FROM posyandu WHERE id_posyandu = ?");
$stmt_p->execute([$id_posyandu]);
$posyandu = $stmt_p->fetch();

// Ambil Data
$query = "SELECT b.*, p.* FROM ibu_hamil b 
          JOIN pemeriksaan_bumil p ON b.id_bumil = p.id_bumil 
          WHERE b.id_posyandu = ? AND MONTH(p.tgl_periksa) = ? AND YEAR(p.tgl_periksa) = ? 
          ORDER BY b.nama_bumil ASC";
$stmt = $pdo->prepare($query);
$stmt->execute([$id_posyandu, $bulan, $tahun]);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Hitung Rekap
$rekap = ['K1'=>0, 'K4'=>0, 'K6'=>0, 'KEK'=>0, 'ANEMIA'=>0];
foreach ($data as $r) {
    if($r['status_k'] == 'K1') $rekap['K1']++;
    if($r['status_k'] == 'K4') $rekap['K4']++;
    if($r['status_k'] == 'K6') $rekap['K6']++;
    if($r['lila'] < 23.5) $rekap['KEK']++;
    if($r['hb'] > 0 && $r['hb'] < 11) $rekap['ANEMIA']++;
}

// Header untuk Download
$filename = "Laporan_Bumil_" . $nama_bulan[$bulan] . "_" . $tahun . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Pragma: no-cache");
header("Expires: 0");
?>
<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
        .str { mso-number-format:"\@"; } /* Mengunci NIK agar tidak berubah jadi 3.21E+15 */
        th { background-color: #0d6efd; color: white; border: 1px solid #000; height: 30px; }
        td { border: 1px solid #000; padding: 5px; }
        .bg-rekap { background-color: #e7f1ff; font-weight: bold; }
    </style>
</head>
<body>
    <table>
        <tr>
            <th colspan="15" style="background:none; color:black; font-size:16px; border:none;">LAPORAN PEMERIKSAAN IBU HAMIL</th>
        </tr>
        <tr>
            <th colspan="15" style="background:none; color:black; border:none;">Posyandu: <?= strtoupper($posyandu['nama_posyandu']) ?> | Periode: <?= $nama_bulan[$bulan] ?> <?= $tahun ?></th>
        </tr>
        <tr></tr>
        <thead>
            <tr>
                <th>NO</th>
                <th>NAMA BUMIL</th>
                <th>NIK</th>
                <th>NAMA SUAMI</th>
                <th>TGL LAHIR</th>
                <th>HPHT</th>
                <th>HPL</th>
                <th>UK (MGG)</th>
                <th>ST</th>
                <th>BB</th>
                <th>TENSI</th>
                <th>LILA</th>
                <th>TFU</th>
                <th>HB</th>
                <th>TTD</th>
            </tr>
        </thead>
        <tbody>
            <?php $n=1; foreach($data as $r): ?>
            <tr>
                <td align="center"><?= $n++ ?></td>
                <td><?= strtoupper($r['nama_bumil']) ?></td>
                <td class="str"><?= $r['nik_bumil'] ?></td>
                <td><?= $r['nama_suami'] ?></td>
                <td align="center"><?= date('d/m/Y', strtotime($r['tgl_lahir'])) ?></td>
                <td align="center"><?= date('d/m/Y', strtotime($r['hpht'])) ?></td>
                <td align="center"><?= date('d/m/Y', strtotime($r['hpl'])) ?></td>
                <td align="center"><?= $r['usia_kehamilan'] ?></td>
                <td align="center"><?= $r['status_k'] ?></td>
                <td align="center"><?= $r['bb'] ?></td>
                <td align="center"><?= $r['tensi'] ?></td>
                <td align="center"><?= $r['lila'] ?></td>
                <td align="center"><?= $r['tfu'] ?></td>
                <td align="center"><?= $r['hb'] ?></td>
                <td align="center"><?= $r['tablet_tambah_darah'] ?></td>
            </tr>
            <?php endforeach; ?>
            
            <tr></tr> <tr>
                <td colspan="5" class="bg-rekap" align="center">RINGKASAN DATA PEMERIKSAAN BULAN INI</td>
                <td colspan="10" style="border:none;"></td>
            </tr>
            <tr>
                <td colspan="2" align="right">JUMLAH K1:</td>
                <td align="left"><b><?= $rekap['K1'] ?> Orang</b></td>
                <td colspan="12" style="border:none;"></td>
            </tr>
            <tr>
                <td colspan="2" align="right">JUMLAH K4:</td>
                <td align="left"><b><?= $rekap['K4'] ?> Orang</b></td>
                <td colspan="12" style="border:none;"></td>
            </tr>
            <tr>
                <td colspan="2" align="right">JUMLAH K6:</td>
                <td align="left"><b><?= $rekap['K6'] ?> Orang</b></td>
                <td colspan="12" style="border:none;"></td>
            </tr>
            <tr>
                <td colspan="2" align="right" style="color:red;">IBU KEK (LILA < 23.5):</td>
                <td align="left" style="color:red;"><b><?= $rekap['KEK'] ?> Orang</b></td>
                <td colspan="12" style="border:none;"></td>
            </tr>
            <tr>
                <td colspan="2" align="right" style="color:red;">ANEMIA (HB < 11):</td>
                <td align="left" style="color:red;"><b><?= $rekap['ANEMIA'] ?> Orang</b></td>
                <td colspan="12" style="border:none;"></td>
            </tr>
        </tbody>
    </table>
</body>
</html>