<?php 
if ($edit=='1') {
    if ($p_tgl_penunjukan_jurusita !='' || $p_jurusita_nama!='') {
            $id_jurusita=$p_id_jurusita;
            $tgl_jurusita=$p_tgl_penunjukan_jurusita;
            $jurusita_nama='Jurusita : '.$p_jurusita_nama;
    }
}else{
        $id_jurusita='';
        $tgl_jurusita='';
        $jurusita_nama='';
        $p_jurusita_id='';
        $p_jurusita_nama='';
}
?>
<div class="centering" style="width:500px;margin-top: 1%;">
        <div id='js' style="border: 1px solid #ffffff;">
        <div class="detil" style="padding: 2px;background-color: white;"> 
        <form id="frm_jurusita" style="background:white;margin:0;border:1px solid #ddd;padding:10px 0px 10px 20px" method="post" action="<?php echo base_url().'manage_delegasi_perkara/simpan_jurusita';?>">
			<table id="tableDetilPerkara" style="font-size:14px;">
            	<col width="38%">
                <col width="62%">
                <tbody>
					<tr><td style="padding:4px;background:white;font-size:16px;color:#111;" align="center" colspan="2"><strong><?php echo $judul;?> Jurusita / Jurusita Pengganti</strong></td></tr> 
                    <tr>
                        <td>Tanggal Penunjukkan Jurusita<font color="red">*</font></td>
                        <td style="padding:0px;padding-left:5px;">
                            <input id="tgl_jurusita" name="tgl_jurusita" value="<?php echo $tgl_jurusita;?>" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun">
                        </td>
                    </tr>
                    <tr>
                        <td>Jurusita / Jurusita Pengganti<font color="red">*</font></td>
                        <td style="padding:0px;padding-left:5px;">
                            <span class="custom-dropdown custom-dropdown--emerald" style='width:250px;'>
                                <select style='width:250px;' name="cmb-jurusita" id="cmb-jurusita" class="custom-dropdown__select custom-dropdown__select--white">
                                    <option id="main-jurusita" value="-1" selected>-- Pilih Jurusita  -- </option>
                                    <?php
                                        if($data_jurusita!=''){
                                            if($data_jurusita->num_rows>0){
                                                foreach ($data_jurusita->result() as $row) {
                                                    echo '<option value="'. $row->id.'" '; echo ($row->id==$id_jurusita) ? 'selected' :''; echo ' >'.$row->nama_gelar.'</option>';
                                                }
                                            }
                                        }
                                    ?>
                                </select>
                            </span>
                        </td>
                    </tr>
                    <tr>
                  	<tr>
                    <tr>
                        <td colspan="3" style="background:white;" align="center">
                            <input id="close_jurusita" type="button" value="Kembali" class="btn kembali">
                            <input type="button" onclick="simpan_jurusita()" value="Simpan" class="btn">
                        </td>
                    </tr>
                    <input type="hidden" id="edit" name="edit" value="<?php echo $edit;?>">
                    <input type="hidden" id="idalur" name="idalur" value="<?php echo $idalur;?>">
                    <input type="hidden" id="iddelegasi" name="iddelegasi" value="<?php echo $iddelegasi;?>">
                    <input type="hidden" id="idpnasal"  name="idpnasal" value="<?php echo $idpnasal;?>">
                    <input type="hidden" id="idpntujuan" name="idpntujuan" value="<?php echo $idpntujuan;?>">
                    <input type="hidden" id="id_jurusita_txt" name="id_jurusita_txt" value="<?php echo $p_id_jurusita;?>">
                    <input type="hidden" id="jurusita_nama_txt" name="jurusita_nama_txt" value="<?php echo $p_jurusita_nama;?>">
                    <input type="hidden" id="tgl_surat_pengantar" name="tgl_surat_pengantar" value="<?php echo $tgl_surat_pengantar;?>">
                    <input type="hidden" id="tgl_surat_pengantar" name="tgl_surat_pengantar" value="<?php echo $tgl_surat_pengantar;?>">
                    <input type="hidden" id="curr_date" value="<?php echo $current_date;?>">
            	</tbody>
         	</table>
     	</form>
        </div>
    </div>
</div>

<script type="text/javascript">

$( document ).ready(function() {
    $( function() {        
        $('#tgl_jurusita').datepicker({
            inline: true,
            showOtherMonths: true,
            dateFormat: 'dd/mm/yy',
            dayNamesMin: ['Ming', 'Sen', 'Sel', 'Rab', 'Kam', 'Jum', 'Sab'],
            beforeShowDay: $.datepicker.noWeekends,
            onSelect: function(dateText, inst) {
                var date = $(this).datepicker('getDate');
                var dayOfWeek = weekday[date.getDay()];
                var error = 0;
                var message ='';
                if(dayOfWeek=='Sunday' || dayOfWeek=='Saturday'){
                    message ='Tidak Dapat Memilih diluar Hari Kerja.';
                    message_error_show_parent(message);
                    return;
                }
                
                var tmp = $(this).val().split("/");
                var tmp2 = $('#curr_date').val().split("/");
                var tgl_surat_pengantar = $('#tgl_surat_pengantar').val();
                var tmp3 = tgl_surat_pengantar.split("-");  
                var tgl_surat = '<?php echo $this->tanggalhelper->convertDayDate($tgl_surat_pengantar); ?>'           

                if((new Date(tmp[2]+"-"+tmp[1]+"-"+tmp[0]+"T00:00:00").getTime() > new Date(tmp2[2]+"-"+tmp2[1]+"-"+tmp2[0]+"T00:00:00").getTime())){
                    message = 'Tidak Dapat Memilih Melebihi Hari ini. Sekarang Tanggal '+$('#curr_date').val();
                    error = 1
                }
                if((new Date(tmp[2]+"-"+tmp[1]+"-"+tmp[0]+"T00:00:00").getTime() < new Date(tmp3[0]+"-"+tmp3[1]+"-"+tmp3[2]+"T00:00:00").getTime())){
                    message = 'Tidak Dapat Memilih Tanggal Sebelum Tanggal Surat Pengantar Pengiriman Berkas Satker Asal. '+tgl_surat;
                    error = 1
                }

                if(error==1){
                    message_error_show_parent(message);
                    $(this).css("background-color", "red");
                }else{
                    $(this).css("background-color", "#dedbdb");
                }               
            }
        });      
    });
});

$("#cmb-jurusita").change(function() {
    var IDJurusita = $("#cmb-jurusita option:selected").val();
    var jurusita_nama=$("#cmb-jurusita option:selected").text();
    if ($('#cmb-jurusita').val()==-1) {
        $('#id_jurusita_txt').val('');
        $('#jurusita_nama_txt').val('');
    }else{
        $('#id_jurusita_txt').val(IDJurusita);
        $('#jurusita_nama_txt').val(jurusita_nama);
    }
});

$('#close_jurusita').click(function(event){
        location.reload();
});

function simpan_jurusita(){
    $.ajax({
        type: 'post',
        url: '<?php echo base_url().'manage_delegasi_perkara/simpan_jurusita';?>',
        data: $('#frm_jurusita').serialize(),
        success: function () {
          location.reload();
        }
      });
}
</script>