<?php
// Fungsi kecil untuk mencegah karakter <, >, &, " dan ' menyebabkan error parsing
function safe($text) {
    return htmlspecialchars((string)$text, ENT_QUOTES, 'UTF-8');
}

// Set zona waktu & perhitungan umur
date_default_timezone_set("Asia/Jakarta");
$tgll = date('Y-m-d', strtotime(@$id['TTL']));
$tanggal_lahir = new DateTime($tgll);
$sekarang = new DateTime("today");
$thn = $bln = $tgl = "0";
if ($tanggal_lahir <= $sekarang) {
    $thn = $sekarang->diff($tanggal_lahir)->y;
    $bln = $sekarang->diff($tanggal_lahir)->m;
    $tgl = $sekarang->diff($tanggal_lahir)->d;
}
?>

<page backtop="10mm" backbottom="10mm" backleft="10mm" backright="10mm">
    <style>
        table.identitas {
            width: 100%;
            border-collapse: collapse;
            font-size: 11pt;
        }
        table.identitas td {
            padding: 4px 6px;
            vertical-align: top;
        }
        table.identitas tr td.label { width: 18%; font-weight: bold; }
        table.identitas tr td.titik { width: 2%; }
        table.identitas tr td.nilai { width: 28%; }
        table.identitas tr td.label-kanan { width: 18%; font-weight: bold; padding-left: 25px; }
        table.identitas tr td.titik-kanan { width: 2%; }
        table.identitas tr td.nilai-kanan { width: 32%; }
        .judul { text-align: center; font-weight: bold; font-size: 14pt; margin-bottom: 15px; text-transform: uppercase; }
        .garis { border-bottom: 1px solid #000; margin: 5px 0 15px 0; }
        .page-center { width: 100%; height: 100%; display: table; text-align: center; }
        .page-center-inner { display: table-cell; vertical-align: middle; }
        table { width: 80%; margin: 0 auto; border-collapse: collapse; font-size: 10pt; }
        th, td { padding: 4px 5px; text-align: left; }
        thead th { border-bottom: 1px solid #000; font-weight: bold; }
        tfoot td { border-top: 1px solid #000; text-align: right; font-size: 8pt; }
        .group-header { font-weight: bold; background: #f5f5f5; }
        .footer-text { text-align: right; font-style: italic; }

        table.lab-result {
            width: 90%;               /* bisa ubah 70–90% sesuai kebutuhan */
            margin: 0 auto;           /* ini yang memusatkan tabel di tengah */
            border-collapse: collapse;
        }

        table.lab-result th, table.lab-result td {
            border-bottom: 0.3px solid #999;
            padding: 4px 6px;
            vertical-align: top;
        }

        table.lab-result th {
            background: #f2f2f2;
            text-align: center;
            font-weight: bold;
        }

        .group {
            font-weight: bold;
            text-transform: uppercase;
            padding-top: 6px;
        }

        .footer-text {
            text-align: right;
            font-size: 9pt;
            margin-top: 8px;
        }
    </style>

    <!-- HEADER -->
    <table width="100%" style="font-size:12pt;">
        <tr>
            <td width="20%">
                <img src="<?= base_url('images/logo2.png'); ?>" width="70" height="70" alt="Logo" />
            </td>
            <td>
                <h3 style="margin-top: 2px;"><?= safe($utama['NamaPerusahaan']); ?></h3>
                <p style="margin-top: -10px;"><?= safe($utama['AlamatPerusahaan']); ?></p>
                <p style="margin-top: -10px;">Jakarta</p>
                <p style="margin-top: -10px;"><?= safe($utama['TeleponPerusahaan']); ?>, Fax : <?= safe($utama['FaxPerusahaan']); ?></p>
            </td>
        </tr>
    </table>
    <hr />

    <!-- IDENTITAS PASIEN -->
    <table class="identitas">
        <tr>
            <td>No. MR</td>
            <td>:</td>
            <td style="width: 40%;"><?= safe($id['MR']); ?></td>

            <td>No. Registrasi</td>
            <td>:</td>
            <td><?= safe($id['ID']); ?></td>
        </tr>
        <tr>
            <td>Nama Pasien</td>
            <td>:</td>
            <td><?= safe($id['Nama']); ?></td>

            <td>Tanggal Kunjungan</td>
            <td>:</td>
            <td><?= safe(date('d-m-Y', strtotime($id['Kunjungan']))); ?></td>
        </tr>
        <tr>
            <td>Tanggal Lahir</td>
            <td>:</td>
            <td><?= !empty($id['TTL']) ? safe(date('d-m-Y', strtotime($id['TTL']))) : ''; ?></td>
            <td>Umur</td>
            <td>:</td>
            <td><?= safe($thn . ' th ' . $bln . ' bln ' . $tgl . ' hr'); ?></td>
        </tr>
        <tr>
            <td>Dokter</td>
            <td>:</td>
            <td><?= safe($id['Dokter']); ?></td>
            <td>Penjamin</td>
            <td>:</td>
            <td><?= safe($id['CompanyName'] ?? ''); ?></td>
        </tr>
        <tr>
            <td>Poli / Unit</td>
            <td>:</td>
            <td colspan="4"><?= safe($id['Poli']); ?></td>
        </tr>
    </table>
    <br />
    <hr />

    <!-- HASIL LAB -->
 <div class="judul">Hasil Pemeriksaan Laboratorium</div>

  <table class="lab-result">
        <thead>
            <tr>
                <th width="45%">PEMERIKSAAN</th>
                <th width="25%">HASIL</th>
                <th width="30%">NILAI NORMAL</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($group)) : ?>
                <?php foreach ($group as $g) : ?>
                    <tr>
                        <td class="group" colspan="3"><b><?= strtoupper(safe($g['BahasaLC'])); ?></b></td>
                    </tr>

                    <?php 
                    $filtered = array_filter($viewlab, function($v) use ($g) {
                        return $v['CategoryID'] == $g['CategoryID'];
                    });
                    ?>

                    <?php if (!empty($filtered)) : ?>
                        <?php foreach ($filtered as $d) : ?>
                            <tr>
                                <td style="padding-left:20px;"><?= safe($d['Bahasa']); ?></td>
                                <td style="text-align:center;"><?= safe($d['ResultValue']); ?></td>
                                <td style="text-align:center;"><?= safe($d['Normalvalue']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <tr>
                            <td colspan="3" style="font-style:italic; text-align:center;">Tidak ada data</td>
                        </tr>
                    <?php endif; ?>
                <?php endforeach; ?>
            <?php else : ?>
                <tr><td colspan="3" style="text-align:center;">Tidak ada hasil laboratorium</td></tr>
            <?php endif; ?>
        </tbody>
    </table>

    <div class="footer-text">
        <i>** Dicetak pada <?= date('d-m-Y H:i:s'); ?></i>
    </div>
</page>
