<div class="content-wrapper">
    <section class="content-header">
        <h1>
            Resep Detail
            <small><?= $abdulradjak['NamaPerusahaan']; ?> - <?= $abdulradjak['KodePPK']; ?></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Sis-RS</a></li>
            <li class="active">Pasien</li>
        </ol>
    </section>


    <section class="content">
        <div class="box">
            <?php
            extract($_GET);
            $pasien = $this->db->query("SELECT * FROM Cases c, Patient p WHERE c.[PatientCode]=p.[PatientCode] AND [Case ID] = '$case'")->row_array();
            $pasienc = $this->db->query("SELECT * FROM Cases WHERE [Case ID] = '$case'")->row_array();
            $sCom = $pasienc['CompanyCode'];
            $cc = $this->db->query("SELECT * FROM Company WHERE CompanyCode = '$sCom'")->row_array();
            ?>
            <small>
                <div class="box-body">
                    <table class="table table-striped">
                        <tr>
                            <th>No.MR</th>
                            <th>Nama</th>
                            <th>Tanggal Lahir</th>
                            <th>Umur</th>
                            <th>Tgl.Kunjungan</th>
                            <th>Kategori</th>
                            <th>Jaminan </th>
                        </tr>
                        <tr>
                            <td><?= $pasien['PatientCode']; ?></td>
                            <td><?= $pasien['Description']; ?></td>
                            <td><?= date('d F Y', strtotime($pasien['DOB'])); ?></td>
                            <td>
                                <?php
                                if ($pasien['DOB'] == '') {
                                } else {
                                    $tgll = date('Y-m-d', strtotime(@$pasien['DOB']));
                                    $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;
                                    echo $thn . " tahun " . $bln . " bulan " . $tgl . " hari";
                                }
                                ?>
                            </td>
                            <td><?= date('d F Y', strtotime($pasien['Intake Date'])); ?></td>
                            <td>
                                <?php
                                if ($pasien['Category'] == "IP") {
                                    echo "R.Inap";
                                } else if ($pasien['Category'] == "OP") {
                                    echo "R.Jalan";
                                } else {
                                    echo "";
                                }
                                ?>
                            </td>
                            <td><?= $cc['CompanyName']; ?></td>
                        </tr>
                    </table>
                </div><!-- /.box-body -->
            </small>
        </div><!-- /.box -->

        <?php
        $id = $this->uri->segment(3);
        $header = $this->db->query("SELECT * FROM Resep_Header WHERE ResepId = '$id'")->row_array();
        extract($_GET);
        $racikHeader = $this->db->query("SELECT  RD.ResepId,p.PharmacyCode,P.Description,iQty FROM Resep_Racikan_D RD, Resep_Racikan_H RH, Pharmacy P WHERE RD.ResepId=RH.ResepId AND RD.PharmacyCode=P.PharmacyCode AND RD.[ResepId] = '$id' AND RH.[Status] = '1'")->result_array();
        ?>

        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title">Pharmacy NON - Racikan
                    <h3 class="box-title">Pharmacy <a href="" data-toggle="modal" data-target="#tambah<?= $this->uri->segment(3); ?>" class="btn btn-sm btn-success"><i class="fa fa-fw fa-plus"></i> Tambah Item Obat</a> </h3>
                </h3>
                <div class="box-tools pull-right">
                    <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    <button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-remove"></i></button>
                </div>
            </div>
            <div class="box-body">
                <small>
                    <form action="<?= base_url('Apotik/submitHeader/' . $this->uri->segment(3)); ?>" onSubmit="document.getElementById('submitAddTransaction').disabled=true;" method="post">
                        <table style="margin-top: 5px;" class="table table-striped table-hover">
                            <tr>
                                <th>No.</th>
                                <th>Resep ID</th>
                                <th>Pharmacy</th>
                                <th>Qty</th>
                                <th>Cara Pakai</th>
                                <th>Dosis</th>
                                <th>Unit</th>
                                <th></th>
                            </tr>
                            <?php
                            $nomor = 1;
                            $isStokHabis = false;
                            foreach ($rDetail as $dataHeader) :
                            ?>
                                <tr>
                                    <td><?= $nomor++; ?>.</td>
                                    <td><?= $dataHeader['ResepId']; ?></td>
                                    <td><?= $dataHeader['Description']; ?> ~ <?= $dataHeader['PharmacyCode']; ?> </td>
                                    <td>

                                        <?php
                                        date_default_timezone_set("Asia/Jakarta");
                                        $tgl = date('Ymd');
                                        $aPharmacyCode = $dataHeader['PharmacyCode'];
                                        $Case = $header['CaseID'];
                                        $aQuery = $this->db->query("exec Sp_DrugsPriceWeb  '" . $aPharmacyCode . "'   ")->row_array();
                                        $qnetprice = $this->db->query("SELECT [NettPrice] FROM [Pharmacy] WHERE [PharmacyCode] = '$aPharmacyCode' ")->row_array();
                                        $cases = $this->db->query("SELECT * FROM Cases WHERE [Case ID] = '$case'")->row_array();
                                        
                                        $ware = $this->session->userdata('wareHouseCode');
                                        $checkStock = $this->db->query(" EXEC Sp_SisaStok '$tgl' , '$ware', '$aPharmacyCode'   ")->row_array()['TotQty'];
                                        // RUMUS
                                        $a = $aQuery['DrugsPrice'] * $dataHeader['iQty'];
                                        if ($cases['Category'] == 'OP') {
                                            $b = (11 / 100) * $a;
                                        } else {
                                            $b = 0;
                                        }

                                        
                                        $c = $qnetprice['NettPrice'];
                                        $drugppn = $a + $b;
                                        $d = $aQuery['DrugsPrice'];
                                        ?>
                                        <?php
                                        if ($dataHeader['iQty'] > $checkStock) {
                                            $isStokHabis = true;
                                            echo 'STOK HABIS';
                                        } else {
                                            echo $dataHeader['iQty'];
                                        }

                                        ?>
                                        <div style="display: none;">
                                            <input type="text" name="Cara[]" value="<?= $dataHeader['CaraPakai']; ?>">
                                            <input type="text" name="Dosis[]" value="<?= $dataHeader['frekuensi']; ?>">
                                            <input type="text" name="Unit[]" value="<?= $dataHeader['PharmacyUnit']; ?>">
                                            <!-- <input type="text" name="Aturan[]" value="">
                                            <input type="text" name="Ket[]" value="<?= $dataHeader['ket']; ?>"> -->
                                            <input type="text" name="CaseIDs[]" value="<?= $header['CaseID']; ?>">
                                            <input type="text" name="DrugsID[]" value="<?= $dataHeader['PharmacyCode']; ?>">
                                            <input type="text" name="Charge[]" value="<?= number_format($a); ?>">
                                            <input type="text" name="Qty[]" value="<?= $dataHeader['iQty']; ?>">
                                            <input type="text" name="NettPrice[]" value="<?= number_format($c); ?>">
                                            <input type="text" name="UnitPrice[]" value="<?= number_format($d); ?>">
                                            <input type="text" name="DateCreated[]" value="<?= date('Y-m-d H:i:s'); ?>">
                                            <input type="text" name="UserCreated[]" value="<?= $user['username']; ?>">
                                            <!-- <input type="text" name="JumlahSatuanPakai[]" value="<?= $dataHeader['JumlahSatuanPakai']; ?>"> -->
                                            <input type="text" name="PPN[]" value="<?= number_format($b); ?>">
                                            <input type="text" name="poli" value="<?= $cases['LastOP']; ?>">
                                            <input type="text" name="company" value="<?= $pasien['CompanyCode']; ?>">
                                            <input type="checkbox" checked name="cek[]">
                                        </div>
                                    </td>
                                    <td><?= $dataHeader['CaraPakai']; ?></td>
                                    <td><?= $dataHeader['frekuensi']; ?></td>
                                    <td><?= $dataHeader['PharmacyUnit']; ?></td>
                                    <td>
                                        <a href="" data-toggle="modal" data-target="#edit<?= $dataHeader['Id']; ?>" class="btn btn-sm btn-primary"><i class="fa fa-fw fa-edit"></i> Edit</a>
                                        <a href="<?= base_url('Obat/hapusObatDetail/' . $dataHeader['Id'] . '/' . $dataHeader['ResepId'] . '/' . $case . '/' . $doct); ?>" onclick="return confirm('Hapus obat <?= $dataHeader['Description']; ?>')" class="btn btn-sm btn-danger"><i class="fa fa-fw fa-trash"></i> Hapus</a>
                                    </td>
                                </tr>
                            <?php
                            endforeach;
                            extract($_GET);
                            $getCase = $this->db->query("SELECT * FROM Cases C, CaseAdmin CA WHERE C.[Case ID]=CA.[Case ID] AND C.[Case ID] = '$case'")->row_array();
                            ?>
                            <tr style="display: none;">
                                <td colspan="9">
                                    <label for="">ACC ACTIVITY JURNAL</label>
                                    <?php
                                    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                                        $ip = $_SERVER['HTTP_CLIENT_IP'];
                                    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                                        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                                    } else {
                                        $ip = $_SERVER['REMOTE_ADDR'];
                                    }
                                    $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
                                    ?>
                                    <input type="text" name="IdComp" value="<?= $hostname; ?>">
                                    <input type="text" name="IdUser" value="web-<?= $user['username']; ?>">
                                </td>
                            </tr>
                            <tr>
                                <td colspan="8">
                                    <div style="display: none;">
                                        <input type="text" name="CaseID" value="<?= $case; ?>">
                                        <input type="text" name="PatientCode" value="<?= $getCase['PatientCode']; ?>">
                                        <input type="text" name="Transaction_Date" value="<?= date('Y-m-d'); ?>">
                                        <input type="text" name="Transaction_Time" value="<?= date('Y-m-d H:i:s'); ?>">
                                        <input type="text" name="DoctorID" value="<?= $getCase['DoctorID']; ?>">
                                        <input type="text" name="RefferedBy" value="<?= $getCase['Unit']; ?>">
                                        <input type="text" name="Class" value="<?= $getCase['Class']; ?>">
                                        <!-- <input type="text" name="Room" value="<?= $user['RoomUnit']; ?>"> -->
                                        <input type="text" name="UserCreated" value="<?= $user['username']; ?>">
                                        <input type="text" name="DateCreated" value="<?= date('Y-m-d H:i:s'); ?>">
                                        <input type="text" name="TransStatus" value="NON EMPLOYE">
                                        <input type="text" name="Type" value="SALE">
                                        <!-- <input type="text" name="RoomUnit" value="<?= $user['RoomUnit']; ?>"> -->
                                        <input type="hidden" name="UserCreated" value="<?= $user['username']; ?>">
                                    </div>

                                    <button type="submit" id="submitAddTransaction" class="btn btn-sm btn-primary" style="width: 100%;">ADD TRANSACTION</button>
                                </td>
                            </tr>
                        </table>
                    </form>
                    <small>Note : Transaksi Dapat Dilanjutkan Apabila Stok Obat Tersedia.. </small>
                </small>

            </div><!-- /.box-body -->
        </div><!-- /.box -->


    </section><!-- /.content -->
</div><!-- /.content-wrapper -->
<!-- Modal -->
<?php
$resp = $this->uri->segment(3);
?>
<div class="modal fade" id="tambah<?= $this->uri->segment(3); ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <form onsubmit="btnSimpan.disabled = true; return true;" action="<?= base_url('Obat/tambahDetailMonitor/' . $resp . '?case=' . $case . '&doct=' . $doct); ?>" method="post">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Tambah ~ <?= $this->uri->segment(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">
                    <input type="text" hidden name="resep" value="<?= $resp; ?>">
                    <input type="text" hidden name="case" value="<?= $case; ?>">
                    <input type="text" hidden name="doct" value="<?= $doct; ?>">
                    <!-- <label for="">Resep ID</label> <br> -->
                    <input type="text" style="display: none;" name="Resep_ID" class="form-control" readonly value="<?= $this->uri->segment(3); ?>">
                    <label for="">Obat</label> <br>
                    <select name="ProcedureCode" class="teanganData" data-live-search="true">
                        <option value=""></option>
                        <?php
                        foreach ($obat as $ob) :
                        ?>
                            <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                        <?php
                        endforeach;
                        ?>
                    </select><br>
                    <label for="">Qty</label><br>
                    <input type="text" name="Qty" class="form-control" required>

                    <label for="">Frekuensi</label><br>
                    <select name="frekuensi" class="form-control" required>
                        <option value=""></option>
                        <?php
                        $dosis = $this->db->query("SELECT * FROM Resep_AturanPakai")->result_array();
                        foreach ($dosis as $sDosis) :
                        ?>
                            <option value="<?= $sDosis['AturanPakai']; ?>"><?= $sDosis['AturanPakai']; ?></option>
                        <?php
                        endforeach;
                        ?>
                    </select>
                    <label for="">Cara Pakai</label><br>
                    <select name="CaraPakai" class="form-control" required>
                        <option value=""></option>
                        <?php
                        foreach ($caraPakai as $cPakai) :
                        ?>
                            <option value="<?= $cPakai['CaraPakai']; ?>"><?= $cPakai['CaraPakai']; ?></option>
                        <?php
                        endforeach;
                        ?>
                    </select>
                    <label for="">Keterangan</label>
                    <textarea name="ket" class="form-control"></textarea> <br>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" id="btnSimpan" class="btn btn-primary">Tambah</button>
                </div>
            </div>
        </form>
    </div>
</div>


<?php
foreach ($rDetail as $edit) :
?>
    <div class="modal fade" id="edit<?= $edit['Id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <form onsubmit="btnSimpan.disabled = true; return true;" action="<?= base_url('Obat/updateDetailMonitor/' . $resp . '?case=' . $case . '&doct=' . $doct); ?>" method="post">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Tambah ~ <?= $this->uri->segment(3); ?> ~ ID Resep Detail <?= $edit['Id']; ?></h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <input type="text" hidden name="resep" value="<?= $resp; ?>">
                        <input type="text" hidden name="case" value="<?= $case; ?>">
                        <input type="text" hidden name="doct" value="<?= $doct; ?>">
                        <input type="text" hidden name="Id" value="<?= $edit['Id']; ?>">
                        <!-- <label for="">Resep ID</label> <br> -->
                        <input type="text" style="display: none;" name="Resep_ID" class="form-control" readonly value="<?= $this->uri->segment(3); ?>">

                        <label for="">Obat <?php $a = $edit['PharmacyCode']; ?></label> <br>
                        <select name="ProcedureCode" class="teanganData" data-live-search="true">
                            <option value=""></option>
                            <?php
                            foreach ($obat as $ob) :
                                if ($ob['PharmacyCode'] == $a) {
                            ?>
                                    <option selected value="<?= $ob['PharmacyCode']; ?>"><?= $ob['PharmacyCode']; ?> - <?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                <?php
                                } else {
                                ?>
                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['PharmacyCode']; ?> - <?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                            <?php
                                }
                            endforeach;
                            ?>
                        </select><br>

                        <label for="">Qty</label><br>
                        <input type="number" name="Qty" class="form-control" value="<?= $edit['iQty']; ?>" required>

                        <label for="">Aturan Pakai <?php $f = $edit['frekuensi']; ?></label><br>
                        <select name="frekuensi" class="form-control" required>
                            <option value=""></option>
                            <?php
                            $dosis = $this->db->query("SELECT * FROM Resep_AturanPakai")->result_array();
                            foreach ($dosis as $sDosis) :
                                if ($sDosis['AturanPakai'] == $f) {
                            ?>
                                    <option selected value="<?= $sDosis['AturanPakai']; ?>"><?= $sDosis['AturanPakai']; ?></option>
                                <?php
                                } else {
                                ?>
                                    <option value="<?= $sDosis['AturanPakai']; ?>"><?= $sDosis['AturanPakai']; ?></option>
                            <?php
                                }
                            endforeach;
                            ?>
                        </select>

                        <label for="">Cara Pakai</label><br>
                        <select name="CaraPakai" class="form-control" required>
                            <option value="<?= $edit['CaraPakai']; ?>"><?= $edit['CaraPakai']; ?></option>
                            <option value=""></option>
                            <?php
                            foreach ($caraPakai as $cPakai) :
                                if ($cPakai['CaraPakai'] == $edit['CaraPakai']) {
                            ?>
                                    <option selected value="<?= $cPakai['CaraPakai']; ?>"><?= $cPakai['CaraPakai']; ?></option>
                                <?php
                                } else {
                                ?>
                                    <option value="<?= $cPakai['CaraPakai']; ?>"><?= $cPakai['CaraPakai']; ?></option>
                            <?php
                                }
                            endforeach;
                            ?>
                        </select>

                        <label for="">Keterangan</label>
                        <textarea name="ket" class="form-control"><?= $edit['Ket']; ?></textarea> <br>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button id="btnSimpan" type="submit" class="btn btn-primary">Update</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
<?php
endforeach;
?>

<script>
    var isEnabledButtonAddTransaction = "<?php echo $isEnabledButtonAddTransaction;  ?>";
    document.getElementById("submitAddTransaction").disabled = false;
    if (!isEnabledButtonAddTransaction) {
        document.getElementById("submitAddTransaction").disabled = true;
    }

    var isStokHabis = "<?php echo $isStokHabis;  ?>";
    if (isStokHabis) {
        document.getElementById("submitAddTransaction").disabled = true;
    }
</script>