<?php
function penyebut($nilai) {
    $nilai = abs($nilai);
    $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
    $temp = "";
    if ($nilai < 12) {
        $temp = " ". $huruf[$nilai];
    } else if ($nilai <20) {
        $temp = penyebut($nilai - 10). " belas";
    } else if ($nilai < 100) {
        $temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10);
    } else if ($nilai < 200) {
        $temp = " seratus" . penyebut($nilai - 100);
    } else if ($nilai < 1000) {
        $temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100);
    } else if ($nilai < 2000) {
        $temp = " seribu" . penyebut($nilai - 1000);
    } else if ($nilai < 1000000) {
        $temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000);
    } else if ($nilai < 1000000000) {
        $temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000);
    } else if ($nilai < 1000000000000) {
        $temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000));
    } else if ($nilai < 1000000000000000) {
        $temp = penyebut($nilai/1000000000000) . " triliun" . penyebut(fmod($nilai,1000000000000));
    }     
    return $temp;
}
 
function terbilang($nilai) {
    if($nilai<0) {
        $hasil = "minus ". trim(penyebut($nilai));
    } else {
        $hasil = trim(penyebut($nilai));
    }     
    return $hasil;
}
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Sistem Klinik
            <small>Rekam Medis</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Klinik</a></li>
            <li class="active">Dashboard</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">

        <!-- Default box -->
        <div class="box">
            <div class="box-header with-border">
                <div class="box-tools pull-right">
                </div>
            <div class="box-body">
            <table class="table table-sm ">
                <tr>
                    <td>Case ID</td>
                    <td>:</td>
                    <td><?= $dt['Case ID']; ?></td>

                    <td>Mr</td>
                    <td>:</td>
                    <td><?= $dt['PatientCode']; ?></td>
                </tr>
                <tr>
                    <td>Nama</td>
                    <td>:</td>
                    <td><?= $dt['Description']; ?></td>

                    <td>Penjamin</td>
                    <td>:</td>
                    <td><?= $dt['CompanyName']; ?></td>
                </tr>
                <tr>
                    <td>Category</td>
                    <td>:</td>
                    <td><?= $dt['Category']; ?></td>

                    <td>Doctor</td>
                    <td>:</td>
                    <td><?= $dt['FirstName']; ?></td>
                </tr>
                <tr>
                    <td>Diagnosa</td>
                    <td>:</td>
                    <td><?= $dt['Presenting Problem']; ?></td>

                    <td>Status</td>
                    <td>:</td>
                    <td><?= $dt['Case Status']; ?></td>
                </tr>
                <tr>
                    <td>Intake Date</td>
                    <td>:</td>
                    <td><?= date('d-m-Y', strtotime($dt['Intake Date'])); ?>

                    <td>Close Date</td>
                    <td>:</td>
                    <td><?= date('d-m-Y', strtotime($dt['Closed Date'])); ?></td>
                </tr>
            </table>  
            <br>
            <?php
                $caseid = $this->uri->segment(3);
                $qcek = $this->db->query("Select [Case Status] from Cases where [Case ID] = $caseid")->row_array();
                if($qcek["Case Status"] == 'OPEN') {
            ?>
            <a href="<?= base_url('Transaction/AddTransaction/'.$dt['Case ID']); ?>" class="btn btn-success" >Add Transaction</a> 
            <?php }else{ ?>
                <a href="#" class="btn btn-danger" disabled><?= $qcek['Case Status']; ?></a>
            <?php } ?> 

            <a href="" class="btn btn-success" >Preview</a>  
            <a href="<?= base_url('Transaction/detailheaderdepo/'.$dt['Case ID']); ?>" class="btn btn-primary" >Drug</a> 
            <a href="<?= base_url('Kasir/detailchasir/'.$dt['Case ID']); ?>" class="btn btn-warning" >Chasir</a>          
            <a href="<?= base_url('Transaction/detailheader/'.$dt['Case ID']);?>" class="btn btn-danger">Back to Transaction</a>              
            </div>    
        </div>
        </div>
        <!-- Header -->
        <div class="box">
            <div class="box-body">
            <small>
            <table class="table table-striped">
                    <th>Transaksi ID</th>
                    <th>Transaction Date</th>
                    <th>Total Charge</th>
                    <th>Category</th>
                    <?php 
                    $sum = 0;
                    $sum = $total['total'];
                    ?>
                    <tr>
                        <td><a href="<?= base_url('Transaction/detailTransactiondepo/'.  $detail['Case ID'] .'/'. $detail['TransactionID']) ?>" id="btnCari"><?= $detail['TransactionID']; ?></a></td>
                        <td><?= date('d-m-Y', strtotime($detail['Transaction_Date'])); ?></td>
                        <td><?= number_format($sum,0); ?></td>
                        <td><?= $detail['RefferedBy']; ?></td>
                    </tr>
                    <tr style="background-color: greenyellow;">
                        <td colspan="3"><b><center>Total</center></b></td>
                        <td><b>Rp. <?= number_format($total['total'],0); ?></b></td>
                        <td><b><i>*(<?= terbilang($sum); ?>)<small>*Harga Sudah Termasuk Ppn 11%</small></i></b></td>
                    </tr>
                    </table>
                </small>
            </div>
        </div>

        <!-- Detail -->
        <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title">Detail Transaction Apotik</h3>
            <div class="box-tools pull-right">
            <button type="button" class="btn btn-xs btn-warning" data-widget="collapse"><i class="fa fa-minus"></i>
            </button>
            <button type="button" class="btn btn-xs btn-danger" data-widget="remove"><i class="fa fa-times"></i></button>
            </div>
            </div>
            <div class="box-body">
                <small>
                    <table class="table table-striped"  id="hidden-detail">
                        <tr>
                            <th>Transaksi ID</th>
                            <th>Treatment</th>
                            <th>Category</th>
                            <th>Harga</th>
                            <th>Doctor</th>
                            <th>~</th>
                        </tr>
                        <?php foreach($trans1 as $trx) : ?>
                            <tr>
                                <td><?= $trx['TransactionID'] ?></td>
                                <td><?= $trx['Description'] ?></td>
                                <td><?= $trx['RefferedBy'] ?></td>
                                <td><?= number_format($trx['Charge'],0) ?></td>
                                <td><?= $trx['FirstName'] ?></td>
                                <td>
                                <?php
                                    if($qcek["Case Status"] == 'OPEN') {
                                ?>     
                                  <a href="#" onclick="additem()" class="btn btn-sm btn-primary bg-blue-gradient" >
                                            <i class="fa fa-fw fa-plus"></i>
                                </a>
                                <a href="#" onclick="showDiv()" class="btn btn-sm btn-warning">
                                <i class="fa fa-fw fa-pencil"></i>
                                </a>
                                    <!-- <a href="<?= base_url('Transaction/EditTransaction/'.$trx['ID']); ?>"  class="btn btn-sm btn-primary">
                                        <i class="fa fa-fw fa-pencil"></i>
                                    </a> -->
                                    <a href="<?= base_url('Transaction/DeletItems/'.$trx['ID'].'/'.$trx['TransactionID'].'/'.$trx['Case ID']); ?>" onclick="return confirm('Apakah anda yakin ingin meng hapusitem transaksi?')"  class="btn btn-sm btn-danger">
                                        <i class="fa fa-fw fa-trash"></i>
                                    </a>
                                    <?php }else{ ?>
                                        <a href="#" class="btn btn-danger" disabled><?= $qcek['Case Status']; ?></a>
                                    <?php } ?> 
                                </td>
                              
                            </tr>
                      <?php endforeach; ?>
                    </table>
                </small>
            <?php
            $id = $trx['ID'];
            $edit = $this->M_Transaksi->getedit($id)->result_array();
            ?>
                <small>
                <form action="<?= base_url('Transaction/UpdateTransaction/'.$id); ?>" method="post">
                    <table class="table table-striped" id="welcomeDiv" style="display: none;">
                    <tr>
                    <th>Treatment</th>
                    <th>Doctor</th>
                    <th>Harga</th>
                    <th>~</th>
                    </tr>
                    <?php foreach($edit as $a) : ?>
                    <tr>
                       <td>
                       <select name="tindakan" class="teanganData" required data-live-search="true">

                                <?php
                                    foreach ($tindakanedit as $d) :
                                        if ($a['Activity'] == $d['ProcedureCode']) {
                                    ?>
                                            <option selected value="<?= $d['ProcedureCode']; ?>"><?= $d['Description']; ?></option>
                                        <?php
                                        } else {
                                        ?>
                                            <option value="<?= $d['ProcedureCode']; ?>"><?= $d['Description']; ?></option>
                                    <?php
                                        }
                                    endforeach;
                                    ?>
                        </select>  
                       </td>
                       <td>
                       <select name="dokter" class="form-control" required data-live-search="true">
                                <option value="">-- Pilih Dokter --</option>
                                <?php
                                    foreach ($dokter as $d) :
                                        if ($a['DoctorID'] == $d['DoctorID']) {
                                    ?>
                                            <option selected value="<?= $d['DoctorID']; ?>"><?= $d['FirstName']; ?></option>
                                        <?php
                                        } else {
                                        ?>
                                            <option value="<?= $d['DoctorID']; ?>"><?= $d['FirstName']; ?></option>
                                    <?php
                                        }
                                    endforeach;
                                    ?>
                         </select>
                         <input type="hidden" name="user" value="<?= $user['nama']; ?>">
                       </td>
                       <td>
                       <input type="text" name="harga" id="txtHarga" class="form-control" value="<?= $a['Charge'] ?>">
                       </td>
                       <td>
                        <button type="submit" class="btn btn-sm btn-success">Update Treatment</button>
                        <button type="submit" class="btn btn-sm btn-warning" onclick="back()">Back</button>
                    </td>
                    </tr>
                    <?php endforeach; ?>
                    </table>
                </form>
                </small>

                <!-- Tambah item Tindakan -->
            <?php
                $caseid       = $this->uri->segment(3);
                $pasienDetail = $this->db->query("Select * from Cases where [Case ID] = '$caseid' ")->row_array();
                $idtransaksi       = $this->uri->segment(4);
                //$cekidtransaksi = $this->db->query("Select * from Activity where TransactionID = '$idtransaksi' ")->num_rows();
            ?>
        <!-- Default box -->
        <div class="box" style="display: none;" id="add-item">
            <div class="box-body">
                <small>
                    <form action="<?= base_url('Transaction/prosesAddTreatment'); ?>" method="post">
                        <table class="table table-striped">
                            <tr>
                                <th colspan="4">Items Transaction</th>
                            </tr>
                            <tr>
                                <th>Treatment</th>
                                <th>Doctor</th>
                                <th>Harga</th>
                                <th>Coa</th>
                            </tr>
                            <tr>
                                <td style="display: none;">
                                    <input type="text" name="caseID" class="form-control" value="<?= $pasienDetail['Case ID']; ?>">
                                    <input type="text" name="mr" class="form-control" value="<?= $pasienDetail['PatientCode']; ?>">
                                    <input type="text" name="class" class="form-control" value="<?= $pasienDetail['Category']; ?>">
                                    <input type="text" name="refrensi" class="form-control" value="<?= $pasienDetail['LastOP']; ?>">
                                    <input type="text" name="cekidtransaksi" value="<?= $cekidtransaksi; ?>">
                                    <input type="text" name="idtransaksi" value="<?= $idtransaksi; ?>">
                                </td>
                                <td>
                                <input type="text" name="namatindakan" readonly class="form-control" id="txtCariTindakan" placeholder="Pilih Treatment" value="">
                                <input type="hidden" name="kodetindakan" class="form-control" id="txtKodeTindakan" value="">
                                </td>
                                <td>
                                <select name="dokter" class="teanganData" required data-live-search="true">
                                <option value="">-- Pilih Dokter --</option>
                                <?php foreach($dokter as $d) : ?>
                                <option value="<?= $d['DoctorID']; ?>"><?= $d['FirstName']; ?></option>
                                <?php endforeach; ?>
                                </select>
                                <input type="hidden" name="user" value="<?= $user['nama']; ?>">
                                </td>
                                <td>
                                <input type="text" name="harga" id="txtHargaa" class="form-control" value="">
                                </td>
                                <td>
                                <input type="text" readonly name="coa" id="txtCoa" class="form-control" value="">
                                </td>
                            </tr>
                            <tr>
                                <td colspan="6">
                                    <button type="submit" class="btn btn-sm btn-success">Add Treatment</button>
                                    <a href="<?= base_url('Transaction/detailTransaction/'.$pasienDetail['Case ID'].'/'.$idtransaksi) ?>" class="btn btn-sm btn-danger">Kembali</a>
                                </td>
                            </tr>
                        </table>
                    </form>
                </small>
            </div><!-- /.box-body -->
        </div><!-- /.box -->
            </div><!-- /.box-body -->
        </div>
    </section>
</div>

<!-- Modal Cari Tindakan -->
<div class="modal fade" id="ModalCariTindakan" 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">Edit Treatment</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="col-ms-4">
            <select name="category" id="textCategory"" class="teanganData" required data-live-search="true">
                                <?php foreach($category as $d) : ?>
                                <option value="<?= $d['ActivityClass']; ?>"><?= $d['ActivityClass']; ?></option>
                                <?php endforeach; ?>
            </select>
            </div><br><br>
            <div class="col-ms-4">
                    <input type="text" class="form-control" id="textCaritindakan" placeholder="Ketik Nama Treatment"><br>
                </div>
                    <table class="table table-hover mt-3">
                        <thead>
                            <tr>
                                <th scope="col" width="50%">Kode Treatment</th>
                                <th scope="col" width="40%">Nama Treatment</th>
                                <th scope="col" width="40%">Harga Treatment</th>
                                <th scope="col" width="40%">COA</th>
                                <th scope="col" width="10%">Action</th>
                            </tr>
                        </thead>
                        <tbody id="ListTindakan">

                        </tbody>
                    </table>
            </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 -->


<script>
function showDiv() {
   document.getElementById('welcomeDiv').style.display = "block";
   document.getElementById('hidden-detail').style.display = "none";
}

function back() {
   document.getElementById('welcomeDiv').style.display = "none";
   document.getElementById('hidden-detail').style.display = "block";
}

function additem() {
   document.getElementById('add-item').style.display = "block";
  // document.getElementById('hidden-detail').style.display = "none";
}
</script>