@extends('layout')

@section('content')
<style type="text/css">
.table_td_align_top > tbody > tr > td{
	vertical-align : top;
}
</style>
<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="#"><i class="icon-home2 position-left"></i> Keuangan</a></li>
			<li><a href="#">Kasbon Pegawai</a></li>
			<li class="active">Pembayaran Kasbon Pegawai</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">{{ (isset($kasbonBayar)) ? "Edit " : "" }}Pembayaran Kasbon Pegawai</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('piutang_pegawai') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>

    	<form class="" method="post" action="{{ url('piutang_pegawai/simpan_bayar_kasbon') }}">
    		{{ csrf_field() }}

    		<input type="hidden" name="id_transaksi_bayar" value="{{ (isset($kasbonBayar)) ? $kasbonBayar->id_transaksi_bayar : '' }}">
    		<input type="hidden" name="id_bayar" value="{{ (isset($kasbonBayar)) ? $kasbonBayar->id : '' }}">

	    <div class="panel-body">
	    		<div class="row">
		    		<div class="col-sm-3 col-md-3">
		    			<div class="form-group">
		    				{{ Form::label("Pegawai", "", ["class"=>"control-label"]) }}
		    				{{ Form::hidden("id_pegawai", $pegawaiById->id_pegawai) }}
		    				{{ Form::text("nama_pegawai", $pegawaiById->nama_pegawai, ["class"=>"form-control", "disabled"=>"disabled"]) }}
		    			</div>
		    		</div>
		    		<div class="col-sm-4 col-md-4">
		    			<div class="form-group">
		    				{{ Form::label("Akun Terima", "", ["class"=>"control-label"]) }}
		    				{{ Form::select("id_akun", $akunTerima, (isset($kasbonBayar)) ? $kasbonBayar->id_akun : null, ["class"=>"form-control set_selected"]) }}
		    			</div>
		    		</div>
	    		</div>

	    		<div class="row">
	    			<div class="col-sm-3 col-md-3">
		    			<div class="form-group">
		    				{{ Form::label("No. Penerimaan", "", ["class"=>"control-label"]) }}
		    				{{ Form::text("no_transaksi_bayar", (isset($kasbonBayar)) ? $kasbonBayar->no_transaksi_bayar : 1, ["class"=>"form-control"]) }}
		    			</div>
	    			</div>
	    			<div class="col-sm-3 col-md-3">
		    			<div class="form-group">
		    				{{ Form::label("Tanggal Terima", "", ["class"=>"control-label"]) }}
		    				{{ Form::text("tgl_bayar", (isset($kasbonBayar)) ? date("d/m/Y", strtotime($kasbonBayar->tgl_bayar)) : date("d/m/Y"), ["class"=>"form-control set_datepicker"]) }}
		    			</div>
	    			</div>
	    		</div>

	    </div>

			<table class="table table_td_align_top">
				<thead>
					<tr>
						<th>Tanggal</th>
						<th>No. Kasbon</th>
						<th>Untuk Keperluan</th>
						<th>Total</th>
						<th>Sisa Kasbon</th>
						<th>Pembayaran</th>
					</tr>
				</thead>
				<tbody>
					@if(isset($kasbonBayar))
					@foreach($kasbonByIdPegawai AS $key => $value)
					@php($sisa_kasbon = $value->nominal-$value->total_bayar+$value->jumlah_bayar)
					<tr>
						<td>{{ $value->tanggal_transaksi }}</td>
						<td>{{ $value->nomor_transaksi }}</td>
						<td>{{ $value->keterangan }}</td>
						<td>{{ $value->nominal }}</td>
						<td>{{ $sisa_kasbon }}</td>
						<td>
							{{ Form::hidden("detail_kasbon[id_kasbon][]", $value->id) }}
							{{ Form::number("detail_kasbon[nominal_bayar][]", $value->jumlah_bayar, ['class'=>'form-control text-right', 'onkeyup'=>'setFormatMoney($(this), \'td\'), set_total_bayar($(this))']) }}
							<span class="help-block text-right"></span>
						</td>
					</tr>
					@endforeach
					@else
					@php($sisa_kasbon = $pegawaiById->nominal-$pegawaiById->total_bayar)
					<tr class="info">
						<td>{{ $pegawaiById->tanggal_transaksi }}</td>
						<td>{{ $pegawaiById->nomor_transaksi }}</td>
						<td>{{ $pegawaiById->keterangan }}</td>
						<td>{{ $pegawaiById->nominal }}</td>
						<td>{{ $sisa_kasbon }}</td>
						<td>
							{{ Form::hidden("detail_kasbon[id_kasbon][]", $pegawaiById->id) }}
							{{ Form::number("detail_kasbon[nominal_bayar][]", $sisa_kasbon, ['class'=>'form-control text-right', 'onkeyup'=>'setFormatMoney($(this), \'td\'), set_total_bayar($(this))', 'min'=>0, 'max'=>$sisa_kasbon]) }}
							<span class="help-block text-right"></span>
						</td>
					</tr>
					@foreach($kasbonByIdPegawai AS $key => $value)
					@php($sisa_kasbon = $value->nominal-$value->total_bayar)
					@php
						if($sisa_kasbon==0) continue;
					@endphp
					<tr>
						<td>{{ $value->tanggal_transaksi }}</td>
						<td>{{ $value->nomor_transaksi }}</td>
						<td>{{ $value->keterangan }}</td>
						<td>{{ $value->nominal }}</td>
						<td>{{ $sisa_kasbon }}</td>
						<td>
							{{ Form::hidden("detail_kasbon[id_kasbon][]", $value->id) }}
							{{ Form::number("detail_kasbon[nominal_bayar][]", 0, ['class'=>'form-control text-right', 'onkeyup'=>'setFormatMoney($(this), \'td\'), set_total_bayar($(this))', 'min'=>0, 'max'=>$sisa_kasbon]) }}
							<span class="help-block text-right"></span>
						</td>
					</tr>
					@endforeach
					@endif
				</tbody>
			</table>
		
		<div class="panel-footer">
			<div class="col-sm-12 col-md-12 text-right">
				<div class="form-group">
					<p class="form-control-static" style="font-size: 25px" id="total_bayar">Rp 0</p>
				</div>
			</div>
			<div class="col-sm-12 col-md-12 text-right">
				<a href="{{ url('piutang_pegawai') }}" class="btn btn-md btn-default">Batal</a>
				<button type="submit" class="btn btn-md btn-success">Simpan</button>
			</div>
		</div>

	    </form>
	</div>
</div> <!-- end : .content -->

<!----Modal---->
<script type="text/javascript">
$(document).ready(function () {
	$(".set_selected").select2();

	$("input[name^='detail_kasbon[nominal_bayar]']").trigger("keyup");
});

function set_total_bayar(this_) {
	var temp_jml_bayar = 0;
	var input_jml_bayar = $("input[name^='detail_kasbon[nominal_bayar]']");
	var temp_item;
	$.each(input_jml_bayar, function (id, item) {
		temp_item = (item.value==null)||(item.value==NaN)||(item.value=="") ? 0 : item.value;
		temp_jml_bayar += parseFloat(temp_item);
	});

	$("#total_bayar").html(accounting.formatMoney(temp_jml_bayar));
}


function setFormatMoney(this_, p) {
	var div = this_.parents(p);
	var data = this_.val();
	if(data==""){
		data = 0;
	}
	div.find(".help-block").html(accounting.formatMoney(data));
}
</script>

@endsection