<script src="<?php echo base_url();?>resources/js/autoNumeric.js"></script>
<div class="centering" style="width:57%;margin-top: 1%;">
	<div id='pihak_pilih' style="background:white;padding:5px;  padding-left: 0px;">
		<div class="detil" style="font-size:14px;margin-top:25px;width: 99%;margin: 0 auto;border-color: #FFFFFF;">
			<?php
			if($error==TRUE){
				if($error_message=='404'){
					echo "<h1>PAGE NOT FOUND</h1>";
					exit();
				}else{
					echo "<h1>".$error_message."</h1>";
				}
			}
			?>
			<form id="frm_putusan_terdakwa" style="background:white;margin:0;" method="post" action="<?php echo base_url().'managePutusanAkhirTerdakwa/validateInput';?>">
			     <table id='infoPerkara' style="font-size:14px;width:95%;margin-left:5px;">
                    <col width="25%">
                    <col width="75%">
                    <tbody>
                        <tr>
                            <td id="first-child">Tanggal Putusan<font color="red">*</font></td>
                            <td>
                                <input id="tgl_putusan_terdakwa" name="tgl_putusan_terdakwa" value="<?php echo (!empty($tanggal_putusan)? $tanggal_putusan:date('d/m/Y'));?>" class="datepicker" maxlength="10" placeholder="tgl/bln/tahun">
                            </td>
                        </tr>
                    </tbody>
                </table>
            <div class="cssTable">
				<table id='tablePerkaraAll' style="font-size:14px;">
					<col width="5%">
					<col width="5%">
					<col width="39%">
					<col width="17%">
					<col width="17%">
					<col width="17%">
					<tbody>
						<tr>
							<td>No</td>
							<td colspan="2">Jenis Putusan</td>
							<td colspan="3">Vonis</td>
                            <input type="hidden" id='enc_pihak' name="enc_pihak" value="<?php echo (!empty($enc_pihak) ? $enc_pihak : '');?>">
                            <input type="hidden" id='enc' name="enc" value="<?php echo $enc;?>">
						</tr>
						<?php
						if($listJenisPutusan!=''){
							if($listJenisPutusan->num_rows>0){
								$i=1;
								foreach ($listJenisPutusan->result() as $row) {
									echo "<tr>";
										echo "<td>".$i."</td>";
										echo "<td><input type='checkbox' class='ckcbox_".$row->id."' placeholder='".$row->satuan."' name='checkbox' id='ckcbox_".$row->id."'></td>";
										echo "<td valign='middle' id='td_ckcbox_".$row->id."'>".$row->nama."</td>";
									if($row->satuan==1){
                                        echo "<td><input id='ckcbox_".$row->id."_tahun' maxlength='3' class='input-terdakwa' nama='tahun' type='text' style='width:50px;'> Tahun</td>";
		                                echo "<td><input id='ckcbox_".$row->id."_bulan' maxlength='3' class='input-terdakwa' nama='bulan' type='text' style='width:50px;'> Bulan</td>";
		                                echo "<td><input id='ckcbox_".$row->id."_hari' maxlength='3' class='input-terdakwa' nama='hari' type='text' style='width:50px;'> Hari</td>";
		                            }elseif($row->satuan==2){
		                                echo "<td colspan='3'>Jumlah/Rp.<input id='input_ckcbox_".$row->id."' class='input-terdakwa-text' data-a-sep='.' data-a-dec=',' type='text'></td>";
		                            }elseif($row->satuan==4){
                                        echo '<td colspan="3"><textarea class="standard-input" id="ckcbox_'.$row->id.'" name="ckcbox_'.$row->id.'" style="width: 100%; height: 70px;resize: none;"></textarea></td>';
                                    }else{
                                        echo "<td colspan='3'></td>";
                                    }
									echo "</tr>";
									$i++;
								}
							}
						}
						?>
						 <tr>
                            <td colspan="7" style="background:white;border:0px;" align="center">
                                <input type="button" id='close_form_putusan_terdakwa' value="Kembali" class="btn">
                                <input type="button" id="btnSimpanPutusanTerdakwa" value="Simpan" class="btn">
                            </td>
                        </tr>
					</tbody>
				</table>
			</div>
			</form>
		</div>
	</div>
</div>
<script type="text/javascript">
(function() {
   $('#close_form_putusan_terdakwa').closingForm2ndWin();

})();
function disableinput(idinput){
    $(idinput).prop('disabled', true);
    $(idinput).prop('readonly', true);
    $(idinput).val('');
}
function enableinput(idinput){
    $(idinput).prop('disabled', false);
    $(idinput).prop('readonly', false);
    $(idinput).val(0);
}
<?php
if($listJenisPutusan!=''){
	if($listJenisPutusan->num_rows>0){
		foreach ($listJenisPutusan->result() as $row) {
			echo "disableinput('#ckcbox_".$row->id."_tahun');";
			echo "disableinput('#ckcbox_".$row->id."_bulan');";
			echo "disableinput('#ckcbox_".$row->id."_hari');";
			echo "disableinput('#input_ckcbox_".$row->id."');";
			if($row->satuan==2){
				echo "$('#input_ckcbox_".$row->id."').autoNumeric('init', {mDec: '0'});";
			}
			#echo "alert('".$row->id."');";
			echo "$('#ckcbox_".$row->id."').change(function() {
				if(this.checked) {
                    enableinput('#ckcbox_".$row->id."_tahun');
                    enableinput('#ckcbox_".$row->id."_bulan');
                    enableinput('#ckcbox_".$row->id."_hari');
                    enableinput('#input_ckcbox_".$row->id."');
                }else{
                	disableinput('#ckcbox_".$row->id."_tahun');
                    disableinput('#ckcbox_".$row->id."_bulan');
                    disableinput('#ckcbox_".$row->id."_hari');
                    disableinput('#input_ckcbox_".$row->id."');
                }
                });";
		}
	}
}

foreach ($status_putusan->result() as $row){
    if($row->pihak2_id==$idpihak){
        echo "$('#tgl_putusan_terdakwa').val('".$this->tanggalhelper->convertToInputDate($row->tanggal_putusan)."');";
        echo "$('#ckcbox_".$row->status_putusan_id."').attr('checked', true);";
        if($row->satuan==1){
            $vonis = $this->tanggalhelper->parse_day_to_year($row->durasi_nominal);
            echo    "enableinput('#input_ckcbox_".$row->status_putusan_id."');
                    enableinput('#ckcbox_".$row->status_putusan_id."_tahun');
                    enableinput('#ckcbox_".$row->status_putusan_id."_bulan');
                    enableinput('#ckcbox_".$row->status_putusan_id."_hari');";
            echo "$('#ckcbox_".$row->status_putusan_id."_tahun').val('".$vonis['year']."');";
            echo "$('#ckcbox_".$row->status_putusan_id."_bulan').val('".$vonis['month']."');";
            echo "$('#ckcbox_".$row->status_putusan_id."_hari').val('".$vonis['day']."');";
            
        }
        if($row->satuan==2){
            echo "enableinput('#input_ckcbox_".$row->status_putusan_id."');";
            echo "$('#input_ckcbox_".$row->status_putusan_id."').val('".number_format($row->durasi_nominal,0,',','.')."');";
        }
    }

}
?>
$('#tgl_putusan_terdakwa').ValidateDateInput();
$('#tgl_putusan_terdakwa').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("/");
                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();
                    message_error_show(message);
                    $(this).css("background-color", "red");
                }else{
                    $(this).css("background-color", "#dedbdb");
                }
            }
        });

function getCheckedBoxes(chkboxName) {
  var checkboxes = document.getElementsByName(chkboxName);
  var checkboxesChecked = new Array();
  
  var classname = '';
  var satuan = '';
  var tahun ='';
  var bulan = '';
  var hari = '';
  var nominal = '';
  var vonis = '';
  var idStatusPutusan = '';
  // loop over them all
  for (var i=0; i<checkboxes.length; i++) {
     // And stick the checked ones onto an array...
     if (checkboxes[i].checked) {
        var values = new Array();
        classname = checkboxes[i].className;
        satuan = $(checkboxes[i]).attr('placeholder');
        vonis = 0;
        if(satuan == 1){
            year = $('#'+classname+'_tahun').val();
            if(year==''){
                year = 0;
            }
            month = $('#'+classname+'_bulan').val();
            if(month==''){
                month = 0;
            }
            day = $('#'+classname+'_hari').val();
            if(day==''){
                day = 0;
            }
            vonis = parseInt(parseInt(year*12*30)+parseInt(month*30)+parseInt(day));
        }else if(satuan == 2){
            nominal = $('#input_'+classname).val();
            vonis = nominal.replace(/\./g,'');
        }else if(satuan == 4){
            vonis = $('textarea#'+classname).val();
        }else{
            vonis = 0;
        }
        idstatusputusan = classname.replace('ckcbox_','');
        values[0] = parseInt(idstatusputusan);
        values[1] = vonis;
        values[2] = satuan;
        checkboxesChecked.push(values);
     }
  }
  // Return the array if it is non-empty, or null
  return checkboxesChecked.length > 0 ? checkboxesChecked : '';
}
$("#btnSimpanPutusanTerdakwa").click(function(e){
    openLoadingDialog();
    var datas = getCheckedBoxes("checkbox");
    var lolosvalidation = true;
    for (var i=0; i<datas.length; i++) {
        if(datas[i][2] == 1 || datas[i][2]==2){
            if(datas[i][1]==''){
                var tds = $('#td_ckcbox_'+datas[i][0]);
                closeLoading()
                message_error_show('Mohon Mengisikan Vonis pada '+tds.text());
                lolosvalidation = false;
                return
            }
            if(datas[i][2]==2 && datas[i][1]==0){
                var tds = $('#td_ckcbox_'+datas[i][0]);
                closeLoading()
                message_error_show('Mohon Mengisikan Nilai Denda pada '+tds.text());
                return
            }
        }
    }
    if(lolosvalidation == false || datas.length<1){
        closeLoading()
        message_error_show('Mohon Mengisikan Jenis Putusan');
        return;
    }

    var toSend = {
        datas : datas,
        tgl_putusan : $('#tgl_putusan_terdakwa').val(),
        enc:'<?php echo $enc;?>',
        enc_pihak:'<?php echo $enc_pihak;?>',
        act:'<?php echo $act;?>',
        keterangan:$('#keterangan').val()
    }

    $.post($('#frm_putusan_terdakwa').attr('action'),toSend).done(function( response ){
        var json = jQuery.parseJSON(response);
            closeLoading()
            if(json.st==1){
                message_show_then_back(json.msg,'close_form_putusan_terdakwa')
                refreshDataPutusan();
                $('#close_msg_windows').click();
            }else{
                message_error_show(json.msg);
                return;
            }
    },'json');
});
$('#frm_putusan_terdakwa').on('click', 'input[type=checkbox]', function() {
    if($(this).attr('id')=='ckcbox_20' && $(this).is(":checked")){
        $('input[type=checkbox]').not($(this)).prop('checked', false);
    }
});
</script>
<?php


function ischecked($idstatusputusan,$datas){
	foreach ($datas->result() as $row) {
		if($row->id==$idstatusputusan){
			return 1;
		}
	}
	return 0;
}
?>