<?php
    $jnsmed=$this->encrypt->decode(base64_decode($encmed));
?>
<div class="centering" style="width:545px;margin-top: 1%;">
<div id='popup_edit_penetapan_mediasi' style="border: 1px solid #ffffff;">
    <div class="detil" style="padding: 2px;background-color: white;">
		<form id="frm_mediasi" name="frm_mediasi" class="removeformbottomspace" method="post" action="<?php echo base_url().'tab_mediasi_c/insert_update_penetapan_mediasi';?>">
			<table id="tableDetilPerkara" style="font-size:14px;">
				<col width="215px">
				<col width="">
                    <tbody>
						<tr>
							<td colspan="2" class="header">Edit Data Penetapan <?php echo $jnsmed;?></td>
						</tr>
                        <tr>
                            <td colspan="2" class="divider">Data Penetapan Jadwal Sidang Pertama</td>
                        </tr>
                        <tr>
                            <td>Nomor Perkara</td>
                            <td style="padding:0px;padding-left:5px;">
                                <?php echo $nomor_perkara;?>
                            </td>
                        </tr>
                        <tr>
                            <td>Tgl Penetapan Sidang Pertama</td>
                            <td style="padding:0px;padding-left:5px;">
                                <?php echo $this->tanggalhelper->convertDayDate($tanggal_penetapan_sidang_pertama); ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Tgl Sidang Pertama</td>
                            <td style="padding:0px;padding-left:5px;">
                                <?php echo $this->tanggalhelper->convertDayDate($sidang_pertama); ?>
                            </td>
                        </tr>
                        <tr>
                        	<td colspan="2" class="divider">Data Penetapan <?php echo $jnsmed;?></td>
                        </tr>
                        <tr>
                            <td>Tgl Penetapan Mediator
                            <span style="color:red">*</span>
                            </td>
                            <td style="padding:0px;padding-left:5px;">
                                <input id="tgl_penetapan_mediator" name="tgl_penetapan_mediator" value="<?php echo (!empty($penetapan_mediator) ? $this->tanggalhelper->convertToInputDate($penetapan_mediator) : '');?>" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun">
                            </td>
                        </tr>
                        <tr>
                            <td>No. Penetapan Mediator</td>
                            <td style="padding:0px;padding-left:5px;">
                                <?php echo $nomor_perkara;?>
                                <input type="hidden" id="sk_penetapan_mediator" name="sk_penetapan_mediator" value="<?php echo (!empty($nomor_perkara) ? $nomor_perkara : '');?>" class="standard-input" style="width:220px;">
                            </td>
                        </tr>
                        <tr>
                            <td>Mediator
                            <span style="color:red">*</span>
                            </td>
                            <td style="padding:0px;padding-left:5px;">
                            <?php if($urutan==2){ ?>
                            <span class="custom-dropdown custom-dropdown--emerald" style="width:300px">
                                <select name="mediator" id="mediator" style='width:300px;' class="custom-dropdown__select custom-dropdown__select--white">
                                <option value="">Pilih Mediator</option>
                                <?php
                                    if($data_mediator!=''){
                                        if($data_mediator->num_rows()>0){
                                            foreach ($data_mediator->result() as $row){
                                                if($row->id==$id_mediator){
                                                    echo '<option value="'.base64_encode($this->encrypt->encode($row->id)).'" selected>'.$row->status_mediator.' - '.$row->nama.'</option>';
                                                }else{
                                                    echo '<option value="'.base64_encode($this->encrypt->encode($row->id)).'">'.$row->status_mediator.' - '.$row->nama.'</option>';
                                                }
                                            }
                                        }
                                    }
                                echo'</select>';
                                echo'</span>';
                                }else{
                                    echo '<div id="container">';
                                    if($data_mediator!=0){
                                        if($data_mediator->num_rows()>0){
                                            $counter=0;
                                            foreach ($data_mediator->result() as $var){
                                                if($counter==0){
                                                    echo '<input type="text" id="mediator0" name="mediator[]" value="'.$var->nama_mediator.'" required="true" oninvalid="InvalidMsg(this);" oninput="InvalidMsg(this);" placeholder="Pilih nama Mediator" onclick="popup_mediator(0)" class="standard-input" style="width:220px;"/>';
                                                    echo '<input type="hidden" name="mediatorid[]" class="mediator_id0"  value="'.base64_encode($this->encrypt->encode($var->mediator_id)).'"/>';
                                                    echo '<input type="hidden" name="status[]" class="status0" value="'.$var->status_mediator.'"/>';
                                                    echo '<input type="hidden" name="enk_id[]" class="enk_id0" value="'.$var->mediator_id.'" >';
                                                    echo '<a href="#"><img  id="add" src="'.base_url('resources/img/add-user-48.gif').'" style="width: 23px; height: 24px;margin-left: 3px;"></a>';
                                                }else{
                                                    echo '<div id="row'.$counter.'">';
                                                        echo '<input type="text" id="mediator'.$counter.'" name="mediator[]" value="'.$var->nama_mediator.'" required="true" oninvalid="InvalidMsg(this);" oninput="InvalidMsg(this);" placeholder="Pilih nama mediator" class="standard-input" style="width:220px;" onclick="popup_mediator('.$counter.')"/>';
                                                        echo '<input type="hidden" name="mediatorid[]" class="mediator_id'.$counter.'" value="'.base64_encode($this->encrypt->encode($var->mediator_id)).'"/>';
                                                        echo '<input type="hidden" name="status[]" class="status'.$counter.'" value="'.$var->status_mediator.'"/>';
                                                        echo '<input type="hidden" name="enk_id[]" class="enk_id'.$counter.'" value="'.$var->mediator_id.'">';
                                                        echo '<a href="#" id="'.$counter.'" class="btn_remove"><img style="width: 20px; height: 22px; margin-left: 3px;" src="'.base_url('resources/img/remove-user-512.gif').'"></a>';
                                                    echo '</div>';
                                                }
                                            $counter++;
                                            }
                                        }else{
                                            echo '<input type="text" id="mediator0" name="mediator[]" value="'.$var->nama_mediator.'" required="true" oninvalid="InvalidMsg(this);" oninput="InvalidMsg(this);" placeholder="Pilih nama Mediator" onclick="popup_mediator(0)" class="standard-input" style="width:220px;"/>';
                                            echo '<input type="hidden" name="mediatorid[]" class="mediator_id0"  value="'.base64_encode($this->encrypt->encode($var->mediator_id)).'"/>';
                                            echo '<input type="hidden" name="status[]" class="status0" value="'.$var->status_mediator.'"/>';
                                            echo '<input type="hidden" name="enk_id[]" class="enk_id0" value="'.$var->mediator_id.'" >';
                                            echo '<a href="#"><img  id="add" src="'.base_url('resources/img/add-user-48.gif').'" style="width: 23px; height: 24px;margin-left: 3px;"></a>';
                                        }
                                    }
                                echo'</div>';
                                }?>
                            </td>
                        </tr>
                        <?php if($urutan==1){?>
                        <tr>
                            <td>Jenis Penetapan Mediator<span style="color:red">*</span></td>
                            <td><span class="custom-dropdown custom-dropdown--emerald">
                            <select id="jenis_pilih_mediator" name="jenis_pilih_mediator" style="width:250px;z-index:99999" class="custom-dropdown__select custom-dropdown__select--white">
                                <option value="">-- Pilih Jenis Penetapan Mediator --</option>
                                <?php   if($uraian==='sepakat memilih Mediator'){
                                            $selected1 ="selected";
                                        }else if($uraian==='sepakat untuk menyerahkan kepada Majelis Hakim untuk menunjuk Mediator dari Daftar Mediator'){
                                            $selected3 ="selected";
                                        }else{
                                            $selected ='';
                                        }
                                    ?>
                                <option value="sepakat memilih Mediator" <?php print $selected1;?>>Pihak Sepakat Memilih Mediator</option>                                
                                <option value="sepakat untuk menyerahkan kepada Majelis Hakim untuk menunjuk Mediator dari Daftar Mediator" <?php print $selected3;?>>Majelis Hakim Memilih Mediator</option>
                            </select>
                            </td>
                        </tr>
                        <?php } ?>
                        <tr>
                            <td>Tgl Mulai Mediasi</td>
                            <td id="tgl_mulai_mediasi" style="padding:0px;padding-left:5px;">
                                <?php 
                                if (!empty($tgl_mediasi)){
                                    echo $this->tanggalhelper->convertDayDate($tgl_mediasi); 
                                }
                                ?>
                            </td>
                        </tr>
						<tr>
                            <td colspan="2" class="buttongroup">
                                <input type="hidden" name="enc" id="enc" value="<?php echo $enc; ?>">
                                <input type="hidden" id="curr_date" name="curr_date" value="<?php echo $tgl_sekarang;?>">
                                <input type="hidden" id="sidang_pertama" name="sidang_pertama" value="<?php echo $sidang_pertama;?>">
                                <input type="hidden" id="act" name="act" value="e">
                                <input type="hidden" id="mediasi_id" name="mediasi_id" value="<?php echo base64_encode($this->encrypt->encode($mediasi_id));?>">
                                <input type="hidden" id="urutan" name="urutan" value="<?php echo $urutan;?>">
                                <input type="button" id='close_form_mediasi' value="Kembali" class="btn">
                                <input type="button" id="hapus" value="Hapus" style="text-decoration:none" class="del_btn">
                                <input type="submit" id="simpan_btn" onClick="return check_duplicate()" value="Simpan" class="btn">

                        	</td>
                        </tr>
					</tbody>
			</table>
		</form>
	</div>
</div>
</div>
<script type="text/javascript">
function popup_mediator(val){
    popup_form2nd('<?php echo base_url("popup_mediator");?>'+'/'+0+'/'+val+'/'+'H');
}

function InvalidMsg(textbox) {
    if (textbox.value == '') {
        textbox.setCustomValidity('Pilih Nama Mediator');
    }else if (textbox.validity.typeMismatch){
        textbox.setCustomValidity('Silahkan pilih Nama mediator yang telah tersedia');
    }else {
       textbox.setCustomValidity('');
    }
    return true;
}

$(document).ready(function(){
    var count='<?php echo $counter;?>';
    if(count>0){
		var counter=count-1;
	}else{
		var counter=count;
	}
    $('#add').click(function(){
       ++counter; 
       $('#container').append('<div id="row'+counter+'"><input type="text" id="mediator'+ counter + '" name="mediator[]" required="true" oninvalid="InvalidMsg(this);" oninput="InvalidMsg(this);" placeholder="Pilih nama mediator" class="standard-input" style="width:220px;" onclick="popup_mediator('+counter+')" /><input class="mediator_id'+counter+ '" type="hidden" name="mediatorid[]"/> <input class="status'+counter+'" type="hidden" name="status[]"/> <input type="hidden" class="enk_id'+counter+'" name="enk_id[]">  <a href="#" id="'+counter+'" class="btn_remove"><img style="width: 20px; height: 22px;" src=<?php echo base_url('resources/img/remove-user-512.gif');?>></a></div>');
    });
    $(document).on('click', '.btn_remove', function(){  
           var button_id = $(this).attr("id");   
           $('#row'+button_id+'').remove(); 
    });  
});

$('#hapus').ButtonClickHandling({
    refresh:1,
    setRefresh:1,
    clicked:1,
    btnClicked:'#close_form_mediasi',
    whenStatus:1,
    actions:'<?php echo base_url();?>tab_mediasi_c/hapus_mediasi/<?php echo $enc;?>/<?php echo base64_encode($this->encrypt->encode($mediasi_id));?>/<?php echo $urutan;?>',
    formName:'#frm_mediasi',
    actionName:'Hapus Data'
});

$('#tgl_penetapan_mediator').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 error = 0;
                
                var tmp = $(this).val().split("/");
                var tmp2 = $('#curr_date').val().split("/");
                var sidang_pertama = $('#sidang_pertama').val();
                var tmp3 = sidang_pertama.split("-");

                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 Sidang Pertama';
                    error = 1
                }

                if(error==1){
                    message_error_show(message);
                    $(this).css("background-color", "red");
                }else{
                    $(this).css("background-color", "#dedbdb");
                }
                error = 0;
                $('#tgl_mulai_mediasi').text($(this).val());
            }
        });
$('#tgl_penetapan_mediator').change(function(){
                var error = 0;
                
                var tmp = $(this).val().split("/");
                var tmp2 = $('#curr_date').val().split("/");
                var sidang_pertama = $('#sidang_pertama').val();
                var tmp3 = sidang_pertama.split("-");

                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 Sidang Pertama';
                    error = 1
                }

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

                $('#tgl_mulai_mediasi').text($(this).val());
});
$('.datepicker').ValidateDateInput();

function check_duplicate(){
    arr = new Array();
    var x = document.getElementsByName("enk_id[]");
    for($i=0; $i<x.length; $i++){
       arr.push(x[$i].value);
    }

    var uniq = arr
    .map((name) => {
    return {
        count: 1, name: name
        }
    })
    .reduce((a, b) => {
    a[b.name] = (a[b.name] || 0) + b.count
    return a
    }, {})

    var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
        if(duplicates!=''){
            message = 'Anda memilih Mediator yang sama, mohon periksa kembali Mediator yang Anda pilih!';
            message_error_show(message);
            return false;
        }
        return true;
}
$('#frm_mediasi').SubmitHandling({
	refresh:1,
	setRefresh:1,
	clicked:1,
	btnClicked:'#close_form_mediasi',
	whenStatus:1,
    next_step:1
});
<?php 
if($this->session->userdata('jenis_pengadilan')==1){
?>
    $('#close_form_mediasi').click(function(){
        window.open("<?php echo base_url('perkara_detil/'.$enc.'/6');?>","_self");    
    });          
<?php
}else if($this->session->userdata('jenis_pengadilan')==4){
?>
    $('#close_form_mediasi').click(function(){
        window.open("<?php echo base_url('perkara_detil_agama/'.$enc.'/6');?>","_self");    
    });           
<?php
}else{
?>
    $('#close_form_mediasi').closingFormWin();
<?php
}
?>
</script>
