<?php 
    $url = 'sampel/pendaftaran/set_uji';
    $edit_value = null; 
    if(!empty($edit) && empty($id_ttpp)) {
        $edit_value = $edit;
        $url .= '/'.$edit_value['id'];
    }    
?>
<?php $nb = $this->m_master->get_field('permintaan_uji', ['id_status', 'nomor_uji', 'tahun', 'bulan']); ?>
<?php //print("<pre>".print_r($nb, true)."</pre>"); ?>

<form class="form-horizontal" method="post" id="formUjiSampel" action="<?= base_url($url); ?>">
<div class="row">
<!-- begin col-8 -->
<div class="col-lg-12">
    <!-- begin panel -->
    <div class="panel panel-grey">
        <div class="panel-heading">
            <a href="<?= site_url('permintaan_uji');?>" class="btn btn-primary m-b-5"><span class="fa fa-arrow-left"></span> Kembali</a>
            <hr>
        </div>
        <div class="panel-body">

            <div class="bg-default">
                <!-- 
                    <input type="hidden" name="<?= $this->security->get_csrf_token_name();?>" value="<?= $this->security->get_csrf_hash();?>"> 
                -->
                <?php if(!empty($id_ttpp)) : ?>
                    <input type="hidden" name="id_ttpp" value="<?= $this->input->get('id_ttpp'); ?>">
                <?php endif; ?>
                
                <?php if(empty($id_ttpp)) : ?>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Jenis Layanan<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <select name="id_layanan" onchange="regLab(this.value);" class="form-control select2" required>
                            <option readonly></option>
                            <?php foreach ($jenis_layanan as $r) { ?>
                                <option <?= opt_selected($edit_value, 'singkatan_layanan', $r['singkatan']); ?>><?= $r['nama'] ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <?php endif; ?>
                
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Registrasi Laboratorium<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <?php
                            if(!empty($noreg)){
                                $edit_value['registrasi_laboratorium'] = $noreg;
                            }
                        ?>
                        <input class="form-control" onkeyup="cek_reglab(this.value)" <?= input_edit($edit_value, 'registrasi_laboratorium'); ?>>
                        <span class='text-danger' id='notif_cek_reglab'></span>
                    </div>
                </div>

                <?php if(empty($id_ttpp)) : ?>

                <div class="form-group m-b-12 tgl_ambil">
                    <label class="col-md-3 col-form-label">Tanggal Pukul Pengambilan<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control datepicker-default tgl_pukul_pengambilan2" <?= input_edit($edit_value, 'tgl_pukul_pengambilan', 'date2', 0); ?>>
                        
                        <input class="form-control datetimepicker1 tgl_pukul_pengambilan" <?= input_edit($edit_value, 'tgl_pukul_pengambilan', 'date2', 0); ?>>

                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Tanggal Pukul Registrasi<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control datetimepicker1 tgl_pukul_registrasi c" <?= input_edit($edit_value, 'tgl_pukul_registrasi', 'date2'); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Perkiraan Estimasi Selesai LHP<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <button type="button" class="btn btn-xs btn-inverse" onclick="get_estimasi();">Auto Set</button>
                        <span class="per_auto text-danger"></span>
                        <input class="form-control perkiraan_estimasi_selesai_lhp datetimepicker1" <?= input_edit($edit_value,'perkiraan_estimasi_selesai_lhp','date2');?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Tanggal Penghapusan<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <span class="per_auto text-danger"></span>
                        <input class="form-control tanggal_penghapusan datetimepicker1" <?= input_edit($edit_value,'tanggal_penghapusan','date2');?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Asal Instansi/Umum<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'instansi_umum'); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Alamat<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'alamat'); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Nomor Surat</label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'nomor_surat', 'text', 0); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Tanggal Surat</label>
                    <div class="col-md-5">
                        <input class="form-control datepicker-default" <?= input_edit($edit_value, 'tgl_surat', 'date1', 0); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Nama Pemohon<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'nama_pemohon'); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Nomor Telepon<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'nomor_telepon'); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Email Pemohon<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'email_pemohon'); ?>>
                    </div>
                </div>
                <div class="form-group m-b-12">
                    <label class="col-md-3 col-form-label">Sumber Dana<span class="text-danger">*</span></label>
                    <div class="col-md-5">
                        <input class="form-control" <?= input_edit($edit_value, 'sumber_dana'); ?>>
                    </div>
                </div>
                <div class="hidden">

                </div>

                <?php endif; ?>

            </div>
        </div>
        <div class="panel-footer">
            <div class="form-group m-b-12">
                <label class="col-md-3 col-form-label"></label>
                <div class="col-md-5">
                    <button type='submit' class='btn btn-success m-b-5' style='margin-right: 5px;'>Simpan</button>
                    <a href="#" onclick="goBack()" class="btn btn-danger m-b-5">Batal</a>
                </div>
            </div>
            </div>
        </div>
    </div>
</form>

<script>
    $('.tgl_ambil').hide();
    <?php if(!empty($edit['singkatan_layanan']) && $edit['singkatan_layanan'] == 'A') { ?>
        $('.tgl_ambil').show();
        $('.tgl_pukul_pengambilan2').hide();
    <?php } else if(!empty($edit['singkatan_layanan']) && in_array($edit['singkatan_layanan'], array('P', 'PS'))) {?>
        $('.tgl_ambil').show();
        $('.tgl_pukul_pengambilan').hide();
    <?php } ?>

    <?php if(!empty($edit['singkatan_layanan'])) : ?>
        <?php if($edit['singkatan_layanan'] == 'PS') : ?>
            $('.tgl_pukul_pengambilan').hide();
            $('.tgl_pukul_pengambilan').removeAttr("name");
        <?php endif; ?>
    <?php endif; ?>

    function regLab(x){
        <?php if(empty($edit)) { ?>
            <?php $jumlah_uji = (strlen($jumlah_uji) == 1) ? '0'.$jumlah_uji : $jumlah_uji; ?>
            $("#registrasi_laboratorium").val('<?= $jumlah_uji.'.'.date("m.Y") ?>'+x);
        <?php }else { ?>
            <?php
                $reg = explode('.', $edit['registrasi_laboratorium']);
                $reg[2] = substr($reg[2],0,4);
            ?>
            $("#registrasi_laboratorium").val('<?= $reg[0].'.'.$reg[1].'.'.$reg[2] ?>'+x);
        <?php } ?>

        if(x == 'A' || x == 'K' || x == 'PS' || x=='PPK'){
            if(x == 'PS') {
                $('.tgl_pukul_pengambilan').hide();
                $('.tgl_pukul_pengambilan').removeAttr("name");
                $('.tgl_pukul_pengambilan2').show();
                $('.tgl_pukul_pengambilan2').attr("name", "tgl_pukul_pengambilan");
            }else{
                $('.tgl_pukul_pengambilan').show();
                $('.tgl_pukul_pengambilan').attr("name", "tgl_pukul_pengambilan");
                $('.tgl_pukul_pengambilan2').hide();
                $('.tgl_pukul_pengambilan2').removeAttr("name");
            }
            $('.tgl_ambil').show();
        }else {
            $('.tgl_ambil').hide();
            $('.tgl_pukul_pengambilan').removeAttr('required');
            $('.tgl_pukul_pengambilan').removeAttr('value');

            $('.tgl_pukul_pengambilan2').removeAttr('required');
            $('.tgl_pukul_pengambilan2').removeAttr('value');
        }
    }
</script>
<script>
    function get_estimasi() {
        if($('.tgl_pukul_registrasi').val() == ''){
            $('.per_auto').html('Harap tanggal pukul registrasi diisi terlebih dahulu');
        }else{
            $('.per_auto').html('');
        }
        var split_tgl = $('.tgl_pukul_registrasi').val().split(' ');
        split_tgl[0] = split_tgl[0].split('/');

        var startDate = split_tgl[0][2]+'-'+split_tgl[0][1]+'-'+split_tgl[0][0]+' '+split_tgl[1];
        startDate = new Date(startDate.replace(/-/g, "/"));

        var endDate = "", noOfDaysToAdd = 16, count = 0;
        while(count < noOfDaysToAdd){
            endDate = new Date(startDate.setDate(startDate.getDate() + 1));
            if(endDate.getDay() != 0 && endDate.getDay() != 6){
                count++;
            }
        }
        var fmonth = endDate.getMonth() + 1;
        var fhour = endDate.getHours();
        var fminutes = endDate.getMinutes();
        if(fminutes.length < 2){
            fminutes = '0'+fminutes;
        }

        var fdate = endDate.getDate()+'/'+fmonth+'/'+endDate.getFullYear()+' '+fhour+':'+fminutes;

        if(isNaN(endDate.getMonth())) fdate='';

        $('.perkiraan_estimasi_selesai_lhp').val(fdate);//You can format this date as per your requirement

        // -----------------------------------------------------
        var split_tgl = fdate.split(' ');
        split_tgl[0] = split_tgl[0].split('/');

        var startDate = split_tgl[0][2]+'-'+split_tgl[0][1]+'-'+split_tgl[0][0]+' '+split_tgl[1];
        startDate = new Date(startDate.replace(/-/g, "/"));

        var endDate = "", noOfDaysToAdd = 30, count = 0;
        while(count < noOfDaysToAdd) {
            endDate = new Date(startDate.setDate(startDate.getDate() + 1));
            //if(endDate.getDay() != 0 && endDate.getDay() != 6){
                count++;
            //}
        }
        var fmonth = endDate.getMonth() + 1;
        var fhour = endDate.getHours();
        var fminutes = endDate.getMinutes();
        if(fminutes.length < 2){
            fminutes = '0'+fminutes;
        }

        var fdate = endDate.getDate()+'/'+fmonth+'/'+endDate.getFullYear()+' '+fhour+':'+fminutes;

        if(isNaN(endDate.getMonth())) fdate='';

        $('.tanggal_penghapusan').val(fdate);//You can format this date as per your requirement
    }
</script>
<script>
    function cek_reglab(x)
    {
        $.post( "<?= base_url('sampel/pendaftaran/cek_nomor_lab'); ?>", { nomor_lab: x }, function( data ) {
            if(data == 1){
                $('#notif_cek_reglab').html('Nomor registrasi laboratorium yang Anda masukkan telah digunakan, Silahkan masukkan nomor lain');
            }else{
                $('#notif_cek_reglab').html('');
            }
        });
    }
</script>
