<?php
if($this->session->userdata('is_logged_in')){
	if($this->session->userdata('idgroup')>2 AND $this->session->userdata('idgroup')!=702){
		redirect('login');
	}
}else{
	redirect('login');
}
function parse_date($date){
	if(!empty($date)){
		$date_fmt = new DateTime(str_replace("-","",$date));
		return $date_fmt->format('d M Y');
	}else{
		return '';
	}
}

?>
<hr class="thick-line" style="clear:both;">
<div id=''>
	<div style="font-size:14px;margin-top:25px;width: 70%;margin: 0 auto;border-color: #FFFFFF;">
		<a style="color: " class="btn" onclick="show_edit(0)">
		<i class="fa fa-plus fa-lg"></i> Tambah Saldo</a>
	</div>
	<div class="cssTable" style="font-size:14px;margin-top:25px;width: 70%;margin: 0 auto;border-color: #FFFFFF;">
				<table style="font-size:14px;">
				<col width="10%">
				<col width="10%">
				<col width="10%">
				<col width="10%">
				<tbody>
					<tr>
						<td>Tahun</td>
						<td>Jumlah Saldo Awal</td>
						<td>Keterangan</td>
						<td>Action</td>
					</tr>
						<?php
						if($alur_perkara_list!=''){
							if($alur_perkara_list->num_rows>0){
								foreach ($alur_perkara_list->result() as $row) {
									echo "<tr>";
									echo "<td align='center'>".($row->tahun_saldo)."</td>";
									echo "<td align='right'>".number_format($row->jumlah_saldo_awal, 0, ',', '.')."</td>";
									echo "<td>".$row->keterangan."</td>";
									echo '<td style="cursor:pointer;" align="center" onclick="show_edit('.$row->saldo_id.')">[<a href="#">edit</a>]</td>';
									echo "</tr>";
								}
							}else{
								echo "<tr><td colspan='4' style='text-align:center;'>Data belum tersedia</td></tr>";	
							}
						}
						?>
				</tbody>
			</table>
	</div>
</div>

<div id="popup_saldo_awal" class="popup">
	<div class="centering" style="width:500px;margin-top: 5%;z-index:-100">
		<div id='saldo_awal' style="border: 1px solid #ffffff;">
			<div class="detil" style="padding: 2px;background-color: white;">
				<form id="frm_saldo_awal" style="background:white;margin:0;" method="post" action="<?php echo base_url().'saldo_awal/validate_input';?>">
				<table id="tableDetilPerkara">
					<col width="38%">
					<col width="62%">
					<tbody>
						<tr>
							<td>Saldo Awal Tahun <font color="red">*</font></td>
							<td>
								<select class="standard-input" style="width: 64px"; id="tahun_saldo" name="tahun">
								<?php 
									foreach ($tahun_saldo->result() as $data) {
										echo "<option value='".$data->tahun."'>".$data->tahun."</option>";
									}
									?>
								</select>
								<input type="hidden" id='action' name='action' class="standard-input">
							</td>
						</tr>
						<tr>
							<td>Jumlah Saldo Awal Tunai <font color="red">*</font></td>
							<td>
								<input type="text"  id='saldo_awal_tunai' name='saldo_awal_tunai' data-a-sep="." data-a-dec="," class="standard-input" placeholder="0">
							</td>
						</tr>
						<tr>
							<td>Jumlah Saldo Awal Bank <font color="red">*</font></td>
							<td>
								<input type="text" id='saldo_awal_bank' name='saldo_awal_bank' data-a-sep="." data-a-dec="," class="standard-input" placeholder="0">
							</td>
						</tr>
						<tr>
							<td>Total Saldo</td>
							<td>
								<input type="text" id='total_saldo' name='total_saldo' data-a-sep="." data-a-dec="," class="standard-input" value="0.00" disabled="">
							</td>
						</tr>
						<tr>
							<td>Keterangan</td>
							<td>
								<textarea id="keterangan" name="keterangan" class="standard-input" style="resize: none;margin: 0px; width: 287px; height: 155px;"></textarea>
							</td>
						</tr>
						<tr>
							<td colspan="2" style="background:white;" align="center">
							<input type="button" onClick='kembali_saldo_awal();' id="back" value="Kembali" class="btn">
							<input type="submit" value="Simpan" class="btn"></td>
							<input type="hidden" id="bulik" value="bulik">
						</tr>
						
					</tbody>
				</table>
			</form>
		</div>
	</div>
</div>
<script src="<?php echo base_url();?>resources/js/autoNumeric.js"></script>
<script type="text/javascript">
$('#bulik').click(function(){
	window.open('<?php echo base_url()?>saldo_awal_edit','_self');
});

function show_edit(id){
	$('#action').val(id);
	$.post( "<?php echo base_url()?>saldo_awal/info",{
        saldo_id:id
    }).done(function( response ) {
    	var json = jQuery.parseJSON(response);
        if(json.st==1){
        	$('#tahun_saldo').val(json.tahun_saldo);
        	$('#saldo_awal_tunai').val(json.saldo_awal_tunai);
        	$('#saldo_awal_bank').val(json.saldo_awal_bank);
        	$('#total_saldo').val(json.total_saldo);
        	$('#keterangan').val(json.ket);
        	$('#popup_saldo_awal').fadeIn();
        }else{
        	message_error_show_parent('Error, Please refresh your browser and clear the cache')
        }
    },'json');
}

function kembali_saldo_awal(){
	$('#popup_saldo_awal').fadeOut();
	if($('#refresh').val()==1){
		window.open('<?php echo base_url()?>saldo_awal_edit','_self');
	}
	$(':input').not('.btn').val('');
	$('.hidden').hide();
	closeLoading();
}

$( document ).ready(function() {
    $('#tgl_saldo').datepicker({
            inline: true,
            showOtherMonths: true,
            dateFormat: 'dd/mm/yy',
            dayNamesMin: ['Ming', 'Sen', 'Sel', 'Rab', 'Kam', 'Jum', 'Sab'],
            });
    $('#tgl_saldo').keyup(function(e){
    	if( (e.keyCode<8 || e.keyCode>57)){
    		return;
    	}
    });
    
	$('#saldo_awal_bank,#saldo_awal_tunai').autoNumeric('init', {mDec: '0'});
    $('#saldo_awal_tunai').ForceNumericOnly();
    $('#saldo_awal_bank').ForceNumericOnly();
    $('#total_saldo').autoNumeric('init', {mDec: '0'});
    $('#total_saldo').ForceNumericOnly();

    $('#frm_saldo_awal').SubmitHandling({
	    refresh:'1',
	    setRefresh:1,
	    divRef:'#frm_saldo_awal',
	    clicked:1,
	    btnClicked:'#bulik',
	    whenStatus: 1
    });
});

$("#saldo_awal_bank,#saldo_awal_tunai").focusout(function(){
    if($(this).val()=='')$(this).val(0)
    var saldo_awal_bank = parseFloat($('#saldo_awal_bank').autoNumeric('get'));
    var saldo_awal_tunai = parseFloat($('#saldo_awal_tunai').autoNumeric('get'));
    var totals = parseFloat(saldo_awal_tunai+saldo_awal_bank);
    $('#total_saldo').val(totals);
    $("#total_saldo").autoNumeric('set', totals);
});
</script>