<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>
    <?php
     // Session 
     if ($this->session->flashdata('message')) {
     echo $this->session->flashdata('message');
     }
    ?>

    <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_Racikan_H WHERE ResepId = '$id'")->row_array();
        extract($_GET);
        $headerRacik = $this->db->query("SELECT  RH.[Case ID],RD.ResepId,p.PharmacyCode,P.Description,iQty,rh.Cara,rh.Frekuensi,rh.Jenis,rh.QtyRacikan,P.PharmacyUnit 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'")->row_array();
        ?>

        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title">Pharmacy Racikan ~ <?= $header['NamaRacikan']; ?>
                <a href="" data-toggle='modal' data-target='.bd-example-modal-lg' class="btn btn-sm btn-success"><i class="fa fa-fw fa-plus"></i> Tambah Obat</a>
                </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/submitHeaderRacikan/' . $this->uri->segment(3)); ?>" method="post">
                    <div class="box-header with-border">
                <b>OBAT RACIKAN</b>
            </div><!-- /.box-header -->
            <div class="box-body">
                <small>
                    <table style="margin-top: 5px;" class="table table-striped table-hover">
                        <tr>
                            <th>Jenis</th>
                            <th>Qty</th>
                            <th>Dosis</th>
                            <th>Aturan Pakai</th>
                            <th></th>
                            <th></th>
                        </tr>
                                <tr style="background-color: gray;color:white">
                                    <td><?= $headerRacik['Jenis']; ?></td>
                                    <td><?= $headerRacik['QtyRacikan']; ?></td>
                                    <td><?= $headerRacik['Frekuensi']; ?></td>
                                    <td><?= $headerRacik['Cara']; ?></td>
                                    <td></td>
                                    <td></td>    
                                </tr>
                                <tr>
                                    <td></td>
                                    <td colspan="4">
                                        <table class="table table-strived" id="hidden-detail">
                                            <tr>
                                                <th width="40%">Nama Obat</th>
                                                <th width="40%">Satuan</th>
                                                <th width="15%">Jumlah</th>
                                                <td width="5%"></td>
                                            </tr>
                                            <?php foreach($tampilracik as $d) : ?>
                                                <tr>
                                                    <td><?= $d['Description']; ?></td>
                                                    <td><?= $d['Satuan']; ?></td>
                                                    <td><?= $d['Jumlah']; ?></td>
                                                    <td>
                                                        <td>
                                                            <a href="" data-toggle="modal" data-target="#ModalEditRacikan<?= $d['PharmacyCode']; ?>" style="color:green" data-backdrop="static" data-keyboard="false"><i class="fa fa-fw fa-pencil"></i></a> 
                                                        </td>
                                                        <td>
                                                        <a href="<?= base_url('Obat/hapusdetailApotekRacikan/' . $d['PharmacyCode'] .'/'. $headerRacik['Case ID'] .'/'. $headerRacik['ResepId']); ?>" onclick="return confirm('Hapus?')" style="color:red"><i class="fa fa-fw fa-trash"></i></a>
                                                    </td></td>
                                                </tr>
                                                <tr>
                                                <td colspan="5">
                                                    <?php
                                                    extract($_GET);
                                                    $aPharmacyCode = $d['PharmacyCode'];
                                                    date_default_timezone_set("Asia/Jakarta");
                                                    $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();
                                                    // RUMUS
                                                    $a = $aQuery['DrugsPrice'] * $d['Jumlah'];
                                                    if ($cases['Category'] == 'OP') {
                                                        $b = (11 / 100) * $a;
                                                    } else {
                                                        $b = 0;
                                                    }
                                                    $c = $qnetprice['NettPrice'];
                                                    //$drugppn = $a + $b;
                                                    $dr = $aQuery['DrugsPrice'];
                                                    ?>
                                                    <div style="display: none;">
                                                        <input type="text" name="Cara[]" value="<?= $headerRacik['Cara']; ?>">
                                                        <input type="text" name="Dosis[]" value="<?= $headerRacik['Frekuensi']; ?>">
                                                        <input type="text" name="Unit[]" value="<?= $d['PharmacyUnit']; ?>">
                                                        <!-- <input type="text" name="Aturan[]" value="">
                                                        <input type="text" name="Ket[]" value="<?= $dataHeader['ket']; ?>"> -->
                                                        <input type="text" name="CaseIDs[]" value="<?= $header['Case ID']; ?>">
                                                        <input type="text" name="DrugsID[]" value="<?= $d['PharmacyCode']; ?>">
                                                        <input type="text" name="Charge[]" value="<?= number_format($a); ?>">
                                                        <input type="text" name="Qty[]" value="<?= $d['Jumlah']; ?>">
                                                        <input type="text" name="NettPrice[]" value="<?= number_format($c); ?>">
                                                        <input type="text" name="UnitPrice[]" value="<?= number_format($dr); ?>">
                                                        <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="<?= $cases['CompanyCode'] ?>">
                                                        <input type="checkbox" checked name="cek[]">
                                                    </div>
                                            </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </table>
                                    </td>
                                    <td></td>
                                </tr> 
                    </table>
                </small>
            </div><!-- /.box-body -->
        </div><!-- /.box -->

                            <?php 
                            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="hidden" name="IdComp" value="<?= $hostname; ?>">
                                    <input type="hidden" name="IdUser" value="web-<?= $user['username']; ?>">
                                </td>
                            </tr>
                            <tr>
                                <td colspan="5">
                                    <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" class="btn btn-sm btn-primary" style="width: 100%;">ADD APOTIK</button>
                                </td>
                            </tr>
                        </table>
                    </form>
                </small>

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

        <?php 
        $this->load->view('apotek/modalRacikdetail');
        ?>

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

        <!-- Modal EditRacikan -->
        <?php foreach($tampilracik as $d) : ?>
        <div class="modal fade" id="ModalEditRacikan<?= $d['PharmacyCode'] ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
            <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
            <form action="<?= base_url('Obat/UpdateRacikan/'.$d['PharmacyCode']) ?>" method="post">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit Obat Racikan</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                <table class="table table-strived">
                <tr>
                    <td>Nama Obat</td>
                    <td><select name="namaobat" required class="teanganData" data-live-search="true">
                    <?php
                        foreach ($obatmodal as $dt) :
                                        if ($d['PharmacyCode'] == $dt['PharmacyCode']) {
                                    ?>
                                            <option selected value="<?= $dt['PharmacyCode']; ?>"><?= $dt['Description']; ?></option>
                                        <?php
                                        } else {
                                        ?>
                                            <option value="<?= $dt['PharmacyCode']; ?>"><?= $dt['Description']; ?></option>
                                    <?php
                                        }
                                    endforeach;
                                    ?>
                                </select>  
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Qty</td>
                    <td><input type="text" class="form-control" name="qty" value="<?= $d['Jumlah']; ?>"></td>
                </tr>
                </table>
                    <div class="modal-footer">
                    <button type="submit" class="btn btn-success">Update</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                    </div>
                </div>
                </form>   
            </div>
            </div>
        </div>
        <?php endforeach; ?>