@extends('layout')


@section('content')

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="index.html"><i class="icon-home2 position-left"></i> Pembelian</a></li>
			<li class="active">Hutang Awal Pembelian</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Pembayaran</h5>
			<div class="heading-elements">
				
	       	</div>
	    </div>
	    <div class="panel-body">
	    	{{-- <table class="table table-striped">
	    		<tr>
	    			<td width="15%">No. Faktur</td>
	    			<td width="5%">:</td>
	    			<td width="30%" id="tb_nofaktur"></td>

	    			<td width="15%">Penyedia</td>
	    			<td width="5%">:</td>
	    			<td id="tb_penyedia"></td>
	    		</tr>
	    		<tr>
	    			<td width="15%">Jenis Transaksi</td>
	    			<td width="5%">:</td>
	    			<td id="tb_jenistran"></td>

	    			<td width="15%">Cara Bayar</td>
	    			<td width="5%">:</td>
	    			<td width="30%" id="tb_carabayar"></td>
	    		</tr>
	    		<tr>
	    			<td width="15%">Total</td>
	    			<td width="5%">:</td>
	    			<td id="tb_total"></td>
	    			
	    			<td width="15%">Kekurangan</td>
	    			<td width="5%">:</td>
	    			<td id="tb_sisa"></td>
	    		</tr>
	    	</table> --}}

	    	<form class="form-horizontal" autocomplete="off" id="form_tambah">
	    		<div class="col-sm-6">
	    			<div class="form-group">
		    			<label class="control-label col-sm-4">Tanggal Input</label>
		    			<div class="col-sm-8">
		    				<input type="text" name="tanggal_input" class="form-control datepicker" value="{{ date('d-m-Y') }}">
		    			</div>
		    		</div>

		    		{{-- <div class="form-group">
		    			<label class="control-label col-sm-4">Tanggal Faktur</label>
		    			<div class="col-sm-8">
		    				<input type="text" name="tanggal_faktur" class="form-control datepicker" value="" required>
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<label class="control-label col-sm-4">No. Faktur</label>
		    			<div class="col-sm-8">
		    				<input type="text" name="no_faktur" class="form-control" value="" required>
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<label class="control-label col-sm-4">Keterangan</label>
		    			<div class="col-sm-8">
		    				<textarea class="form-control" rows="3" name="keterangan">
		    					
		    				</textarea>
		    			</div>
		    		</div> --}}
	    		</div>
	    		<div class="col-sm-6">
	    			<div class="form-group">
		    			<label class="control-label col-sm-4">Faktur Pembelian</label>
		    			<div class="col-sm-8">
		    				<select class="form-control select-search" name="id_pembelian">
		    					
		    				</select>
		    			</div>
		    		</div>

		    		{{-- <div class="form-group">
		    			<label class="control-label col-sm-4">Jenis Transaksi</label>
		    			<div class="col-sm-8">
		    				<select class="form-control select-search" name="jenis_transaksi">
		    					
		    				</select>
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<label class="control-label col-sm-4">Penyedia</label>
		    			<div class="col-sm-8">
		    				<select class="form-control select-search" name="penyedia">
		    					
		    				</select>
		    			</div>
		    		</div> --}}

	    		</div>

	    		<table class="table table-striped">
		    		<tr>
		    			<td width="15%">No. Faktur</td>
		    			<td width="5%">:</td>
		    			<td width="30%" id="tb_nofaktur"></td>

		    			<td width="15%">Penyedia</td>
		    			<td width="5%">:</td>
		    			<td id="tb_penyedia"></td>
		    		</tr>
		    		<tr>
		    			<td width="15%">Jenis Transaksi</td>
		    			<td width="5%">:</td>
		    			<td id="tb_jenistran"></td>

		    			<td width="15%">Cara Bayar</td>
		    			<td width="5%">:</td>
		    			<td width="30%" id="tb_carabayar"></td>
		    		</tr>
		    		<tr>
		    			<td width="15%">Total</td>
		    			<td width="5%">:</td>
		    			<td id="tb_total"></td>
		    			
		    			<td width="15%">Kekurangan</td>
		    			<td width="5%">:</td>
		    			<td id="tb_sisa"></td>
		    		</tr>
		    		<input type="hidden" name="no_faktur">
		    		<input type="hidden" name="id_penyedia">
		    		<input type="hidden" name="">
	    		</table>
	    	</form>
	    	    	
	    	
	    	<div class="col-sm-12">
	    		<hr>
	    		<h5 class="form_simpan">Tambah Pembayaran</h5>
	    		<form class="form-horizontal form_simpan" autocomplete="off" id="form_simpan">
		    		<input type="hidden" name="id_pembelian" value="">
		    		<input type="hidden" name="id_hutangbeli" value="">
		    		
		    		<div class="form-group">
		    			<label class="control-label col-sm-2">Tanggal</label>
		    			<div class="col-sm-4">
		    				<input type="text" name="tanggal" class="form-control datepicker" value="{{ date('d-m-Y') }}">
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<label class="control-label col-sm-2">Cara Bayar</label>
		    			<div class="col-sm-4">
	        				<select class="form-control select-search" name="id_carabayar">
	        					@php foreach($data['cara_bayar'] as $s){ 
	        					@endphp
	        					<option value="@php echo $s->id_carabayar @endphp" >@php echo  $s->cara.' - '.$s->tipe; @endphp</option>
	        					@php } @endphp
	        				</select>
	        			
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<label class="control-label col-sm-2">Akun</label>
		    			<div class="col-sm-4">
		    				<select class="form-control select-search" name="id_akun">
	        					@php foreach($data['sumber_dana'] as $s){ 
	        					@endphp
	        					<option value="@php echo $s->id @endphp" tipe="{{ $s->saldo_normal }}">@php echo  $s->kode_perkiraan.' '.$s->nama_perkiraan; @endphp</option>
	        					@php } @endphp
	        				</select>
	        			
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<label class="control-label col-sm-2">Jumlah</label>
		    			<div class="col-sm-4">
		    				<input type="text" name="jumlah" class="form-control number-only text-right" onkeyup="maksimal($(this))">
		    				<p class="help-block pull-right" id="help_jumlah"></p>
		    			</div>
		    		</div>
		    		<div class="form-group">
		    			<div class="col-sm-6 ">
		    				<button type="button" class="btn btn-warning btn_batal" >Batal</button>
		    				<button type="button" class="btn btn-primary pull-right" id="btn_simpan">Simpan</button>
		    			</div>
		    		</div>	
		    		
		    	</form>
	    	</div>
	    	<hr>
			
			<div class="col-sm-12">
				<h5>Detail Pembayaran</h5>
			</div>
			<table class="table table_ table-stripped">
			    <thead>
					<tr>
						<th>No</th>
						<th>Tanggal</th>
						<th>Jenis Pembayaran</th>
						<th>Akun</th>
						<th>Jumlah</th>
						<th class="text-center"></th>
					</tr>
				</thead>
				<tbody>
					
				</tbody>
				<tfoot>
					<tr>
						<th colspan="4" class="text-right">Total</th>
						<th colspan="" id="td_totalbayar" class="text-right"></th>
						<th></th>
					</tr>
				</tfoot>
			</table>
	</div>

</div>

<input type="hidden" name="store_id" value="">
<input type="hidden" name="store_total" value="">
<input type="hidden" name="store_bayar" value="">
<input type="hidden" name="store_sisa" value="">
<!----Modal---->
<script type="text/javascript">
	

	var table_;
	var tb_no = parseInt(10000);
	var id_pembelian;


	$(document).ready(function(){
		table_ = $(".table_").DataTable({
			"paging": false
		});

		table_.on( 'order.dt search.dt', function () {
            table_.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();
		
		// id_pembelian = $("[name=store_id]").val();
		// if(id_pembelian != ''){
		// 	get_bayar(id_pembelian);
		// }

	})

	// function get_detail(id){
	// 	var sisa = parse_float(0);
	// 	$.ajax({
	// 		url: '{{ url('pembelian/hutangawal_beli/get_detailbayar') }}'+'/'+id,
	// 		type: 'get',
	// 		dataType: 'json',
	// 		success: function(respon){
	// 			clear();
	// 			if(respon.status == 1){
	// 				var ttl = (parse_float(respon.detail.nominal_netto));
	// 				var bayar_db = parse_float(0);

	// 				$("#tb_nofaktur").text(respon.detail.no_faktur);
	// 				$("#tb_carabayar").text(respon.detail.cara_bayar+' - '+respon.detail.tipe_bayar);
	// 				$("#tb_jenistran").text(respon.detail.nama_transaksi+' - '+respon.detail.tipe_transaksi);
	// 				$("#tb_penyedia").text(respon.detail.nama_penyedia+' - '+respon.detail.pic);
	// 				$("#tb_total").text(curr(ttl));
	// 				$("#tb_sisa").text('');
	// 				$("[name=store_total]").val(ttl);
	// 				bayar_db = parse_float(respon.detail.total_bayar);
	// 				// if(respon.detail.jenis_bayar==1){
	// 				// 	$('.btn_tambah').hide();
	// 				// }else{
	// 				// 	$('.btn_tambah').show();
	// 				// }
					

	// 				var sisa = parse_float($("[name=store_total]").val())-(parse_float($("[name=store_bayar]").val())+bayar_db);
					

	// 				$("[name=store_sisa]").val(sisa);
	// 				$("#tb_sisa").text(curr(sisa));


	// 				$("[name=jumlah]").attr('min', '0');
	// 				$("[name=jumlah]").attr('max', sisa);
	// 			}
	// 		}
	// 	})
	// }

	// function get_bayar(id){
	// 	var jum_bayar = parse_float(0);
	// 	$.ajax({
	// 		url: '{{ url('pembelian/hutangawal_beli/get_bayar') }}'+'/'+id,
	// 		type: 'get',
	// 		dataType: 'json',
	// 		success: function(respon){
	// 			table_.clear().draw();
	// 			if(respon.bayar.length > 0){
	// 				for(i in respon.bayar){
	// 					var button='<div><button type="button" class="btn bg-warning-400 btn-xs" onclick="edit_table('+respon.bayar[i].id_hutangbeli+')"><i class="icon-pencil"></i> </button> <button class="btn  btn-xs bg-danger-800" type="button" onclick="hapus('+respon.bayar[i].id_hutangbeli+')"><i class="icon-eraser"></i></button></div>';
	// 					if(respon.bayar[i].id_hutangbeli=='nominal_bayar'){
	// 						button='';
	// 					}

	// 					table_.row.add(['<div></div>',
	// 						'<div id="list_kode'+respon.bayar[i].id_hutangbeli+'">'+respon.bayar[i].tgl_bayar+'</div>',
	// 						'<div id="list_kode'+respon.bayar[i].id_hutangbeli+'">'+(respon.bayar[i].jenis)+'</div>',
	// 						'<div id="list_kode'+respon.bayar[i].id_hutangbeli+'">'+respon.bayar[i].kode_perkiraan+'</div>',
	// 						'<div id="list_kode'+respon.bayar[i].id_hutangbeli+'" class="text-right">'+curr(respon.bayar[i].jum_bayar)+'</div>',
	// 						button+
	// 						'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon.bayar[i].id_hutangbeli+'" value="'+respon.bayar[i].id_hutangbeli+'">'+
	// 						'<input type="hidden" name="tabel_jum[]" id="tabel_jum'+respon.bayar[i].id_hutangbeli+'" value="'+respon.bayar[i].jum_bayar+'">'+
	// 						'<input type="hidden" name="tabel_idakun[]" id="tabel_idakun'+respon.bayar[i].id_hutangbeli+'" value="'+respon.bayar[i].id_akun+'">'+
	// 						'<input type="hidden" name="tabel_idcarabayar[]" id="tabel_idcarabayar'+respon.bayar[i].id_hutangbeli+'" value="'+respon.bayar[i].id_carabayar+'">'+
	// 						'<input type="hidden" name="tabel_tanggal[]" id="tabel_tanggal'+respon.bayar[i].id_hutangbeli+'" value="'+respon.bayar[i].tgl_bayar+'">'
	// 						]).draw(false);
	// 					jum_bayar += parse_float(respon.bayar[i].jum_bayar);
	// 				}

	// 				$("#td_totalbayar").text(curr(jum_bayar));
	// 				$("[name=store_bayar]").val(jum_bayar);
	// 			}else{
	// 				$("#td_totalbayar").text(curr(jum_bayar));
	// 				$("[name=store_bayar]").val(jum_bayar);					
	// 			}
	// 			get_detail(id);
	// 		}
	// 	})
	// }

	// function clear(){
	// 	$("#tb_nofaktur").text('');
	// 	$("#tb_carabayar").text('');
	// 	$("#tb_jenistran").text('');
	// 	$("#tb_penyedia").text('');
	// 	$("#tb_total").text('');
	// 	$("#tb_sisa").text('');
	// }

	// function clear2(){
	// 	$("h5.form_simpan").html('Tambah Pembayaran');
	// 	$("[name=id_hutangbeli]").val('');
	// 	$("[name=tanggal]").val('{{date('d-m-Y')}}');
	// 	$("[name=jumlah]").val('');
	// 	$("[name=id_akun]").val(null).change();
	// 	$("#help_jumlah").text('');
	// }

	// $("[name=jumlah]").keyup(function(){
	// 	var jum = parse_float($("[name=jumlah]").val());
	// 	if(isNaN(jum) || jum == 0 || jum == ''){
	// 		jum = parse_float(0);
	// 	}
	// 	$("#help_jumlah").text(curr(jum));
	// })

	// function set_jumlah(){
	// 	var jum = parse_float($("[name=jumlah]").val());
	// 	if(isNaN(jum) || jum == 0 || jum == ''){
	// 		jum = parse_float(0);
	// 	}
	// 	$("#help_jumlah").text(curr(jum));
	// }

	// $(".btn_batal").click(function(){
	// 	clear2();
	// })

	// $("#btn_simpan").click(function(){
	// 	var id_hutangbeli = $("[name=id_hutangbeli]").val();
	// 	var id_beli = id_pembelian;
	// 	var tanggal = $("[name=tanggal]").val();
	// 	var id_akun = $("[name=id_akun]").val();
	// 	var id_carabayar = $("[name=id_carabayar]").val();
	// 	var jum_bayar = $("[name=jumlah]").val();
	// 	if(tanggal != '' && jum_bayar != '' && jum_bayar > 0){
	// 		$.ajax({
	// 			url: '{{ url('pembelian/hutangawal_beli/simpan') }}',
	// 			type: 'post',
	// 			dataType: 'json',
	// 			data: {id_hutangbeli : id_hutangbeli, id_beli : id_pembelian, id_akun : id_akun,id_carabayar : id_carabayar, tanggal : tanggal, jum_bayar : jum_bayar},
	// 			headers : {
	// 				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	// 			},
	// 			success: function(respon){
	// 				clear2();
	// 				get_bayar(id_pembelian);
	// 			}
	// 		})
	// 	}
	// })

	// function edit_table(id){
		
	// 	$("h5.form_simpan").html('Update Pembayaran');

	// 	var id_hutangbeli = $("#tabel_id"+id).val();
	// 	var tanggal = $("#tabel_tanggal"+id).val();
	// 	var id_akun = $("#tabel_idakun"+id).val();
	// 	var id_carabayar = $("#tabel_idcarabayar"+id).val();
	// 	var jum = parse_float($("#tabel_jum"+id).val());
	// 	var sisa = parse_float($("[name=store_sisa]").val());

	// 	$("[name=id_hutangbeli]").val(id_hutangbeli);
		
	// 	$("[name=tanggal]").val(tanggal);
	// 	$("[name=jumlah]").val(jum);
	// 	$("[name=id_akun]").val(id_akun).change();
	// 	$("[name=id_carabayar]").val(id_carabayar).change();
	// 	set_jumlah();

	// 	$("[name=jumlah]").attr('min', '0');
	// 	$("[name=jumlah]").attr('max', jum+sisa);
	// 	$( ".form_simpan" ).focus();
	// }

	// function hapus(id){
	// 	swal({
	// 		title: 'Anda yakin akan menghapus item ini?',
	// 		type: 'warning',
	// 		showCancelButton: true,
	// 		confirmButtonColor: '#3085d6',
	// 		cancelButtonColor: '#d33',
	// 		confirmButtonText: 'Ya',
	// 		cancelButtonText: 'Tidak',
	// 	}).then((result) => {
	// 	 	if(result.value){
	// 	 		$.ajax({
	// 				url: "{{ url('pembelian/hutangawal_beli/hapus')}} ",
	// 				type: 'post',
	// 				data: {id : id},
	// 				headers : {
	// 					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	// 				},
	// 				success: function(respon){
	// 					get_bayar(id_pembelian);
	// 				}
	// 			})
	// 	 	}
	// 	})
	// }
</script>

@endsection