<script>
    window.onload = function() {
        window.print();
        setTimeout(window.close, 500);
    };
</script>
<title>
    <?= $judul; ?>
</title>

<body>

    <table width="100%" style="font-size:12pt;">
        <tr>
            <td width="20%">
                <img src="<?= base_url('images/logo2.png'); ?>" height="40px">
            </td>
            <td>
                <small>
                    <small>
                        <small style="font-size:11pt;"><?= $utama['NamaPerusahaan']; ?></small> <br>
                        <small style="font-size:11pt;"><?= $utama['AlamatPerusahaan']; ?></small>
                    </small>
                </small>
            </td>
        </tr>
    </table>

    <!-- <small><center><b>REKAM MEDIS</b></center></small> -->

    <hr>
    <table width="100%" border="0" style="border-collapse: collapse; font-size:11pt;">
        <tr>
            <td valign="top">Case ID/MR</td>
            <td valign="top">:</td>
            <td valign="top"><?= $pasien['Case ID'] . '/' . $pasien['PatientCode']; ?></td>
        </tr>
        <tr>
            <td valign="top">Nama</td>
            <td valign="top">:</td>
            <td valign="top"><?= $pasien['Description']; ?></td>
        </tr>
        <tr>
            <td valign="top">Gender</td>
            <td valign="top">:</td>
            <td valign="top"><?php
                                if ($pasien['Sex'] == 1) {
                                    echo "P";
                                } else {
                                    echo "L";
                                }; ?></td>
        </tr>
        <tr>
            <td valign="top">Tanggal Lahir</td>
            <td valign="top">:</td>
            <td valign="top"><?php
                                $tglL = date('Y-m-d', strtotime($pasien['DOB']));
                                echo $tglL;
                                ?>
            </td>
        </tr>
        <tr>
            <td valign="top">Umur</td>
            <td valign="top">:</td>
            <td valign="top">
                <?php
                // tanggal lahir

                $tanggal = new DateTime($tglL);

                // tanggal hari ini
                $today = new DateTime('today');

                // tahun
                $y = $today->diff($tanggal)->y;

                // bulan
                $m = $today->diff($tanggal)->m;

                // hari
                $d = $today->diff($tanggal)->d;
                echo "" . $y . " tahun " . $m . " bulan " . $d . " hari";
                ?>
            </td>
        </tr>
    </table>
    <hr>
    <table border="0" style="border-collapse: collapse; width:100mm">
        <tr>
            <td>Berat Badan</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td><?= $pasien['BeratBadan']; ?></td>

            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tinggi Badan</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td><?= $pasien['TinggiBadan']; ?></td>
        </tr>
        <tr>
            <td>RespRate</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td><?= $pasien['respRate']; ?></td>

            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeartRate</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td><?= $pasien['heartRate']; ?></td>
        </tr>
        <tr>
            <td>Tensi</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td><?= $pasien['Tensi']; ?></td>

            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Diastole</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td><?= $pasien['Diastole']; ?></td>
        </tr>
        <tr>
            <td>Suhu</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td><?= $pasien['Suhu']; ?></td>
        </tr>
    </table>
    <hr>
    <table width="100%" border="0" style="border-collapse: collapse; font-size: 11pt;">
        <?php
        if ($countPemeriksaan > 0) {
        ?>
            <tr>
                <td width="100px" valign="top">Poli</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['poli']; ?>
                </td>
            </tr>

            <?php
            if ($hasilpemeriksaan['poli'] == 'POLI KEBIDANAN') {
            ?>
                <tr>
                    <td width="100px" valign="top">Usia Kehamilan</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $usiahamil['Description']; ?>
                    </td>
                </tr>
                <tr>
                    <td width="100px" valign="top">Jenis Imunisasi</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $imunisasi['Description']; ?>
                    </td>
                </tr>
                <tr>
                    <td width="100px" valign="top">Jenis KB</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $kb['Description']; ?>
                    </td>
                </tr>
            <?php } else { ?>
                <tr>
                    <td width="100px" valign="top">Penyakit Terdahulu</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['penyakit_terdahulu']; ?>
                    </td>
                </tr>
            <?php } ?>

            <tr>
                <td width="100px" valign="top">Keluhan</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['keluhan_utama']; ?>
                </td>
            </tr>

            <tr>
                <td width="100px" valign="top">Pemeriksaan Fisik</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['pemeriksaan_fisik']; ?>
                </td>
            </tr>

            <?php
            if ($hasilpemeriksaan['poli'] == 'POLI GIGI') {
            ?>
                <tr>
                </tr>
                <tr>
                    <td colspan="3"><b>Pemeriksaan Gigi :</b></td>
                </tr>
                <tr>
                    <td width="100px" valign="top">Element Gigi</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $pemeriksaangigi['elemenGigi']; ?>
                    </td>
                </tr>
                <tr>
                    <td width="100px" valign="top">Diagnosa</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $pemeriksaangigi['diagnosa']; ?>
                    </td>
                </tr>
                <tr>
                    <td width="100px" valign="top">Prosedur</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $pemeriksaangigi['prosedur']; ?>
                    </td>
                </tr>
            <?php } else { ?>

            <?php } ?>
            <tr>
                <td width="100px" valign="top">Diagnosa</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['diagnosa_primer'] . ' (' . $hasilpemeriksaan['DESCRPTION'] . ')'; ?>
                </td>
            </tr>
            <?php if ($hasilpemeriksaan['diagnosa_sekunder'] == Null) { ?>
                <tr>
                    <td width="100px" valign="top">Diagnosa Sekunder</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                    </td>
                </tr>
            <?php } else { ?>

                <tr>
                    <td width="100px" valign="top">Diagnosa Sekunder</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['diagnosa_sekunder'] . ' (' . $hasilpemeriksaan['diagsekunder'] . ')'; ?>
                    </td>
                </tr>

            <?php } ?>

            <?php if ($hasilpemeriksaan['poli'] == 'POLI KEBIDANAN') { ?>
                <tr>
                    <td width="100px" valign="top">Panel Diagnosa</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['paneldiag']; ?>
                    </td>
                </tr>
            <?php } else { ?>

            <?php } ?>
            <tr>
                <td width="100px" valign="top">Rencana Tindakan</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['rencana_tindakan']; ?>
                </td>
            </tr>
            <tr>
                <td width="100px" valign="top">Tindak Lanjut</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['tindak_lanjut']; ?>
                </td>
            </tr>
            <tr>
                <td width="100px" valign="top">Terapi Obat</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <pre><?= $hasilpemeriksaan['obat']; ?></pre>
                </td>
            </tr>
            <tr>
                <td>Non-Racikan</td>
                <td>:</td>
                <?php foreach ($nonracik as $nr) : ?>
                    <td><?= $nr['Description'] . ','; ?>
                    <?php endforeach; ?>
                    </td>
            </tr>
            <tr>
                <td>Racikan</td>
                <td>:</td>
                <?php foreach ($racikan as $r) : ?>
                    <td><?= $r['Description'] . ','; ?>
                    <?php endforeach; ?>
                    </td>
            </tr>
            <?php if (!empty($hasilpemeriksaan['tanggal_kontrol'] && $hasilpemeriksaan['tanggal_kontrol'] != '1900-01-01')) { ?>
                <tr>
                    <td width="100px" valign="top">Tanggal Kontrol</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['tanggal_kontrol']; ?>
                    </td>
                </tr>
            <?php } ?>

            <?php if (!empty($hasilpemeriksaan['tanggal_meninggal'] && $hasilpemeriksaan['tanggal_meninggal'] != '1900-01-01')) { ?>
                <tr>
                    <td width="100px" valign="top">Tanggal Meninggal</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['tanggal_meninggal']; ?>
                    </td>
                </tr>
            <?php } ?>

            <?php if (!empty($hasilpemeriksaan['poli_tujuan'])) { ?>
                <tr>
                    <td width="100px" valign="top">Poli Tujuan</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['poli_tujuan']; ?>
                    </td>
                </tr>
            <?php } ?>

            <?php if (!empty($hasilpemeriksaan['instansi_tujuan'])) { ?>
                <tr>
                    <td width="100px" valign="top">Instansi Tujuan</td>
                    <td align="center" valign="top">:</td>
                    <td valign="top">
                        <?= $hasilpemeriksaan['instansi_tujuan']; ?>
                    </td>
                </tr>
            <?php } ?>

            <tr>
                <td width="100px" valign="top">Catatan</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['catatan']; ?>
                </td>
            </tr>
            <tr>
                <td width="100px" valign="top">Alergi</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilpemeriksaan['alergi']; ?>
                </td>
            </tr>
        <?php
        }
        ?>

        <?php
        if ($countDiagnosa > 0) {
        ?>
            <tr>
                <td valign="top">Diagnosa</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilDiagnosa['ICD_Code'] . '-' . $hasilDiagnosa['DESCRPTION']; ?>
                </td>
            </tr>
        <?php
        }
        ?>

        <?php
        if ($countRujukan > 0) {
        ?>
            <tr>
                <td valign="top">Rujukan</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilRujukan['Rujukan']; ?>
                </td>
            </tr>
            <tr>
                <td valign="top">Poli</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilRujukan['Poli']; ?>
                </td>
            </tr>
        <?php
        }
        ?>

        <?php
        if ($countTindakan > 0) {
        ?>
            <tr>
                <td valign="top">Tindakan</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?= $hasilTindakan['Tindakan']; ?>
                </td>
            </tr>
        <?php
        }
        ?>

        <?php
        if ($countLaboratory > 0) {
        ?>
            <tr>
                <td valign="top">Tindakan Lab</td>
                <td align="center" valign="top">:</td>
                <td valign="top">
                    <?php
                    if ($hasilLaboratory['Hematologi'] == 1) {
                        echo "Hematologi ,";
                    }
                    if ($hasilLaboratory['Serologi'] == 1) {
                        echo "Serologi ,";
                    }
                    if ($hasilLaboratory['KimiaDarah'] == 1) {
                        echo "KimiaDarah ,";
                    }
                    if ($hasilLaboratory['Urinalisa'] == 1) {
                        echo "Urinalisa ,";
                    }
                    if ($hasilLaboratory['FungsiGinjal'] == 1) {
                        echo "FungsiGinjal ,";
                    }
                    if ($hasilLaboratory['Lemak'] == 1) {
                        echo "Lemak ,";
                    }
                    if ($hasilLaboratory['TestKehamilan'] == 1) {
                        echo "TestKehamilan ,";
                    }
                    if ($hasilLaboratory['Lainnya'] == 1) {
                        echo "Lainnya ,";
                    }
                    if ($hasilLaboratory['DarahLengkap'] == 1) {
                        echo "DarahLengkap ";
                    }
                    ?>
                </td>
            </tr>
        <?php
        }
        ?>

        <?php
        if ($countObat > 0) {
        ?>
            <tr>
                <td colspan="3"><br></td>
            </tr>
            <tr>
                <td valign="top" colspan="3">Resep Obat</td>
            </tr>
            <tr>
                <td colspan="3">
                    <table border='1' width="90%" style="border-collapse: collapse; font-size: 6pt;">
                        <tr>
                            <td>No.</td>
                            <td>Obat</td>
                            <td>Jumlah</td>
                            <td>Satuan</td>
                            <td>Dosis</td>
                            <td>Cara Pakai</td>
                        </tr>
                        <?php
                        $no = 1;
                        foreach ($hasilObat as $obat) :
                        ?>
                            <tr>
                                <td><?= $no++; ?>.</td>
                                <td><?= $obat['Obat']; ?></td>
                                <td><?= $obat['Jumlah']; ?></td>
                                <td><?= $obat['Satuan']; ?></td>
                                <td><?= $obat['Dosis']; ?></td>
                                <td><?= $obat['CaraPakai']; ?></td>
                            </tr>
                        <?php
                        endforeach;
                        ?>
                    </table>
                </td>
            </tr>
        <?php
        }
        ?>
    </table>
    <hr>
    <br>
    <?php
    $case = $this->uri->segment(3);
    $squery = $this->db->query("Select * from DetailCppt where CaseId = '$case'")->result_array();
    $countcppt = $this->db->query("Select * from DetailCppt where CaseId = '$case'")->num_rows();
    if ($countcppt > 0) {
    ?>
        <table align="center" width="90%" style="font-size: 9pt;" border="1">
            <tr>
                <th colspan="3">Riwayat Cppt</th>
            </tr>
            <tr>
                <th>Tanggal & Waktu</th>
                <th>UserCreat</th>
                <th>Description</th>
            </tr>
            <?php foreach ($squery as $sd) : ?>
                <tr>
                    <tbody>
                        <td><?= date('d-m-Y', strtotime($sd['DateDetail'])) . '/' . date('H:i', strtotime($sd['TimeDetail'])) ?></td>
                        <td><?= $sd['UserCreated']; ?></td>
                        <td><?= $sd['Description']; ?></td>
                    </tbody>
                </tr>
            <?php endforeach; ?>
        </table>
        <br><br>
        <table align="center" width="90%" style="font-size: 7pt;">
            <tr align="center">
                <td>
                    <?php
                    date_default_timezone_set("Asia/Jakarta");
                    echo $lokasi . ', ' . date("d/m/Y", strtotime($pasien['Intake Date'])); ?><br>
                    Mengetahui,
                    <br><br><br>
                    <?= $pasien['doctorName']; ?>
                </td>
                <td></td>
                <td>
                    <br>
                    Pasien,
                    <br><br><br>
                    <?= $pasien['Description']; ?>
                </td>
            </tr>
        </table>
    <?php } ?>

    <br><br>
    <table align="center" width="90%" style="font-size: 7pt;">
        <tr align="center">
            <td>
                <?php
                date_default_timezone_set("Asia/Jakarta");
                echo $lokasi . ', ' . date("d/m/Y", strtotime($pasien['Intake Date'])); ?><br>
                Mengetahui,
                <br><br><br>
                <?= $pasien['doctorName']; ?>
            </td>
            <td></td>
            <td>
                <br>
                Pasien,
                <br><br><br>
                <?= $pasien['Description']; ?>
            </td>
        </tr>
    </table>

</body>