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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'kader') {
    header("Location: ../login.php"); 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"];

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

$query = "SELECT l.nama_lansia, l.nik_lansia, l.jenis_kelamin, 
                 TIMESTAMPDIFF(YEAR, l.tgl_lahir, CURDATE()) AS umur,
                 p.* FROM pemeriksaan_lansia p
          JOIN lansia l ON p.id_lansia = l.id_lansia
          WHERE l.id_posyandu = ? AND MONTH(p.tgl_periksa) = ? AND YEAR(p.tgl_periksa) = ?
          ORDER BY p.tgl_periksa ASC";

$stmt = $pdo->prepare($query);
$stmt->execute([$id_posyandu, $bulan, $tahun]);
$data = $stmt->fetchAll();

// Hitung Ringkasan
$rekap = ['total' => count($data), 'hipertensi' => 0, 'gds' => 0, 'imt' => 0, 'au' => 0, 'kol' => 0];
foreach ($data as $d) {
    $t = explode('/', $d['tensi']);
    if (isset($t[0]) && $t[0] >= 140) $rekap['hipertensi']++;
    if ($d['gula_darah'] > 200) $rekap['gds']++;
    if ($d['imt'] > 0 && ($d['imt'] < 18.5 || $d['imt'] > 25)) $rekap['imt']++;
    if (($d['jenis_kelamin'] == 'L' && $d['asam_urat'] > 7) || ($d['jenis_kelamin'] == 'P' && $d['asam_urat'] > 6)) $rekap['au']++;
    if ($d['kolesterol'] > 200) $rekap['kol']++;
}

// Gunakan .xls agar kompatibel tanpa error format
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=Laporan_Lansia_".$nama_bulan[$bulan]."_".$tahun.".xls");
?>

<style>
    .text-nik { mso-number-format:"\@"; }
    th { background-color: #6f42c1; color: white; border: 0.5pt solid #000; }
    td { border: 0.5pt solid #000; }
    .bg-rekap { background-color: #f2f2f2; font-weight: bold; }
</style>

<table border="1">
    <thead>
        <tr>
            <th colspan="16" style="font-size: 14pt; background: none; color: black; border: none;">LAPORAN PEMERIKSAAN KESEHATAN LANSIA</th>
        </tr>
        <tr>
            <th colspan="16" style="font-size: 11pt; background: none; color: black; border: none;">Posyandu: <?= strtoupper($posyandu['nama_posyandu']) ?> | Periode: <?= $nama_bulan[$bulan] ?> <?= $tahun ?></th>
        </tr>
        <tr></tr>
        <tr>
            <th rowspan="2">No</th>
            <th rowspan="2">Nama Lansia</th>
            <th rowspan="2">NIK</th>
            <th rowspan="2">Umur</th>
            <th rowspan="2">JK</th>
            <th colspan="3">Antropometri</th>
            <th rowspan="2">Tensi</th>
            <th colspan="3">Laboratorium</th>
            <th colspan="2">Status</th>
            <th rowspan="2">Keluhan / Obat</th>
            <th rowspan="2">Tgl Periksa</th>
        </tr>
        <tr>
            <th>BB (kg)</th><th>TB (cm)</th><th>LP (cm)</th>
            <th>GDS</th><th>AU</th><th>Kol</th>
            <th>Mandiri</th><th>Mental</th>
        </tr>
    </thead>
    <tbody>
        <?php $no=1; foreach($data as $d): ?>
        <tr>
            <td align="center"><?=$no++?></td>
            <td><?= strtoupper($d['nama_lansia']) ?></td>
            <td class="text-nik"><?=$d['nik_lansia']?></td>
            <td align="center"><?=$d['umur']?></td>
            <td align="center"><?=$d['jenis_kelamin']?></td>
            <td align="center"><?=$d['bb']?></td>
            <td align="center"><?=$d['tb']?></td>
            <td align="center"><?=$d['lingkar_perut']?></td>
            <td align="center"><?=$d['tensi']?></td>
            <td align="center"><?=$d['gula_darah']?></td>
            <td align="center"><?=$d['asam_urat']?></td>
            <td align="center"><?=$d['kolesterol']?></td>
            <td><?=$d['kemandirian']?></td>
            <td><?=$d['skrining_mental']?></td>
            <td><?=$d['catatan_obat']?></td>
            <td align="center"><?= date('d/m/Y', strtotime($d['tgl_periksa'])) ?></td>
        </tr>
        <?php endforeach; ?>

        <tr></tr>
        <tr class="bg-rekap">
            <td colspan="3">RINGKASAN DATA</td>
            <td colspan="13"></td>
        </tr>
        <tr>
            <td colspan="2">Total Data</td>
            <td align="center"><b><?=$rekap['total']?></b></td>
            <td colspan="13">Orang</td>
        </tr>
        <tr>
            <td colspan="2">Tensi Tinggi</td>
            <td align="center" style="color: red;"><b><?=$rekap['hipertensi']?></b></td>
            <td colspan="13">Orang</td>
        </tr>
        <tr>
            <td colspan="2">GDS > 200</td>
            <td align="center"><b><?=$rekap['gds']?></b></td>
            <td colspan="13">Orang</td>
        </tr>
        <tr>
            <td colspan="2">IMT Abnormal</td>
            <td align="center"><b><?=$rekap['imt']?></b></td>
            <td colspan="13">Orang</td>
        </tr>
        <tr>
            <td colspan="2">Asam Urat Tinggi</td>
            <td align="center"><b><?=$rekap['au']?></b></td>
            <td colspan="13">Orang</td>
        </tr>
        <tr>
            <td colspan="2">Kolesterol > 200</td>
            <td align="center"><b><?=$rekap['kol']?></b></td>
            <td colspan="13">Orang</td>
        </tr>
    </tbody>
</table>