<?php
function isPemohon($parapihak,$idpihak){
    foreach ($parapihak->result() as $val) {
        if($val->pihak_id==$idpihak AND ($val->status_pihak_id !=5 AND $val->status_pihak_id !=3)){
            return $val->urutan_keberatan;
        }
    }
    return 0;
}

function getStatus($parapihak,$idpihak){
    foreach ($parapihak->result() as $val) {
        if($val->pihak_id==$idpihak AND ($val->status_pihak_id !=5 AND $val->status_pihak_id !=3)){
            return $val->status_pihak_text;
        }
    }
    return 0;
}

$pihakpenggugat = 0;
$pihaktergugat = 0;
?>
<div class="centering" style="width:615px;margin-top: 1%;">
    <div id='pihak_pilih' style="border: 1px solid #ffffff;">
		<div class="detil" style="padding: 2px;background-color: white;">
            <div>
				<?php
				if ($action=='add'){
                    $amar_putusan_keberatan='';
                    echo "<h3 align='center'>TAMBAH DATA PIHAK KEBERATAN</h3>";
				?>
				<form id="frm_keberatan" method="post" action="<?php echo base_url().'addKeberatan_c/f_validate_add_pihak';?>">
				<?php
				}
				?>
                	<table id="tableDetilPerkara" style="font-size:14px;">
                		<col width="30%">
                        <col width="45%">
                        <col width="25%">
                        <tbody>
                            <tr>
                                <td id="first-child" colspan="3" class="divider">Data Perkara</td>
                            </tr>
                        	<tr>
                        		<td id="first-child">Nomor Perkara</td>
                        		<td colspan="2">
                        			<span><?php echo $nomor_perkara;?></span>
                                    <input type='hidden' name='nomor_perkara' value='<?php echo $nomor_perkara;?>'>
                                    <input type='hidden' name='enc' value='<?php echo $enc;?>'>
                        		</td>
                        	</tr>
                        	<tr>
                        		<td id="first-child">Tanggal Putusan</td>
                        		<td colspan="2">
                        			<span><?php echo $this->tanggalhelper->convertDayDate($tanggal_putusan);?></span>
                                    <input type='hidden' id='tgl_putusan' name='tgl_putusan' value='<?php echo $this->tanggalhelper->convertToInputDate($tanggal_putusan);?>'>
                        		</td>
                        	</tr>
                        	<tr>
                        		<td id="first-child">Tanggal Pendaftaran Keberatan<font color="red">*</font></td>
                        		<td colspan="2">
                        			<input id="tgl_pendaftaran_keberatan" name="tgl_pendaftaran_keberatan" value="<?php echo $this->tanggalhelper->convertToInputDate($tanggal_pendaftaran_keberatan);?>" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun">
                        		</td>
                        	</tr>
                            <tr>
                                <td id="first-child" colspan="3" class="divider">Pihak Pemohon Keberatan</td>
                            </tr>
                            <tr>
                                <td id="first-child" style="vertical-align: top;" rowspan="<?php echo ($list_penggugat->num_rows+1);?>">Penggugat</td>
                            </tr>
                            <?php foreach ($list_penggugat->result() as $row) {?>
                                <tr>
                                    <td style="background:snow; color:#111;">
                                        <?php
                                        $isChecked ='';
                                        $urutans =0;
                                        if($parapihakkeberatan->num_rows>0){
                                            $urutans = isPemohon($parapihakkeberatan,$row->pihak_id);
                                            if($urutans!=0){
                                                $isChecked = 'checked';
                                                $pihakpenggugat = 1;
                                            }
                                        }
                                        if(!empty($isChecked)){
                                            echo $row->nama.'</br>';
                                        }else{
                                            echo '<input status="ck_pihak1" id="ck_pihak_'.$row->pihak_id.'" value="'.$row->pihak_id.'" name="para_pihak1[]" class="checkbox_status" type="checkbox" '.$isChecked.'>'.$row->nama.'</br>';
                                        }
                                        
                                        ?>
                                    </td>
                                    
                                    <?php
                                    if(!empty($isChecked)){
                                        echo '<td>';
                                        echo getStatus($parapihakkeberatan,$row->pihak_id).' Ke-'.$urutans;
                                    }else{
                                    ?>
                                    <td class="st_pihak1" id="st_ck_pihak_<?php echo $row->pihak_id;?>">
                                        <span class="custom-dropdown custom-dropdown--emerald" style='width:130px;'>
                                            <select style="width:130px" name="<?php echo 'pihak_'.$row->pihak_id;?>" id="<?php echo 'pihak_'.$row->pihak_id;?>" class="custom-dropdown__select custom-dropdown__select--white">
                                             <?php
                                                $selected ='';
                                                if($list_penggugat->num_rows==1){
                                                    echo '<option value="1">Pemohon Ke- 1</option>';
                                                }elseif($list_penggugat->num_rows>0){
                                                    $i=1;
                                                    echo '<option value="">Pemohon Ke-</option>';
                                                    foreach ($list_penggugat->result() as $data) {
                                                        if($urutans==$i){
                                                            $selected = 'selected';
                                                        }
                                                        echo '<option value="'.$i.'" '.$selected.'>Pemohon Ke- '.$i.'</option>';
                                                        $i++;
                                                    }
                                                }?>
                                            </select>
                                        </span>
                                    <?php
                                    }
                                    ?>
                                    </td>
                                </tr>
                            <?php 
                            }?>
                            <tr>
                                <td id="first-child" style="vertical-align: top;" rowspan="<?php echo ($list_tergugat->num_rows+1);?>">Tergugat</td>
                            </tr>
                            <?php foreach ($list_tergugat->result() as $row) {
                                $isChecked ='';
                                $urutans =0;
                                if($parapihakkeberatan->num_rows>0){
                                    $urutans = isPemohon($parapihakkeberatan,$row->pihak_id);
                                    if($urutans!=0){
                                        $isChecked = 'checked';
                                        $pihaktergugat = 0;
                                    }
                                }
                                ?>
                                <tr>
                                    <td style="background:snow; color:#111;" >
                                        <?php 
                                        if(!empty($isChecked)){
                                            echo $row->nama.'</br>';
                                        }else{
                                            echo '<input status="ck_pihak2" id="ck_pihak_'.$row->pihak_id.'" value="'.$row->pihak_id.'" name="para_pihak2[]" class="checkbox_status" type="checkbox" '.$isChecked.'>'.$row->nama.'</br>';
                                        }?>
                                    </td>
                                    <?php
                                    if(!empty($isChecked)){
                                        echo '<td>';
                                        echo getStatus($parapihakkeberatan,$row->pihak_id).' Ke-'.$urutans;
                                    }else{
                                    ?>
                                    <td class="st_pihak2"  id="st_ck_pihak_<?php echo $row->pihak_id;?>">
                                        <span class="custom-dropdown custom-dropdown--emerald" style='width:130px;'>
                                            <select style="width:130px" name="<?php echo 'pihak_'.$row->pihak_id;?>" id="<?php echo 'pihak_'.$row->pihak_id;?>" class="custom-dropdown__select custom-dropdown__select--white">
                                             <?php
                                                $selected ='';
                                                if($list_tergugat->num_rows==1){
                                                    echo '<option value="1">Pemohon Ke- 1</option>';
                                                }elseif($list_tergugat->num_rows>0){
                                                    $i=1;
                                                    echo '<option value="">Pemohon Ke-</option>';
                                                    foreach ($list_tergugat->result() as $data) {
                                                        if($urutans==$i){
                                                            $selected = 'selected';
                                                        }
                                                        echo '<option value="'.$i.'" '.$selected.'>Pemohon Ke- '.$i.'</option>';
                                                        $i++;
                                                    }
                                                }?>
                                            </select>
                                        </span>
                                    <?php
                                    }
                                    ?>
                                    </td>
                                    
                                </tr>
                            <?php 
                            }?>
                        	<tr>
                        		<td id="first-child">Keterangan</td>
                        		<td colspan="2">
                        			<span>
                        				<textarea id='keterangan' name='keterangan' style="margin: 0px; width: 100%; height: 68px;resize: none;"></textarea>
                        			</span>
                        		</td>
                        	</tr>
                        	<tr>
								<td colspan="3" style="background:white;padding:2px;">
									<input type="hidden" id="enc" name="enc" value="<?php echo $enc;?>">
									<input type="hidden" id="curr_date" value="<?php echo $current_date;?>">
								</td>
							</tr>
                        	<tr>
								<td colspan="3" style="background:white;" align="center">
									<input type="button" id='close_form_add_keberatan' value="Kembali" class="btn">
                                    <?php
                                    if(!empty($tanggal_pendaftaran_keberatan)){
                                        echo '<input type="submit" id="hapus" value="Hapus" class="btn btn-danger" onclick="return false;">';
                                    }?>
                                    <input id="simpan" class="btn" type="submit" value="Simpan">
								</td>
							</tr>
                        </tbody>
                	</table>               	
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $('#frm_keberatan').SubmitHandling({
		refresh:'1',
		setRefresh:1,
		divRef:'#konten',
		clicked:1,
		btnClicked:'#close_form_add_keberatan',
		whenStatus:1,
        next_step:1

    });
    $('#hapus').ButtonClickHandling({
        refresh:'1',
        setRefresh:1,
        divRef:'#konten',
        clicked:1,
        btnClicked:'#close_form_add_keberatan',
        whenStatus:1,
        actions:'<?php echo base_url("addKeberatan_c/validateDelete");?>',
        formName:'#frm_keberatan',
        actionName:'Hapus Data Permohonan Keberatan'
    });
    // $('#close_form_add_keberatan').closingFormWin();   
    <?php 
    if($this->session->userdata('jenis_pengadilan')==1){
    ?>
        $('#close_form_add_keberatan').click(function(){
            window.open("<?php echo base_url('perkara_detil/'.$enc.'/19');?>","_self");    
        });    
    <?php
    }else if($this->session->userdata('jenis_pengadilan')==4){
    ?>
        $('#close_form_add_keberatan').click(function(){
            window.open("<?php echo base_url('perkara_detil_agama/'.$enc.'/19');?>","_self");    
        });        
    <?php
    }else{
    ?>
        $('#close_form_add_keberatan').closingFormWin();
    <?php
    }
    ?>
    $('#tgl_pendaftaran_keberatan').datepicker({
        beforeShowDay: $.datepicker.noWeekends,
    	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("/");
			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(error==1){
				message_error_show(message);
				$(this).css("background-color", "red");
			}else{
				$(this).css("background-color", "#dedbdb");
			}
            error = 0;
        }
    });
    
    $('.datepicker').ValidateDateInput();
    $('#frm_keberatan').on('click', '.checkbox_status', function() {
        var pihakStatus = $(this).attr('id');
        if($(this).is(":checked")) {
            if($(this).attr('status')=='ck_pihak1'){
                $("input[status='ck_pihak2']:checkbox").prop('checked', false);
                $('#st_'+$(this).attr('id')).fadeIn();
                $('.st_pihak2').fadeOut();
            }else if($(this).attr('status')=='ck_pihak2'){
                $("input[status='ck_pihak1']:checkbox").prop('checked', false);
                $('.st_pihak1').fadeOut();
                $('#st_'+$(this).attr('id')).fadeIn();
            }
        }else{
            $('#st_'+$(this).attr('id')).hide();
        }
});
$( document ).ready(function() {
    var pengguggat = '<?php echo $pihakpenggugat;?>';
    var tergugat = '<?php echo $pihaktergugat;?>';
    if(pengguggat==0){
        $('.st_pihak1').hide();
    }
    if(tergugat==0){
        $('.st_pihak2').hide();
    }
    
});
</script>