<!-- Modal RACIK-->
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <form action="<?= base_url('Obat/prosesRacikan/' . $this->uri->segment(3) .'/'. $this->uri->segment(4)); ?>" id="formObatRacikan" method="post">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">FORM OBAT RACIKAN</h5>
      </div>
      
        <div class="modal-body">
            <input type="text" hidden name="DokterPengirim" value="<?= $user['IdAlias'];?>">
            <table class="table table-striped table-hover">
                <tr>
                    <th>Nama Racikan</th>
                    <td><input name="namaracikan" required type="text" class="form-control"></td>
                </tr>
                <tr>
                    <th>Jenis</th>
                    <td>
                        <select name="Jenis" required class="form-control">
                            <option value=""></option>
                            <option value="PUYER">PUYER</option>
                            <option value="SIRUP">SIRUP</option>
                            <option value="SALEP">SALEP</option>
                            <option value="LARUT">LARUT</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>Qty</th>
                    <td>
                        <input name="QtyJenis" required type="text" class="form-control">
                        <input type="hidden" name="user" value="<?= $user['nama']; ?>" class="">
                    </td>
                </tr>
                <tr>
                            <th>Racikan</th>
                            <td>
                                <table>
                                    <tr>
                                        <th> Deskripsi </th>
                                        <th>&nbsp;&nbsp;</th>
                                        <th> Jumlah </th>
                                        <th>&nbsp;&nbsp;</th>
                                        <th> Satuan </th>
                                        <th>&nbsp;&nbsp;</th>
                                        <th> Stok </th>
                                        <th>&nbsp;&nbsp;</th>
                                        <th></th>
                                    </tr>
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat1" required onChange="CheckStockObat(this);"  class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" required placeholder="" id="JumlahObat1" onKeyUp="checkJumlahRacikan(this);"  name="Jumlah[]" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                         <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat1" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport" onclick="ShowHideDiv(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat2" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" id="JumlahObat2" name="Jumlah[]" onKeyUp="checkJumlahRacikan(this);"  style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat2" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport2" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport2" onclick="ShowHideDiv2(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport2" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat3" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" name="Jumlah[]" onKeyUp="checkJumlahRacikan(this);"  id="JumlahObat3" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat3" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport3" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport3" onclick="ShowHideDiv3(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport3" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat4" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" name="Jumlah[]" onKeyUp="checkJumlahRacikan(this);"  id="JumlahObat4" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat4" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport4" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport4" onclick="ShowHideDiv4(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport4" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat5" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" name="Jumlah[]" onKeyUp="checkJumlahRacikan(this);"  id="JumlahObat5" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat5" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport5" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport5" onclick="ShowHideDiv5(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport5" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat6" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" onKeyUp="checkJumlahRacikan(this);"  name="Jumlah[]" id="JumlahObat6" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat6" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport6" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport6" onclick="ShowHideDiv6(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport6" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat7" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" name="Jumlah[]" onKeyUp="checkJumlahRacikan(this);"  id="JumlahObat7" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat7" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="chkPassport7" style="cursor: pointer;">
                                                <input type="checkbox" id="chkPassport7" onclick="ShowHideDiv7(this)" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>

                                <table id="dvPassport7" style="display: none;margin-top:5px">
                                    <tr>
                                        <td>
                                            <select name="Obat[]" id="obat8" onChange="CheckStockObat(this);" class="teanganData" data-live-search="true">
                                                <option value=""></option>
                                                <?php
                                                foreach ($obatmodal as $ob) :
                                                ?>
                                                    <option value="<?= $ob['PharmacyCode']; ?>"><?= $ob['Description']; ?> (<?= $ob['PharmacyUnit']; ?>)</option>
                                                <?php
                                                endforeach;
                                                ?>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <input type="text" placeholder="" name="Jumlah[]" onKeyUp="checkJumlahRacikan(this);"  id="JumlahObat8" style="width:100%" class="form-control">
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            <select name="" disabled class="form-control">
                                                <option value=""></option>
                                            </select>
                                        </td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td><input disabled type="text" required placeholder="" name="Stok[]" id="StokObat8" style="width:100%" class="form-control"></td>
                                        <td>&nbsp;&nbsp;</td>
                                        <td>
                                            &nbsp;&nbsp;
                                            <label for="" style="cursor: pointer;">
                                                <input type="checkbox" id="" onclick="" name="cek[]" value="1" />
                                                Tambah
                                            </label>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                <tr>
                    <th>Dosis</th>
                    <td>
                          <input type="text" name="Frekuensi" class="form-control" required>
                        <!-- <select name="Frekuensi" class="form-control" required>
                            <option value="">- Pilih -</option>
                            <?php
                                foreach ($dosis as $dos) :
                                ?>
                                    <option value="<?= $dos['AturanPakai']; ?>"><?= $dos['AturanPakai']; ?></option>
                                <?php
                                endforeach;
                            ?>
                        </select> -->
                    </td>
                </tr>
                <tr>
                    <th>Aturan Pakai</th>
                    <td>
                        <select required name="AturanPakai" class="form-control" required>
                            <option value="">- Pilih -</option>
                            <?php
                            foreach ($caraPakai as $cara) :
                                ?>
                                    <option value="<?= $cara['CaraPakai']; ?>"><?= $cara['CaraPakai']; ?></option>
                                <?php
                            endforeach;
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>Keterangan</th>
                    <td>
                        <textarea class="form-control" name="keterangan" placeholder="Keterangan.."></textarea>
                    </td>
                </tr>
            </table>
            

        </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
        <button type="button" id="btnSubmitObatRacikan" onClick="checkSubmitObatRacikan()" class="btn btn-primary btn-sm">Simpan</button>
      </div>
      
    </div>
    </form>
  </div>
</div>

<script type="text/javascript">

    function CheckStockObat(selectedObject){
        var obatId = selectedObject.id;
        var obatCode = selectedObject.value;
        var stockId = obatId.replace("obat", "StokObat") 
        var jumlahId = obatId.replace("obat", "JumlahObat") 
        CheckStokObatFromDB(obatCode, stockId, jumlahId);
    }

    function checkJumlahRacikan(selectedObject){
        var jumlahId = selectedObject.id;
        var jumlahValue = selectedObject.value;
        var stockId = jumlahId.replace("JumlahObat", "StokObat") 

        var stockValue = document.getElementById(stockId).value;
        if(stockValue == ''){
            document.getElementById(jumlahId).value = "";
            return;
        }
        //alert(jumlahValue+'|'+stockValue)
        if(parseInt(jumlahValue) > parseInt(stockValue)){
            document.getElementById(jumlahId).value = "";
            alert("melebihi Stok Obat");
            return;
        }
    }

    function CheckStokObatFromDB(obatCode, stokId, jumlahId){
        if (obatCode != '') {
            $.ajax({
                url: '<?php echo base_url() ?>RekamMedis/CheckStockObat',
                method: "POST",
                data: {
                    pharmachyCode: obatCode
                },
                dataType: 'json',
                success: function(data) {
                    if(data.stock == "" || data.stock < 0){
                        document.getElementById(jumlahId).value = "";
                        document.getElementById(jumlahId).disabled = true;
                    }else{
                        document.getElementById(jumlahId).disabled = false;
                    }
                    document.getElementById(stokId).value = data.stock;
                }
            });
        }
    }

    function checkSubmitObatRacikan(){
        var qtys = document.getElementsByName("Jumlah[]");
        if(qtys == null || qtys == undefined){
            alert("Silahkan Masukan Obat");
            return false;
        }

        if(qtys.length < 1){
            alert("Silahkan Masukan Obat");
            return false;
        }

        for (var i = 0, len = qtys.length; i < len; i++) {
            var id = i + 1;
            var obat = document.getElementById("obat"+ id).value;
            if((qtys[i].value == "" || qtys[i].value == "0" || qtys[i].value < 0) && obat != ""){
                alert("Masukan Jumlah / Stok Obat ada yang Habis.. Silahkan check kembali..");
                return false;
            }
        }   
        document.getElementById("btnSubmitObatRacikan").disabled = true;
        document.getElementById("formObatRacikan").submit();
        return true;
    }


    function ShowHideDiv(chkPassport) {
        var dvPassport = document.getElementById("dvPassport");
        dvPassport.style.display = chkPassport.checked ? "block" : "none";
    }
    function ShowHideDiv2(chkPassport2) {
        var dvPassport2 = document.getElementById("dvPassport2");
        dvPassport2.style.display = chkPassport2.checked ? "block" : "none";
    }
    function ShowHideDiv3(chkPassport3) {
        var dvPassport3 = document.getElementById("dvPassport3");
        dvPassport3.style.display = chkPassport3.checked ? "block" : "none";
    }
    function ShowHideDiv4(chkPassport4) {
        var dvPassport4 = document.getElementById("dvPassport4");
        dvPassport4.style.display = chkPassport4.checked ? "block" : "none";
    }
    function ShowHideDiv5(chkPassport5) {
        var dvPassport5 = document.getElementById("dvPassport5");
        dvPassport5.style.display = chkPassport5.checked ? "block" : "none";
    }
    function ShowHideDiv6(chkPassport6) {
        var dvPassport6 = document.getElementById("dvPassport6");
        dvPassport6.style.display = chkPassport6.checked ? "block" : "none";
    }   
    function ShowHideDiv7(chkPassport7) {
        var dvPassport7 = document.getElementById("dvPassport7");
        dvPassport7.style.display = chkPassport7.checked ? "block" : "none";
    }   
</script>