<?php
echo'<div class="centering" style="width:1000px;margin-top: 1%;z-index:-100">
    <div id="frm_mediasi_verzet" style="border: 1px solid #ffffff;">
        <div class="detil" style="padding: 2px;background-color: white;">
            <div style="font-size:14px;">
                <form id="frm_mediasi_diluar" class="removeformbottomspace" method="post" action="'.Base_url().'mediasi_diluar/validate_input_add'.'">
                    <table id="tableDetilPerkara" style="font-size:14px; width:100%" border="0">
                        <col width="280px">
                        <col>
                        <tbody>
                            <tr><td colspan="2" class="header">Tambah Data Mediasi</td></tr>
                        	<tr><td colspan="2" class="divider">Data Register</td></tr>
                            <tr>
								<td id="first-child">Nomor Perkara</td>
								<td style="font-weight: bold;"><span>'.$nomor_perkara.'</span></td>
                            </tr>                    
                            <tr>
                            	<td id="first-child">Tanggal Pendaftaran</td>
                                <td style="font-weight: bold;"><span>'.$tgl_pendaftaran.'</span></td>
                            </tr>                    
                            <tr><td colspan="2" class="divider">Data Mediasi</td></tr>
                            <tr>
                                <td id="first-child">Mediator</td>
                                <td style="padding:0px;padding-left:5px;">
                                <div id="container">';
                                    if($data_mediator->num_rows>0){
                                            $counter = 0;
                                            foreach ($data_mediator->result() as $var){
                                                if($counter==0){
                                                    echo '<input type="hidden" name="btn_action" value="edit">';
                                                    echo '<input type="text" id="mediator0" name="mediator[]" value="'.$var->nama_mediator.'" placeholder="Klik kolom untuk pilih mediator" onclick="popup_mediator(0)" class="standard-input" style="width:251px;"/>';
                                                    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="hidden" name="btn_action" value="edit">';
                                                        echo '<input type="text" id="mediator'.$counter.'" name="mediator[]" value="'.$var->nama_mediator.'" placeholder=" Klik kolom untuk pilih mediator" class="standard-input" style="width:251px;" 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" name="mediator[]" placeholder="Klik kolom untuk pilih mediator" class="standard-input" onclick="popup_mediator(0)" style="width:251px;" id="mediator0" />';
                                        echo '<input type="hidden" class="mediator_id0" name="mediatorid[]"/>';
                                        echo '<input type="hidden" class="status0" type="hidden" name="status[]"/>';
                                        echo '<input type="hidden" name="enk_id[]" class="enk_id0">';
                                        echo '<input type="hidden" name="btn_action" value="add">';
                                        echo '<a href="#" class="first_add" hidden><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>
        				    <tr>
                                <td id="first-child">Tanggal Kesepakatan<span style="color:red"> *</span></td>
                                <td>
                                	<input id="tgl_mediasi" name="tgl_mediasi" value="'.(!empty($tgl_kesepakatan)?$tgl_kesepakatan:'').'" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun" >
                               	</td>
                            </tr>
                            <tr>
                                <td id="first-child">Isi Kesepakatan<span style="color:red"> *</span></td>
                                <td style="background:white;" id="isi_kesepakatan_text">';
                                echo $this->ckeditor->editor("isi_kesepakatan",$isi_kesepakatan_perdamaian);
                            echo'</td>
                            </tr>
                            <tr>
                                <td>Keterangan</td>
                                <td style="padding:0px;padding-left:5px;">
									<textarea id="keterangan" name="keterangan" class="standard-input" style="resize: none;margin: 0px; width: 98%; height: 60px;">'.(!empty($keterangan)?$keterangan:'').'</textarea>
                                </td>
                            </tr> 
                            <tr>
                            	<td colspan="3" class="buttongroup">
                                    <input type="button" id="close_frm_mediasi_verzet" value="Kembali" class="btn">';
                                    if ($action=='edit'){                                
                                        echo'<input style="margin-left:3px" type="button" id="hapus" value="Hapus" style="text-decoration:none" class="btn btn-danger">';
                                    }
                                    echo' ';
                                    echo'<input type="submit" id="simpan" value="Simpan" onClick="return check_duplicate()" class="btn">
                                </td>
                            </tr>
                            <input type="hidden" id="curr_date" value="'.$current_date.'">
                            <input type="hidden" id="action" name="action" value="'.$action.'">
                            <input type="hidden" id="refresh" class="detil_perkara_refresh" value="0">
                            <input type="hidden" name="enc" value='.$enc.'>
                            <input type="hidden" id="close_form_refresh" value="">
                            <input type="hidden" name="enc_pendaftaran" id="enc_pendaftaran" value="'.$default_tgl_pendaftaran.'">
                    	</tbody>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>';?>
<script type="text/javascript">
    function popup_mediator(val){
		$('.first_add').show();
		popup_form2nd('<?php echo base_url("popup_mediator");?>'+'/'+0+'/'+val+'/'+'N');
	}
	
    $('#close_form_refresh').closingFormWin();    
    <?php 
    if($this->session->userdata('jenis_pengadilan')==1){
    ?>
        $('#close_frm_mediasi_verzet').click(function(){
            window.open("<?php echo base_url('perkara_detil/'.$enc.'/24');?>","_self");    
        });    
    <?php
    }else if($this->session->userdata('jenis_pengadilan')==4){
    ?>
        $('#close_frm_mediasi_verzet').click(function(){
            window.open("<?php echo base_url('perkara_detil_agama/'.$enc.'/26');?>","_self");    
        });        
    <?php
    }else{
    ?>
        $('#close_frm_mediasi_verzet').closingFormWin();
    <?php
    }
    ?> 
    $('#hapus').ButtonClickHandling({
        refresh:'1',
        setRefresh:1,
        divRef:'#konten',
        clicked:1,
        btnClicked:'#close_form_refresh',
        whenStatus:1,
        actions:'<?php echo base_url();?>mediasi_diluar/hapus_tgl_kesepakatan/<?php echo $enc;?>',
        formName:'#frm_mediasi_diluar',
        actionName:'Hapus Data'
    });
    
    $('#tgl_mediasi').datepicker({
                inline: true,
                showOtherMonths: true,
                dateFormat: 'dd/mm/yy',
                dayNamesMin: ['Ming', 'Sen', 'Sel', 'Rab', 'Kam', 'Jum', 'Sab'],
                onSelect: function(dateText, inst) {
                	var error = 0;
                    var tmp = $(this).val().split("/");
    				var tmp2 = $('#curr_date').val().split("/"); 
                    var tmp3 = $('#enc_pendaftaran').val().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;
                        $(this).val('');
    				}
                    if((new Date(tmp[2]+"-"+tmp[1]+"-"+tmp[0]+"T00:00:00").getTime() > new Date(tmp3[2]+"-"+tmp3[1]+"-"+tmp3[0]+"T00:00:00").getTime())){
                        message = 'Tidak Dapat Memilih Melebihi Tanggal Pendaftaran. Pendaftaran Tanggal '+$('#enc_pendaftaran').val();
                        error = 1;
                        $(this).val('');
                    }
    				if(error==1){
    					message_error_show(message);
    					$(this).css("background-color", "red");
    				}else{
    					$(this).css("background-color", "#dedbdb");
    				}
    				error = 0;
            	}
    });

    $('.datepicker').ValidateDateInput();
    $(document).ready(function(){  
        var counter='<?php echo $counter;?>';
        if(counter>0){
			var count=counter-1;
		}else{
			var count=counter;
		}
        $('#add').click(function(){  
           ++count; 
           $('#container').append('<div id="row'+count+'"><input type="text" id="mediator'+ count + '" name="mediator[]" placeholder="Klik Kolom untuk memilih mediator" class="standard-input" style="width:251px;" required="true" oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" onclick="popup_mediator('+count+')" /><input class="mediator_id'+ count + '" type="hidden" name="mediatorid[]"/> <input class="status'+count+'" type="hidden" name="status[]"/><input type="hidden" class="enk_id'+count+'" name="enk_id[]"/><a href="#" id="'+count+'" 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();
        });  
    });

    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;
    }

    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_diluar').SubmitHandling({
	refresh:1,
	setRefresh:1,
	clicked:1,
	btnClicked:'#close_frm_mediasi_verzet',
	whenStatus:1,
	isCKEdit:1,
	tdID:'isi_kesepakatan_text',
	textID:'isi_kesepakatan'
});
</script>
