<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1>
                    
                </h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="<?php echo base_url();?>">Dashboard</a></li>
                    <li class="breadcrumb-item"><?php echo $parent;?></li>
                    <li class="breadcrumb-item"><a href="<?php echo base_url()."index.php/".$link;?>"><?php echo $titlebefore;?></a></li>
                    <li class="breadcrumb-item active"><?php echo $title;?></li>
                </ol>
            </div>
        </div>
    </div>
</section>
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="card card-default">
                    <?php 
                        $setdisabled    = "";
                        $setid          = "";
                        $setnama        = "";
                        $setkode        = "";
                        $setketerangan  = "";
                        $setquotabangku = "";

                        $setstatus      = 1;
                        if ($actionform=="prosesview"){
                            $setdisabled="disabled='disabled'";
                        }
                        if ($actionform!="prosesadd"){
                            $setid          = $kl->row('mk_id');
                            $setnama        = $kl->row('mk_nama');
                            $setkode        = $kl->row('mk_kode');
                            $setketerangan  = $kl->row('mk_ket');
                            $setquotabangku = $kl->row('mk_quota_bangku');
                            $setstatus      = $kl->row('mk_status');
                        }
                    ?>
                    <form action="<?php echo(base_url()); ?>index.php/<?php echo $link;?>/<?php echo $actionform; ?>" method="post" autocomplete="off">
                        <div class="card-header">
                            <h3 class="card-title"><?php echo $title;?></h3>
                        </div>
                        <div class="card-body">
                            <?php if(validation_errors()){ ?>
                                <div id="alert" class="alert alert-danger alert-dismissable">
                                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                    <i class="icon fa fa-ban"></i> Alert!
                                        <?php echo validation_errors() ?>
                                </div>
                                <script> 
                                    setTimeout(function() {
                                        $('#alert').hide('fast');
                                    }, 5000);
                                </script>
                            <?php } ?>
                            <div class="form-group row">
                                <label for="nama" class="col-sm-2 col-form-label">Nama</label>
                                <div class="col-sm-3">
                                    <input type="hidden" name="id" value="<?php echo $setid;?>"/>
                                    <input id="nama" name="nama" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Nama Klinik" value="<?php echo $setnama;?>" />
                                </div>
                                <div class="col-sm-1">
                                    <input id="kode" name="kode" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Kode" value="<?php echo $setkode;?>" maxlength="2" style="text-transform:uppercase" />
                                </div>
                                <div class="col-sm-2">
                                    <input id="quotabangku" name="quotabangku" type="number" <?php echo $setdisabled;?> class="form-control" placeholder="Quota Bangku" value="<?php echo $setquotabangku;?>" />
                                </div>
                                <div class="col-sm-4">
                                    <input id="keterangan" name="keterangan" type="text" <?php echo $setdisabled;?> class="form-control" placeholder="Keterangan" value="<?php echo $setketerangan;?>" />
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Jadwal Klinik</label>
                                <div class="col-sm-10">
                                    <table class="table table-sm table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th style="width: 5%">#</th>
                                                <th style="width: 10%">Hari</th>
                                                <th style="width: 10%">Shift</th>
                                                <th style="width: 10%">Quota</th>
                                                <th style="width: 10%">Buka</th>
                                                <th style="width: 10%">Tutup</th>
                                                <th style="width: 5%">Pilih</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
                                            <?php 
                                                for($i=1;$i<=7;$i++){
                                                    $hari = ""; $haritext = "";
                                                    switch ($i) {
                                                        case 1: $hari = "senin";    $haritext = "Senin";    break;
                                                        case 2: $hari = "selasa";   $haritext = "Selasa";   break;
                                                        case 3: $hari = "rabu";     $haritext = "Rabu";     break;
                                                        case 4: $hari = "kamis";    $haritext = "Kamis";    break;
                                                        case 5: $hari = "jumat";    $haritext = "Jum'at";   break;
                                                        case 6: $hari = "sabtu";    $haritext = "Sabtu";    break;
                                                        case 7: $hari = "minggu";   $haritext = "Minggu";   break;
                                                    }

                                                    $setidjadwal    = "";
                                                    $setshiftjadwal = "";
                                                    $setquotajadwal = "";
                                                    $setbukajadwal  = "";
                                                    $settutupjadwal = "";
                                                    $setcheckedjadwal   = "";
                                                    $setdisabledjadawal = "disabled='disabled'";
                                                    if ($actionform!="prosesadd"){
                                                        foreach($jd as $datjadwal){
                                                            if($datjadwal->mkj_kode_jadwal==$i){
                                                                $setidjadwal    = $datjadwal->mkj_id;
                                                                $setshiftjadwal = $datjadwal->mkj_jumlah_shift;
                                                                $setquotajadwal = $datjadwal->mkj_total_quota;
                                                                $setbukajadwal  = $datjadwal->mkj_jam_buka;
                                                                $settutupjadwal = $datjadwal->mkj_jam_tutup;
                                                                $setcheckedjadwal   = "checked";
                                                                $setdisabledjadawal = "";
                                                            }
                                                        }
                                                    }
                                                ?>
                                                    <tr>
                                                        <td class="bg-info"><?php echo $i;?></td>
                                                        <td class="bg-info"><?php echo $haritext;?></td>
                                                        <td>
                                                            <input <?php echo $setdisabledjadawal;?> onkeyup="addcolomjadwal(<?php echo $i;?>)" id="shift<?php echo $i;?>" name="shift<?php echo $hari;?>" type="number" <?php echo $setdisabled;?> class="form-control form-control-sm" value="<?php echo $setshiftjadwal;?>" />
                                                            <input hidden id='idjadwal<?php echo $i;?>' name="idjadwal<?php echo $hari;?>" type="text" value="<?php echo $setidjadwal;?>" />
                                                        </td>
                                                        <td>
                                                            <input <?php echo $setdisabledjadawal;?> onkeyup="bagiquota(<?php echo $i;?>)" id="quota<?php echo $i;?>" name="quota<?php echo $hari;?>" type="number" <?php echo $setdisabled;?> class="form-control form-control-sm" value="<?php echo $setquotajadwal;?>" />
                                                        </td>
                                                        <td>
                                                            <input <?php echo $setdisabledjadawal;?> onkeyup="bagijam(<?php echo $i;?>)" id="jambuka<?php echo $i;?>" name="jambuka<?php echo $hari;?>" type="text" <?php echo $setdisabled;?> class="form-control form-control-sm" data-inputmask="'mask': ['99:99', '99:99']" data-mask value="<?php echo $setbukajadwal;?>" />
                                                        </td>
                                                        <td>
                                                            <input <?php echo $setdisabledjadawal;?> onkeyup="bagijam(<?php echo $i;?>)" id="jamtutup<?php echo $i;?>" name="jamtutup<?php echo $hari;?>" type="text" <?php echo $setdisabled;?> class="form-control form-control-sm" data-inputmask="'mask': ['99:99', '99:99']" data-mask value="<?php echo $settutupjadwal;?>"  />
                                                        </td>
                                                        <td style="text-align: center;">
                                                            <div class="custom-control custom-checkbox">
                                                                <input <?php echo $setcheckedjadwal;?> onchange="enablecolom(<?php echo $i;?>)" <?php echo $setdisabled; ?> id="jadwal<?php echo $i;?>" name="jadwalklinik[]" class="custom-control-input custom-control-input-danger custom-control-input-outline" type="checkbox" id="customCheckbox5" value="<?php echo $i;?>" />
                                                                <label for="jadwal<?php echo $i;?>" class="custom-control-label"></label>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td colspan="7" id="app<?php echo $i;?>">
                                                        <?php 
                                                            if ($actionform!="prosesadd"){
                                                                $numshift=0;
                                                                echo "<table>";
                                                                foreach($sh as $datshift){ 
                                                                    if($datshift->mks_klinik_jadwal_id==$setidjadwal){
                                                                        $numshift++;
                                                                        $sjbuka     = $datshift->mks_jam_buka;
                                                                        $splitsjb   = explode(":",$sjbuka);
                                                                    ?>
                                                                    <tr class="shiftremove<?php echo $i;?>">
                                                                        <td style="width: 10%">Mulai Shift <?php echo $numshift;?></td>
                                                                        <td style="width: 10%"><input id="jambukashift<?php echo $i.$numshift;?>" name="jambuka<?php echo $i;?>[]" type="text" class="form-control form-control-sm" placeholder="00 (Jam)" maxlength="2" value="<?php echo $splitsjb[0];?>"/></td>
                                                                        <td style="width: 2%;text-align: center;">:</td>
                                                                        <td style="width: 10%"><input id="menitbukashift<?php echo $i.$numshift;?>" name="menitbuka<?php echo $i;?>[]" type="text" class="form-control form-control-sm" placeholder="00 (Menit)" maxlength="2" value="<?php echo $splitsjb[1];?>"/></td>
                                                                        <td style="width: 5%">Quota Shift </td>
                                                                        <td style="width: 10%">
                                                                            <input onkeyup="tambahquota(<?php echo $i;?>)" id="quotashift<?php echo $i.$numshift;?>" name="quotashift<?php echo $i;?>[]" type="number" class="form-control form-control-sm" value="<?php echo $datshift->mks_quota;?>"/>
                                                                            <input hidden name="idshift<?php echo $i;?>[]" type="text" value="<?php echo $datshift->mks_id;?>"/>
                                                                        </td>
                                                                    </tr>
                                                                    <?php
                                                                    }
                                                                }
                                                                echo "</table>";
                                                            }
                                                        ?>
                                                        </td>
                                                    </tr>
                                                <?php
                                                }
                                            
                                            ?>
                                        </tbody>
                                    </table>
                                    <script>
                                        function enablecolom(num){
                                            if( $('#jadwal'+num).is(':checked') ){
                                                $('.shiftremove'+num).remove();
                                                document.getElementById("shift"+num).disabled = false;
                                                document.getElementById("quota"+num).disabled = false;
                                                document.getElementById("jambuka"+num).disabled = false;
                                                document.getElementById("jamtutup"+num).disabled = false;
                                            }
                                            else{
                                                $("#shift"+num).val("");
                                                $("#quota"+num).val("");
                                                $("#jambuka"+num).val("");
                                                $("#jamtutup"+num).val("");
                                                document.getElementById("shift"+num).disabled = true;
                                                document.getElementById("quota"+num).disabled = true;
                                                document.getElementById("jambuka"+num).disabled = true;
                                                document.getElementById("jamtutup"+num).disabled = true;
                                                var idjadwal = $("#idjadwal"+num).val();
                                                $('.shiftremove'+num).remove();
                                                $("#app"+num).append("<div class='shiftremove"+num+"'><input hidden name='iddeljadwal"+num+"' type='text' value='"+idjadwal+"' /></div>");
                                            }
                                        }

                                        function addcolomjadwal(num){
                                            $('.shiftremove'+num).remove();
                                            var jumapp = $("#shift"+num).val();
                                            for(let i=1;i<=jumapp;i++){
                                                $("#app"+num).append("  <tr class='shiftremove"+num+"'>"+
                                                                            "<td style='width: 10%'>Mulai Shift "+i+"</td>"+
                                                                            "<td style='width: 10%'><input id='jambukashift"+num+i+"' name='jambuka"+num+"[]' type='text' class='form-control form-control-sm' placeholder='00 (Jam)' maxlength='2' /></td>"+
                                                                            "<td style='width: 2%;text-align: center;'>:</td>"+
                                                                            "<td style='width: 10%'><input id='menitbukashift"+num+i+"' name='menitbuka"+num+"[]' type='text' class='form-control form-control-sm' placeholder='00 (Menit)' maxlength='2' /></td>"+
                                                                            "<td style='width: 5%'>Quota Shift </td>"+
                                                                            "<td style='width: 10%'><input onkeyup='tambahquota("+num+")' id='quotashift"+num+i+"' name='quotashift"+num+"[]' type='number' class='form-control form-control-sm' /></td>"+
                                                                        "</tr>");
                                            }
                                            this.bagiquota(num);
                                            this.bagijam(num);
                                        }

                                        function bagiquota(num){
                                            var shift = parseInt($("#shift"+num).val());
                                            var quota = parseInt($("#quota"+num).val());
                                            var hasil = quota / shift;
                                            for(let i=1;i<=shift;i++){
                                                $("#quotashift"+num+i).val(Math.floor(hasil));
                                            }
                                        }

                                        function tambahquota(num){
                                            var shift = parseInt($("#shift"+num).val());
                                            var hasil = 0;
                                            for(let i=1;i<=shift;i++){
                                                hasil = parseInt(hasil) + parseInt($("#quotashift"+num+i).val());
                                            }
                                            $("#quota"+num).val(hasil);
                                        }

                                        function bagijam(num){
                                            var shift   = parseInt($("#shift"+num).val());
                                            var jambuka = new Date("2023-01-01 "+$("#jambuka"+num).val());
                                            var jamtutup= new Date("2023-01-01 "+$("#jamtutup"+num).val());
                                            var diff = Math.abs(jamtutup - jambuka);
                                            var minutes = Math.floor((diff/1000)/60);
                                            var selshift= parseInt(minutes)/parseInt(shift);

                                            for(let i=1;i<=shift;i++){
                                                if(parseInt(i)==1){
                                                    var h = jambuka.getHours("H");
                                                    var m = jambuka.getMinutes("M");
                                                    if(h<10){$("#jambukashift"+num+i).val("0"+h);
                                                    }else{$("#jambukashift"+num+i).val(h);}
                                                    if(m<10){$("#menitbukashift"+num+i).val("0"+m);
                                                    }else{$("#menitbukashift"+num+i).val(m);}
                                                }else{
                                                    jambuka.setMinutes(jambuka.getMinutes() + selshift);
                                                    var h = jambuka.getHours("H");
                                                    var m = jambuka.getMinutes("M");
                                                    if(h<10){$("#jambukashift"+num+i).val("0"+h);
                                                    }else{$("#jambukashift"+num+i).val(h);}
                                                    if(m<10){$("#menitbukashift"+num+i).val("0"+m);
                                                    }else{$("#menitbukashift"+num+i).val(m);}
                                                }
                                            }
                                        }
                                    </script>
                                </div>
                            </div>
                            
                            
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label"></label>
                                <div class="col-sm-2">
                                    <input id="status" name="status" type="checkbox" value="1" <?php echo $setdisabled;?> <?php if($setstatus==1){echo "checked";}?> data-bootstrap-switch data-off-color="danger" data-on-color="success"/>
                                </div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <?php if ($actionform!="prosesview"){ ?>
                                <button type="submit" class="btn btn-info">Simpan</button>
                            <?php } ?>
                            <a href="<?php echo base_url()."index.php/".$link;?>" class="btn btn-default float-right">Cancel</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>
