<?php
echo'<div style="padding:10px;"></div>';
echo'<div class="detil" style="padding: 4px;font-size:14px;margin-top:25px;width: 44%;margin: 0 auto;border-color: #FFFFFF;">';
echo'<form id="frm_mediasi_verzet" style="padding: 4px;background:white;margin:0;" method="post" action="'.base_url().'savemediasiverzet'.'">';
echo'<table id="tableDetilPerkara" style="font-size:14px;">';
echo'<col width="40%"> <col width="60%"><tbody><tr>';
if($data_mediator==''){
	echo '<td colspan="2" class="header">Penetapan Mediasi</td>';
}else{
	echo '<td colspan="2" class="header">Edit Penetapan Mediasi</td>';
}
echo'</tr>
<tr id="tr_pihak">	
	<input type="hidden" id="tgl_sidang_verzet" name="tgl_sidang_verzet" value="'.$tgl_sidang_verzet.'">
	<input type="hidden" id="enc" name="enc" value="'.(!empty($idperkara)? $idperkara:'').'">
	<input type="hidden" id="mediasi_id" name="mediasi_id" value="'.(!empty($idmediasi)? $idmediasi:'').'">
	<input type="hidden" id="curr_date" value="'.$current_date.'">
	<input type="hidden" id="enc_act" name="enc_act" value="'.$enc_act.'">
	<input type="hidden" id="enc_id" name="enc_id" value="'.$enc_id.'">
	<td colspan="2" class="divider">Data Perkara Verzet</td>
</tr>
<tr>
	<td>Nomor Perkara</td>
		<td style="padding:0px;padding-left:5px;">'.(!empty($noperkarapn) ? $noperkarapn : '-').'</td>
	</tr>
	<tr>
		<td>Nomor Perkara Verzet</td>
		<td style="padding:0px;padding-left:5px;">'.(!empty($noperkarapn) ? $nomorperkaraverzet : '-').'</td>
	</tr>
	<tr>
		<td>Tanggal Pendaftaran Verzet</td>
		<td style="padding:0px;padding-left:5px;">'.(!empty($tglpermohonan) ? $tglpermohonan : '').'</td>
	</tr>
	<tr>
		<td colspan="2" class="divider">Data Penetapan Mediasi</td>
	</tr>
	<tr>
		<td>Tanggal Penetapan Mediator<font color="red">*</font></td>
		<td style="padding:0px;padding-left:5px;">
			<input id="tanggal_penetapan" name="tanggal_penetapan" value="'.(!empty($tanggal_penetapan) ? $tanggal_penetapan : '').'" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun">
		</td>
	</tr>
	<tr>
		<td>No. Penetapan Mediator</td>
		<td style="padding:0px;padding-left:5px;">
			<input type="text" id="sk_penetapan_mediator" name="sk_penetapan_mediator" value="'.(!empty($nomor_sk_penetapan) ? $nomor_sk_penetapan : '-').'" class="standard-input" style="width:251px;">
		</td>
	</tr>
	<tr>
		<td>Mediator<span style="color:red">*</span></td>
		<td style="padding:0px;padding-left:5px;">
		<div id="container">';
		if($data_mediator!=''){
			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="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="text" id="mediator'.$counter.'" name="mediator[]" value="'.$var->nama_mediator.'" required="true" oninvalid="InvalidMsg(this);" oninput="InvalidMsg(this);" 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[]" required="true" oninvalid="InvalidMsg(this);" oninput="InvalidMsg(this);" placeholder="Klik kolom untuk pilih mediator" onclick="popup_mediator(0)" class="standard-input" 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 '<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>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>';
		if($info_data_dukung!=''){
			if($info_data_dukung->num_rows>0){
				foreach($info_data_dukung->result() as $var){
					if($var->identifier==='#pilih_mediator#'){
						$uraian=$var->uraian;
					}
				}
			}
		}
		if($uraian==='sepakat memilih Mediator'){
			$selected1 ="selected";
		}else if($uraian==='sepakat untuk menyerahkan kepada Majelis Hakim untuk menunjuk Mediator dari Daftar Mediator'){
			$selected2 ="selected";
		}else{
			$selected='';
		}
		echo'<option value="sepakat memilih Mediator"'.$selected1.'>Pihak Sepakat Memilih Mediator</option>
		<option value="sepakat untuk menyerahkan kepada Majelis Hakim untuk menunjuk Mediator dari Daftar Mediator"'.$selected2.'>Majelis Hakim Memilih Mediator</option>
		</select>
		</td>
	</tr>
	<tr>
		<td>Tanggal Mulai Mediasi<font color="red">*</font></td>
		<td id="tgl_mulai_mediasi" style="padding:0px;padding-left:10px;">'; 
		if (!empty($tgl_mediasi)){
			echo $this->tanggalhelper->convertDayDate($tgl_mediasi); 
		}
	echo'</td>
	</tr>
	<tr>
		<td colspan="2" style="background:white;" align="center">
		<input type="button" value="Kembali" id="kembali" class="btn">'; 
		if(!empty($idmediator)){
			echo '<input style="margin-left:3px" type="button" id="hapus" value="Hapus" style="text-decoration:none" class="btn btn-danger">';
		}
		echo'<input style="margin-left:3px" type="submit" id="simpan" value="Simpan" onClick="return check_duplicate()" class="btn">
		</td>
	</tr>
	</tbody>
</table>
</form>
</div>
<div style="padding:5px;"></div>';?>
<script type="text/javascript">
function popup_mediator(val){
$('.first_add').show();
	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;
}

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

// $('#kembali').closingFormWin();
	<?php 
    if($this->session->userdata('jenis_pengadilan')==1){
    ?>
        $('#kembali').click(function(){
            window.open("<?php echo base_url('perkara_detil/'.$enc.'/5');?>","_self");    
        });
    <?php
    }else if($this->session->userdata('jenis_pengadilan')==3){
    ?>
        $('#kembali').click(function(){
            window.open("<?php echo base_url('perkara_detil_tun/'.$enc.'/5');?>","_self");    
        });
    <?php
    }else if($this->session->userdata('jenis_pengadilan')==4){
    ?>
        $('#kembali').click(function(){
            window.open("<?php echo base_url('perkara_detil_agama/'.$enc.'/5');?>","_self");    
        });        
    <?php
    }else{
    ?>
        $('#kembali').closingFormWin();
    <?php
    }
    ?>
$('#hapus').ButtonClickHandling({
	refresh:1,
	setRefresh:1,
	clicked:1,
	btnClicked:'#kembali',
	whenStatus:1,
	actions:'<?php echo base_url()?>tab_verzet_c/hapus_mediasi_verzet/<?php echo $enc_id; ?>',
	formName:'#frm_mediasi_verzet',
	actionName:'Hapus Data'
});

$(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=" Klik kolom untuk pilih mediator" class="standard-input" style="width:251px;" 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(); 
    });  
});

$('.datepicker').ValidateDateInput();
$( function() {
    $('#tanggal_penetapan').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_sidang_verzet = $('#tgl_sidang_verzet').val();
            var tmp3 = tgl_sidang_verzet.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 Verzet';
                error = 1
            }

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

$('#frm_mediasi_verzet').SubmitHandling({
	refresh:1,
	setRefresh:1,
	clicked:1,
	btnClicked:'#kembali',
	whenStatus:1
});
</script>
