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

$pihakpenggugat = 0;
$pihaktergugat = 0;
?>
<div class="centering" style="width:1064px;margin-top: 1%;">
    <div id='rekonvensi' style="border: 1px solid #ffffff;">
		<div class="detil" style="padding: 2px;background-color: white;">
			<form id="frm_rekonvensi" method="post" action="<?php echo base_url().'rekonv_validate_input/val';?>">
                <table id="tableDetilPerkara" style="font-size:14px;">
                    <col width="30%">
                    <col width="50%">
                    <col width="20%">
                    <tbody>
                        <tr>
                        	<td id="first-child">Tanggal Pendaftaran Rekonvensi<font color="red">*</font></td>
                        	<td colspan="2">
                        	   <input id="tgl_pendaftaran" name="tgl_pendaftaran" value="<?php echo $this->tanggalhelper->convertToInputDate($tanggal_pendaftaran);?>" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun">
                        	</td>
                        </tr>
                        <tr>
                            <td id="first-child" colspan="3" class="divider">Para Pihak Asal</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 colspan='2' style="color:#444;background-color: #fff;"><?php echo $row->nama;?></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) {?>
                            <tr>
                                <td colspan='2' style="color:#444;background-color: #fff;"><?php echo $row->nama;?></td>
                            </tr>
                        <?php }
                        if($list_turut_tergugat->num_rows>0){
                            echo '<tr>';
                                echo '<td style="vertical-align: top;" rowspan="'.($list_turut_tergugat->num_rows+1).'">Turut Tergugat</td>';
                            echo '</tr>';
                            foreach ($list_turut_tergugat->result() as $row) {
                                echo '<tr>';
                                    echo '<td colspan="2" style="color:#444;background-color: #fff;">'.$row->nama.'</td>';
                                echo '</tr>';
                            }
                        }?>
                        <tr>
                            <td id="first-child" colspan="3" class="divider">Pihak Rekonvensi</td>
                        </tr>
                        <tr>
                            <td id="first-child" style="vertical-align: top;" rowspan="<?php echo ($list_tergugat->num_rows+1)+($list_turut_tergugat->num_rows);?>">Penggugat Rekonvensi</td>
                        </tr>
                        <?php foreach ($list_tergugat->result() as $row) {
                            $isChecked ='';
                            $urutans =0;
                            if($parapihakrekonvensi->num_rows>0){
                                $urutans = isPemohon($parapihakrekonvensi,$row->pihak_id);
                                if($urutans!=0){
                                    $isChecked = 'checked';
                                    $pihaktergugat = 0;
                                }
                            }
                            ?>
                        <tr>
                            <td style="color:#444;background-color: #fff;"s>
                            <?php echo '<input status="ck_pihak2" id="ck_pihak_'.$row->pihak_id.'" value="'.$row->pihak_id.'_2" name="para_pihak2[]" class="checkbox_status" type="checkbox" '.$isChecked.'>'.$row->nama.'</br>';?>
                            </td>
                            <td class="st_pihak2"  id="st_ck_pihak_<?php echo $row->pihak_id;?>">
                                <span class="custom-dropdown custom-dropdown--emerald" style='width:150px;'>
                                    <select style="width:150px" 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">Penggugat Ke- 1</option>';
                                        }elseif($list_tergugat->num_rows>0){
                                            $i=1;
                                            echo '<option value="">Penggugat Ke-</option>';
                                            foreach ($list_tergugat->result() as $data) {
                                                if($urutans==$i){
                                                    $selected = 'selected';
                                                }
                                                echo '<option value="'.$i.'" '.$selected.'>Penggugat Ke- '.$i.'</option>';
                                                $i++;
                                            }
                                            foreach ($list_turut_tergugat->result() as $data) {
                                                echo '<option value="'.$i.'" '.$selected.'>Penggugat Ke- '.$i.'</option>';
                                                $i++;
                                            }
                                        }?>
                                    </select>
                                </span>
                            </td>
                        </tr>
                            <?php 
                            }?>
                        <?php foreach ($list_turut_tergugat->result() as $row) {
                            $isChecked ='';
                            $urutans =0;
                            if($parapihakrekonvensi->num_rows>0){
                                $urutans = isPemohon($parapihakrekonvensi,$row->pihak_id);
                                if($urutans!=0){
                                    $isChecked = 'checked';
                                    $pihaktergugat = 0;
                                }
                            }
                            ?>
                            <td style="color:#444;background-color: #fff;">
                            <?php echo '<input status="ck_pihak2" id="ck_pihak_'.$row->pihak_id.'" value="'.$row->pihak_id.'_4" name="para_pihak2[]" class="checkbox_status" type="checkbox" '.$isChecked.'>'.$row->nama.'</br>';?>
                            </td>
                            <td style="color:#444;background-color: #fff;" class="st_pihak2"  id="st_ck_pihak_<?php echo $row->pihak_id;?>">
                                <span class="custom-dropdown custom-dropdown--emerald" style='width:150px;'>
                                    <select style="width:150px" 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_turut_tergugat->num_rows==1){
                                            echo '<option value="1">Penggugat Ke- 1</option>';
                                        }elseif($list_turut_tergugat->num_rows>0){
                                            echo '<option value="">Penggugat Ke-</option>';
                                            $i=1;
                                            foreach ($list_tergugat->result() as $data) {
                                                echo '<option value="'.$i.'" '.$selected.'>Penggugat Ke- '.$i.'</option>';
                                                $i++;
                                            }
                                            foreach ($list_turut_tergugat->result() as $data) {
                                                if($urutans==$i){
                                                    $selected = 'selected';
                                                }
                                                echo '<option value="'.$i.'" '.$selected.'>Penggugat Ke- '.$i.'</option>';
                                                $i++;
                                            }
                                        }?>
                                    </select>
                                </span>
                            </td>
                        </tr>
                            <?php 
                            }?>
                        <tr>
                            <td id="first-child" style="vertical-align: top;" rowspan="<?php echo ($list_penggugat->num_rows+1);?>">Tergugat Rekonvensi</td>
                        </tr>
                       	<?php foreach ($list_penggugat->result() as $row) {?>
                        <tr>
                            <td style="color:#444;background-color: #fff;">
                                    <?php
                                    $isChecked ='';
                                    $urutans =0;
                                    if($parapihakrekonvensi->num_rows>0){
                                        $urutans = isPemohon($parapihakrekonvensi,$row->pihak_id);
                                        if($urutans!=0){
                                            $isChecked = 'checked';
                                            $pihakpenggugat = 1;
                                        }
                                    }
                                    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>
                            <td style="color:#444;background-color: #fff;"class="st_pihak1" id="st_ck_pihak_<?php echo $row->pihak_id;?>">
                                <span class="custom-dropdown custom-dropdown--emerald" style='width:150px;'>
                                    <select style="width:150px" 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"> Tergugat Ke- 1</option>';
                                        }elseif($list_penggugat->num_rows>0){
                                            $i=1;
                                            echo '<option value="">Tergugat Ke-</option>';
                                            foreach ($list_penggugat->result() as $data) {
                                                if($urutans==$i){
                                                    $selected = 'selected';
                                                }
                                                echo '<option value="'.$i.'" '.$selected.'>Tergugat Ke- '.$i.'</option>';
                                                $i++;
                                            }
                                        }?>
                                    </select>
                                </span>
                            </td>
                        </tr>
                        <?php }?>
                        <tr>
                            <td id="first-child">Isi Petitum</td>
                            <td style="background:white;" id='isi_petitum_text' colspan='2'>
                                <?php echo $this->ckeditor->editor("isi_petitum",$isi_petitum);?>
                            </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' value="Kembali" class="btn close_form_add">
                                <?php
                                if(!empty($tanggal_pendaftaran)){
                                    echo '<input type="submit" id="hapus" value="Hapus" class="btn btn-danger" onclick="return false;">';
                                }?>
                                <input id="simpan" class="btn" type="submit" value="Simpan">
                                <input type="hidden" id='close_form_refresh' value="">
				    		</td>
				    	</tr>
                    </tbody>
                </table>               	
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $('#frm_rekonvensi').SubmitHandling({
		refresh:'1',
		setRefresh:1,
		divRef:'#konten',
		clicked:1,
		btnClicked:'#close_form_add',
		whenStatus:1,
        isCKEdit:1,
        tdID:'isi_petitum_text',
        textID:'isi_petitum',
        next_step:1

    });
    $('#hapus').ButtonClickHandling({
        refresh:'1',
        setRefresh:1,
        divRef:'#konten',
        clicked:1,
        btnClicked:'#close_form_refresh',
        whenStatus:1,
        actions:'<?php echo base_url("del_gugatan_rekonve/enc");?>',
        formName:'#frm_rekonvensi',
        actionName:'Hapus Data Pendaftaran Rekonvensi'
    });
    $('#close_form_refresh').closingFormWin();
    <?php 
    if($this->session->userdata('jenis_pengadilan')==1){
    ?>
        $('#close_form_add').click(function(){
            window.open("<?php echo base_url('perkara_detil/'.$enc.'/22');?>","_self");    
        });           
    <?php
    }else if($this->session->userdata('jenis_pengadilan')==4){
    ?>
        $('#close_form_add').click(function(){
            window.open("<?php echo base_url('perkara_detil_agama/'.$enc.'/22');?>","_self");    
        });            
    <?php
    }else{
    ?>
        $('#close_form_add').closingFormWin();
    <?php
    }
    ?>
    $('#tgl_pendaftaran').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_rekonvensi').on('click', '.checkbox_status', function() {
        var pihakstatus = $(this).attr('id');
        if($(this).is(":checked")) {
            if($(this).attr('status')=='ck_pihak1'){
                $('#st_'+$(this).attr('id')).fadeIn();
            }else if($(this).attr('status')=='ck_pihak2'){
                $('#st_'+$(this).attr('id')).fadeIn();
            }
        }else{
            $('#st_'+$(this).attr('id')).hide();
        }
});
$( document ).ready(function() {

    
});
</script>