<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Pelayanan Bpjs
            <small><?= $abdulradjak['NamaPerusahaan']; ?></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Klinik</a></li>
            <li class="active">Peserta BPJS</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="box">
            <div class="box-body">
                <?php
                date_default_timezone_set("Asia/Jakarta");
                $tgll = date('Y-m-d', strtotime(@$id['TTL']));
                $tanggal_lahir = new DateTime($tgll);
                $sekarang = new DateTime("today");
                if ($tanggal_lahir > $sekarang) {
                    $thn = "0";
                    $bln = "0";
                    $tgl = "0";
                }
                $thn = $sekarang->diff($tanggal_lahir)->y;
                $bln = $sekarang->diff($tanggal_lahir)->m;
                $tgl = $sekarang->diff($tanggal_lahir)->d;

                $c = $id['CompanyCode'];
                $com = $this->db->query("SELECT * FROM Company WHERE CompanyCode = '$c'")->row_array();
                $case = $id['ID'];
                $idS = $this->uri->segment(3);
                $dList = $this->db->query("SELECT * FROM [Activity Apotik] AA, Pharmacy PC WHERE AA.[DrugsID]=PC.PharmacyCode AND [TransactionID] = '$idS'")->result_array();
                ?>
                <table border="0" class="table table-bordered table-striped dataTable dtr-inline">
                    <tr>
                        <td>No.MR</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $id['MR']; ?></td>

                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;No.Reg</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $id['ID']; ?></td>
                    </tr>
                    <tr>
                        <td>Nama</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $id['Nama']; ?></td>

                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tanggal Kunjungan</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= date('d-m-Y', strtotime($id['Kunjungan'])); ?></td>
                    </tr>
                    <tr>
                        <td>Tanggal Lahir</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td>
                            <?php
                            if ($id['TTL'] == '') {
                            } else {
                                echo date('d-m-Y', strtotime($id['TTL']));
                            }
                            ?>
                        </td>

                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Umur</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $thn . ' th ' . $bln . ' bulan ' . $tgl . ' hari'; ?></td>
                    </tr>
                    <tr>
                        <td>Dokter Praktek</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $id['Dokter']; ?></td>

                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pembayar</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $com['CompanyName']; ?></td>
                    </tr>
                    <tr>
                        <td>Poli</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $id['Poli']; ?></td>

                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dokter Praktek</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                        <td><?= $id['Dokter']; ?></td>
                    </tr>
                </table>
                <hr>
                <form action="<?= base_url('Pendaftaran/prosespelayananbpjs'); ?>" method="post">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="tab-content">
                                <div class="row">
                                    <div class="col-md-6">
                                        <table width="100%" class="table">
                                            <?php foreach ($hasilpemeriksaan as $hasil) : ?>
                                                <tr>
                                                    <td width="25%">
                                                        Dokter Tujuan
                                                    </td>
                                                    <td>
                                                        <?php
                                                        // $getdokter = [
                                                        //     ['namadokter' => 'ZULFIKAR UMAR', 'kodedokter' => 246185, 'jampraktek' => '00:00-08:59'],
                                                        //     ['namadokter' => 'KUNANG KUNANG PERMATA BULAN', 'kodedokter' => 284025, 'jampraktek' => '09:00-23:59']
                                                        // ];
                                                        date_default_timezone_set('Asia/Jakarta');
                                                        $jamSekarang = date('H:i');
                                                        $today = date('Y-m-d');
                                                        $now = strtotime("$today $jamSekarang");
                                                        ?>
                                                        <select name="doktertujuan" class="form-control" required>
                                                            <?php
                                                            foreach ($getdokter as $d):
                                                                list($a, $b) = explode('-', $d['jampraktek']);
                                                                $start = strtotime("$today $a");
                                                                $end = strtotime("$today $b");
                                                                $sel = ($now >= $start && $now <= $end) ? ' selected="selected"' : '';
                                                               echo "<option value='{$d['kodedokter']}'{$sel}>{$d['namadokter']} ({$d['jampraktek']})</option>";
                                                            endforeach;
                                                            ?>
                                                        </select>
                                                        <p>Jam Sekarang : <?= $jamSekarang ?> Wib</p>
                                                        <input type="hidden" name="kartu" value="<?= $id["EmployeeID"] ?>">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Tenaga Medis
                                                    </td>
                                                    <td>
                                                        <select name="tenagamedis" id="tenagamedis" class="form-control" required>
                                                            <?php
                                                            foreach ($getdokter as $d):
                                                                list($a, $b) = explode('-', $d['jampraktek']);
                                                                $start = strtotime("$today $a");
                                                                $end = strtotime("$today $b");
                                                                $sel = ($now >= $start && $now <= $end) ? ' selected="selected"' : '';
                                                              echo "<option value='{$d['kodedokter']}'{$sel}>{$d['namadokter']} ({$d['jampraktek']})</option>";
                                                            endforeach;
                                                            ?>
                                                        </select>
                                                        <p>Jam Sekarang : <?= $jamSekarang ?> Wib</p>
                                                        <input type="hidden" name="kartu" value="<?= $id["EmployeeID"] ?>">
                                                    </td>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Keluhan
                                                    </td>
                                                    <td>
                                                        <textarea class="form-control" name="keluhan"><?= $hasil["Keluhan"] ?></textarea>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Terapi
                                                    </td>
                                                    <td>
                                                        <textarea class="form-control" name="terapi" required><?= $terapi ?></textarea>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Terapi Non Obat
                                                    </td>
                                                    <td>
                                                        <textarea class="form-control" id="myTextarea" minlength="6" id="myTextarea" name="terapinonobat" required><?= $terapi ?></textarea>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        LingkarPerut
                                                    </td>
                                                    <td>
                                                        <input class="form-control" name="lingkarperut" class="form-control" value="<?= $hasil['lingkarperut']; ?> " required>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Diagnosa 1
                                                    </td>
                                                    <td>
                                                        <input style="display:none;" type="text" readonly class="form-control" id="record" name="record" value="<?= $record; ?>">
                                                        <input type="text" readonly class="form-control" id="txtCariDiagnosa3" placeholder="Pilih Diagnosa 1" value="<?= $diagnosa1["Description"]; ?>">
                                                        <input type="hidden" name="diag1" class="form-control" value="<?= $diagnosa1["diagnosa_primer"]; ?>">
                                                        <input type="hidden" name="txtCariIdDiagnosa3" id="txtCariIdDiagnosa3">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Diagnosa 2
                                                    </td>
                                                    <td>
                                                        <input type="text" readonly class="form-control" id="txtCariDiagnosa1" placeholder="Pilih Diagnosa 2" value="<?= $diagnosa2["Description"]; ?>">
                                                        <input type="hidden" name="diag2" class="form-control" value="<?= $diagnosa2["diagnosa_sekunder"]; ?>">
                                                        <input type="hidden" name="txtCariIdDiagnosa1" id="txtCariIdDiagnosa1">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Diagnosa 3
                                                    </td>
                                                    <td>
                                                        <input type="text" readonly class="form-control" id="txtCariDiagnosa2" placeholder="Pilih Diagnosa 3">
                                                        <input type="hidden" name="txtCariIdDiagnosa2" id="txtCariIdDiagnosa2">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>Kesadaran</td>
                                                    <td>
                                                        <select name="kesadaran" class="form-control" required>
                                                            <option value="">- PILIH KESADARAN -</option>
                                                            <option selected value="01">Compos Mentis</option>
                                                            <option value="02">Somnolence</option>
                                                            <option value="03">Sopor</option>
                                                            <option value="04">Coma</option>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>Status Pulang</td>
                                                    <td>
                                                        <select name="statuspulang" id="statuspulang" onchange="cekstatusrujuk()" class="form-control" required>
                                                            <option value="">- PILIH STATUS -</option>
                                                            <?php foreach ($getpulang as $pulang) : ?>
                                                                <option value="<?= $pulang['kdStatusPulang'] . '-' . $pulang['nmStatusPulang']; ?>"><?= $pulang['nmStatusPulang']; ?></option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr id="rowAlergi">
                                                    <td>Alergi Makan</td>
                                                    <td>
                                                        <select required id="subAlergiMakan" name="subAlergiMakan" class="form-control">
                                                            <option value="">- PILIH ALERGI MAKAN -</option>
                                                            <?php foreach ($alergiMakan as $alergiMakan) : ?>
                                                                <option value="<?= $alergiMakan['kdAlergi'] . '-' . $alergiMakan['nmAlergi']; ?>"><?= $alergiMakan['nmAlergi']; ?></option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr id="rowAlergi">
                                                    <td>Alergi Obat</td>
                                                    <td>
                                                        <select required id="subAlergiObat" name="subAlergiObat" class="form-control">
                                                            <option value="">- PILIH ALERGI OBAT -</option>
                                                            <?php foreach ($alergiObat as $alergiObat) : ?>
                                                                <option value="<?= $alergiObat['kdAlergi'] . '-' . $alergiObat['nmAlergi']; ?>"><?= $alergiObat['nmAlergi']; ?></option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr id="rowAlergi">
                                                    <td>Alergi Udara</td>
                                                    <td>
                                                        <select required id="subAlergiUdara" name="subAlergiUdara" class="form-control">
                                                            <option value="">- PILIH ALERGI UDARA -</option>
                                                            <?php foreach ($alergiUdara as $alergiUdara) : ?>
                                                                <option value="<?= $alergiUdara['kdAlergi'] . '-' . $alergiUdara['nmAlergi']; ?>"><?= $alergiUdara['nmAlergi']; ?></option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>Prognosa</td>
                                                    <td>
                                                        <select id="prognosa" name="prognosa" class="form-control" required>
                                                            <option value="">- PILIH PROGNOSA -</option>
                                                            <?php foreach ($getprognosa as $prognosa) : ?>
                                                                <option selected value="<?= $prognosa['kdPrognosa'] . '-' . $prognosa['nmPrognosa']; ?>"><?= $prognosa['nmPrognosa']; ?></option>
                                                            <?php endforeach; ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                        </table>
                                    </div>
                                    <div class="col-md-6">
                                        <table width="100%" class="table">
                                            <tr>
                                                <td width="25%">
                                                    Poli Tujuan
                                                </td>
                                                <td>
                                                    <select name="politujuan" class="teanganData" data-live-search="true" require>
                                                        <?php
                                                        foreach ($poli as $p) :
                                                            if ($id['Poli'] == $p['Jenis']) {
                                                        ?>
                                                                <option selected value="<?= $p['KodePoli']; ?>"><?= $p['Jenis']; ?></option>
                                                            <?php
                                                            } else {
                                                            ?>
                                                                <option value="<?= $p['KodePoli']; ?>"><?= $p['Jenis']; ?></option>
                                                        <?php
                                                            }
                                                        endforeach;
                                                        ?>
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Tinggi Badan
                                                </td>
                                                <td>
                                                    <input type="text" name="tinggibadan" class="" value="<?= $hasil["TinggiBadan"] ?>" size="8"> Cm
                                                    <input type="hidden" name="caseid" class="form-control" value="<?= $hasil["Case ID"]; ?>">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Berat Badan
                                                </td>
                                                <td>
                                                    <input type="text" class="" name="beratbadan" value="<?= $hasil["BeratBadan"] ?>" size="8"> Kg
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Suhu
                                                </td>
                                                <td>
                                                    <input type="text" class="" name="suhu" value="<?= $hasil["Suhu"] ?>" size="8"> Celcius
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Tensi
                                                </td>
                                                <td>
                                                    <input type="text" class="" name="tensi" value="<?= $hasil["Tensi"] ?>"> / <input type="text" name="diastole" class="" value="<?= $hasil["Diastole"] ?>" size="4"> mmHg
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Resp. Rate
                                                </td>
                                                <td>
                                                    <input type="number" min="5" max="70" name="resprate" class="" value="<?= $hasil["respRate"] ?>" placeholder="5-70" size="8"> per Minute
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Heart Rate
                                                </td>
                                                <td>
                                                    <input type="text" class="" name="heartrate" value="<?= $hasil["heartRate"] ?>" size="8"> bpm
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    BMHP
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control" name="bmhp" value="" required>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Hasil Pemeriksaan Min. 10 Huruf
                                                </td>
                                                <td>
                                                    <input type="text" minlength="10" class="form-control" name="anamnesa" value="<?= $hasil["pemeriksaan_fisik"] ?>" required>
                                                </td>
                                            </tr>
                                            <tr></tr>
                                            <tr></tr>
                                            <tr>
                                                <td></td>
                                                <td align="right">
                                                    <a href="" class="btn btn-sm btn-danger bg-red-gradient"><i class="fa fa-fw fa-undo"></i> Reset</a>
                                                    <button type="submit" class="btn btn-sm btn-success bg-green-gradient"> <?php if (empty($record)) {
                                                                                                                                echo 'SIMPAN';
                                                                                                                            } else {
                                                                                                                                echo 'UPDATE';
                                                                                                                            } ?> <i class="fa fa-save fa-fw"></i></button>
                                                </td>
                                            </tr>
                                        </table>
                                        <table id="carirujukan" width="100%" class="table">
                                            <tr>
                                                <td>
                                                    <button type="button" class="btn btn-sm btn-default" data-toggle="modal" data-target="#myModal">
                                                        Cari Rujukan
                                                    </button>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>KDPPK</td>
                                                <td>:</td>
                                                <td>
                                                    <p id="selectedkdppk"><?php echo $rujukan['KodePPK']; ?></p>
                                                </td>
                                                <td><input type="hidden" value="<?php echo $rujukan['KodePPK']; ?>" name="kdppkrujukan" id="inputkdppk"></td>
                                            </tr>
                                            <tr>
                                                <td>PPK Rujukan</td>
                                                <td>:</td>
                                                <td>
                                                    <p id="selectedPpkRujukan"><?php echo $rujukan['DestName']; ?></p>
                                                </td>
                                                <td><input type="hidden" name="ppkrujukan" value="<?php echo $rujukan['DestName']; ?>" id="inputPpkRujukan"></td>
                                            </tr>
                                            <tr>
                                                <td>Spesialis</td>
                                                <td>:</td>
                                                <td>
                                                    <div id="selectedspesialis"><?php echo $rujukan['DestPoliName']; ?></div>
                                                </td>
                                                <td><input type="hidden" value="<?php echo $rujukan['DestPoliName']; ?>" name="spesialis" id="inputspesialis"></td>
                                                <td><input type="hidden" value="<?php echo $rujukan['DestPoliCode']; ?>" id="kdspesialis" name="kdspesialis"></td>
                                            </tr>
                                            <tr>
                                                <td>SubSpesialis</td>
                                                <td>:</td>
                                                <td>
                                                    <p id="selectedsubspesialis"></p>
                                                </td>
                                                <td><input type="hidden" name="subspesialis" id="inputsubspesialis"></td>
                                                <td><input type="hidden" id="kdsubspesialis" name="kdsubspesialis"></td>
                                            </tr>
                                            <tr>
                                                <td>Catatan</td>
                                                <td>:</td>
                                                <td>
                                                    <p id="inputnote"><?php echo $rujukan['Note']; ?></p>
                                                </td>
                                                <td><input type="hidden" value="<?php echo $rujukan['Note']; ?>" name="catatan" id=""></td>
                                            </tr>
                                            <tr>
                                                <td>Tgl Rencana</td>
                                                <td>:</td>
                                                <td>
                                                    <p id="selectedtglrujuk"><?php echo $rujukan['TglBerkunjung']; ?></p>
                                                </td>
                                                <td><input type="hidden" value="<?php echo $rujukan['TglBerkunjung']; ?>" name="tglrujuk" id="inputtglrujuk"></td>
                                            </tr>
                                            <tr>
                                                <td>Jadwal</td>
                                                <td>:</td>
                                                <td>
                                                    <p id="selectedjadwal"><?php echo $rujukan['JadwalPraktek']; ?></p>
                                                </td>
                                                <td><input type="hidden" value="<?php echo $rujukan['JadwalPraktek']; ?>" name="jadwal" id="inputjadwal"></td>
                                            </tr>
                                        </table>
                                    <?php endforeach; ?>
                </form>
            </div>
        </div>
</div>
</div>
</div>
</div>
<!-- -->

</div>

</section><!-- /.content -->

<!-- Modal Cari Diagnosa -->
<div class="modal fade" id="ModalCariDiagnosa3" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Daftar Diagnosa 1</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <input type="text" class="form-control col-lg-12" id="textCariDiagnosa3" placeholder="Ketik Nama Diagnosa">
                    <table class="table table-hover mt-3">
                        <thead>
                            <tr>
                                <th scope="col" width="50%">Kode Diagnosa</th>
                                <th scope="col" width="40%">Nama Diagnosa</th>
                                <th scope="col" width="10%">Action</th>
                            </tr>
                        </thead>
                        <tbody id="ListDiagnosa3">

                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
            </div>
        </div>
    </div>
</div>
<!-- End of Modal Cari Diagnosa -->

<!-- Modal Cari Diagnosa -->
<div class="modal fade" id="ModalCariDiagnosa1" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Daftar Diagnosa 2</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <input type="text" class="form-control col-lg-12" id="textCariDiagnosa1" placeholder="Ketik Nama Diagnosa">
                    <table class="table table-hover mt-3">
                        <thead>
                            <tr>
                                <th scope="col" width="50%">Kode Diagnosa</th>
                                <th scope="col" width="40%">Nama Diagnosa</th>
                                <th scope="col" width="10%">Action</th>
                            </tr>
                        </thead>
                        <tbody id="ListDiagnosa1">

                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
            </div>
        </div>
    </div>
</div>
<!-- End of Modal Cari Diagnosa -->

<!-- Modal Cari Diagnosa -->
<div class="modal fade" id="ModalCariDiagnosa2" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Daftar Diagnosa 3</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <input type="text" class="form-control col-lg-12" id="textCariDiagnosa2" placeholder="Ketik Nama Diagnosa">
                    <table class="table table-hover mt-3">
                        <thead>
                            <tr>
                                <th scope="col" width="50%">Kode Diagnosa</th>
                                <th scope="col" width="40%">Nama Diagnosa</th>
                                <th scope="col" width="10%">Action</th>
                            </tr>
                        </thead>
                        <tbody id="ListDiagnosa2">

                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
            </div>
        </div>
    </div>
</div>
<!-- End of Modal Cari Diagnosa -->


<!-- Modal Rujukan -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">Cari Rujukan</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <!-- Modal Body -->
            <div class="modal-body">
                <form action="<?= base_url('Pendaftaran/CariRujukan') ?>" method="post" id="rujukanForm">
                    <table class="tabel" width="100%">
                        <tr>
                            <td>Tgl Rencana Berkunjung</td>
                            <td>
                                <input type="date" id="tglrujuk" name="TglRencana" class="form-control" value="<?= date('Y-m-d'); ?>"><br>
                            </td>
                        </tr>
                        <tr>
                            <td>Cari Spesialis</td>
                            <td>
                                <select name="spesialis" id="spesialis" class="form-control" required>
                                    <option value="">- PILIH SPESIALIS -</option>
                                    <?php foreach ($getspesialis as $spesialis) : ?>
                                        <option value="<?= $spesialis['kdSpesialis']; ?>"><?= $spesialis['nmSpesialis']; ?></option>
                                    <?php endforeach; ?>
                                </select><br>
                            </td>
                        </tr>
                        <tr>
                            <td>Sub Spesialis</td>
                            <td>
                                <select id="subspesialis" name="subspesialis" class="form-control" required>
                                    <option value="">- PILIH SUBSPESIALIS -</option>
                                </select>
                                <br>
                            </td>
                        </tr>
                        <tr>
                            <td>Sarana</td>
                            <td>
                                <select name="sarana" id="spesialis" class="form-control teanganData" data-live-search="true">
                                    <option value="">- PILIH SARANA -</option>
                                    <?php foreach ($getsarana as $sarana) : ?>
                                        <option value="<?= $sarana['kdSarana']; ?>"><?= $sarana['nmSarana']; ?></option>
                                    <?php endforeach; ?>
                                </select><br><br>
                            </td>
                        </tr>
                        <tr>
                            <td>Catatan</td>
                            <td>
                                <textarea name="catatan" id="note" class="form-control"></textarea>
                                <br>
                            </td>
                        </tr>
                        <tr>
                            <td>Cari Rujukan</td>
                            <td>
                                <button type="submit" class="btn btn-primary">Cari Rujukan</button>
                            </td>
                        </tr>
                    </table><br>
                    <table class="table table-bordered example" id="resultsTable">
                        <thead>
                            <tr>
                                <th>KodePPK</th>
                                <th>Faskes</th>
                                <th>Kelas</th>
                                <th>Kantor Cabang</th>
                                <th>Alamat</th>
                                <th>Telepon</th>
                                <th>Total Rujukan</th>
                                <th>Kapasitas</th>
                                <th>Jadwal</th>
                                <th>Persentase</th>
                                <th>Pilih</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </form>
            </div>
            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
</div>

</div><!-- /.content-wrapper -->

<script>
    document.getElementById("doktertujuan").value = "<?php echo $hasilpemeriksaan[0]['KodeAlias']; ?>";
    document.getElementById("tenagamedis").value = "<?php echo $hasilpemeriksaan[0]['KodeAlias']; ?>";

    var record = "<?php echo $record; ?>";

    if (record != '') {
        document.getElementById("statuspulang").value = "<?php echo $hasilpemeriksaan[0]['statusPulang']; ?>";
        document.getElementById("subAlergiMakan").value = "<?php echo $hasilpemeriksaan[0]['alergiMakan']; ?>";
        document.getElementById("subAlergiObat").value = "<?php echo $hasilpemeriksaan[0]['alergiObat']; ?>";
        document.getElementById("subAlergiUdara").value = "<?php echo $hasilpemeriksaan[0]['alergiUdara']; ?>";
        document.getElementById("prognosa").value = "<?php echo $hasilpemeriksaan[0]['prognosa']; ?>";
    } else {
        document.getElementById("statuspulang").value = "3";
        document.getElementById("subAlergiMakan").value = "00-Tidak Ada";
        document.getElementById("subAlergiObat").value = "00-Tidak Ada";
        document.getElementById("subAlergiUdara").value = "00-Tidak Ada";
        document.getElementById("prognosa").value = "01-Sanam (Sembuh)";
    }

    cekstatusrujuk();

    function cekstatusrujuk() {
        var statuspulang = document.getElementById("statuspulang").value;
        if (statuspulang == "4-Rujuk Vertikal") {
            document.getElementById("carirujukan").style.display = "block";
        } else {
            document.getElementById("carirujukan").style.display = "none";
        }
    }
</script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
    $(document).ready(function() {
        $('#spesialis').change(function() {
            var kdSpesialis = $(this).val();
            if (kdSpesialis != '') {
                $.ajax({
                    url: "<?= base_url(); ?>pendaftaran/get_subspesialis",
                    method: "POST",
                    data: {
                        kdSpesialis: kdSpesialis
                    },
                    success: function(data) {
                        $('#subspesialis').html('<option value="">- PILIH SUBSPESIALIS -</option>');
                        var subspesialis = JSON.parse(data);
                        $.each(subspesialis, function(index, sub) {
                            $('#subspesialis').append('<option value="' + sub.kdSubSpesialis + '">' + sub.nmSubSpesialis + '</option>');
                        });
                    }
                });
            } else {
                $('#subspesialis').html('<option value="">- PILIH SUBSPESIALIS -</option>');
            }
        });
    });
</script>
<script>
    $(document).ready(function() {
        $('#rujukanForm').on('submit', function(e) {
            e.preventDefault();

            $.ajax({
                url: "<?= site_url('pendaftaran/carirujukanfaskes'); ?>",
                type: "POST",
                data: $(this).serialize(),
                dataType: "json",
                success: function(data) {
                    if (data.metaData.code == 200) {
                        var results = data.response.list.list;
                        var rows = '';
                        $.each(results, function(index, result) {
                            rows += '<tr>' +
                                '<td>' + result.kdppk + '</td>' +
                                '<td>' + result.nmppk + '</td>' +
                                '<td>' + result.kelas + '</td>' +
                                '<td>' + result.nmkc + '</td>' +
                                '<td>' + result.alamatPpk + '</td>' +
                                '<td>' + result.telpPpk + '</td>' +
                                '<td>' + result.jmlRujuk + '</td>' +
                                '<td>' + result.kapasitas + '</td>' +
                                '<td>' + result.jadwal + '</td>' +
                                '<td>' + result.persentase + '</td>' +
                                '<td><button type="button" class="btn btn-primary pilih-btn" data-kdppk="' + result.kdppk + '"  data-ppk="' + result.nmppk + '" data-spesialis="' + result.kelas + '" data-catatan="' + result.alamatPpk + '" data-tanggal="' + $('input[name="tanggalEstimasiRujuk"]').val() + '" data-jadwal="' + result.jadwal + '">Pilih</button></td>' +
                                '</tr>';
                        });
                        $('#resultsTable tbody').html(rows);
                        $('#resultsModal').modal('show');
                    } else {
                        alert('Error: ' + data.metaData.message);
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert('Error: ' + textStatus + ' - ' + errorThrown);
                }
            });
        });

        //
        $(document).on('click', '.pilih-btn', function() {
            var kdppk = $(this).data('kdppk');
            var ppkRujukan = $(this).data('ppk');
            var spesialis = $(this).data('spesialis');
            var catatan = $(this).data('catatan');
            var tanggal = $(this).data('tanggal');
            var jadwal = $(this).data('jadwal');
            var selectedspesialis = $('#spesialis').find('option:selected').text()
            var inputkdspesialis = $('#spesialis').find('option:selected').val()
            var inputkdsubspesialis = $('#subspesialis').find('option:selected').val()
            var selectedsubspesialis = $('#subspesialis').find('option:selected').text()
            var tglrujuk = $('#tglrujuk').val()
            var note = $('#note').val()

            // alert(ppkRujukan);
            var formattedDate = moment(tglrujuk).format('DD-MM-YYYY');
            $('#selectedkdppk').text(kdppk);
            $('#inputkdppk').val(kdppk);
            $('#kdspesialis').val(inputkdspesialis);
            $('#kdsubspesialis').val(inputkdsubspesialis);
            $('#inputnote').text(note);
            $('#selectedjadwal').text(jadwal);
            $('#inputjadwal').val(jadwal);
            $('#selectedtglrujuk').text(formattedDate);
            $('#inputtglrujuk').val(formattedDate);
            $('#selectedspesialis').text(selectedspesialis);
            $('#inputspesialis').val(selectedspesialis);
            $('#selectedsubspesialis').text(selectedsubspesialis);
            $('#inputsubspesialis').val(selectedsubspesialis);
            $('#selectedPpkRujukan').text(ppkRujukan);
            $('#inputPpkRujukan').val(ppkRujukan);
            $('#selectedSpesialis').text(spesialis);
            $('#selectedSpesialis').val(spesialis);
            $('#selectedCatatan').text(catatan);
            $('#inputCatatan').val(catatan);
            $('#selectedTanggal').text(tanggal);
            $('#inputTanggal').val(tanggal);

            $('#myModal').modal('hide');
        });
    });
</script>

<script>
    function validateTextarea() {
        var textarea = document.getElementById("myTextarea");
        if (textarea.value.length < 6) {
            alert("Textarea harus diisi minimal 6 karakter.");
            return false; // Mencegah form submission
        }
        return true; // Melanjutkan form submission jika validasi lulus
    }
</script>

<!-- SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<?php
$success = $this->session->flashdata('success');
$error   = $this->session->flashdata('error');
?>

<script>
document.addEventListener('DOMContentLoaded', function () {
    <?php if (!empty($success)): ?>
        Swal.fire({
            icon: 'success',
            title: 'Berhasil!',
            text: '<?= addslashes($success); ?>',
            showConfirmButton: false,
            timer: 2500
        });
    <?php elseif (!empty($error)): ?>
        Swal.fire({
            icon: 'error',
            title: 'Gagal!',
            text: '<?= addslashes($error); ?>',
            showConfirmButton: true,
            confirmButtonText: 'OK'
        });
    <?php else: ?>
        console.log('Tidak ada flashdata untuk SweetAlert');
    <?php endif; ?>
});
</script>

