@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">
			@if($sbg == "piutang_awal")
			<li><a href="{{ url('piutangawal') }}"><i class="icon-home2 position-left"></i> Piutang</a></li>
			@else
			<li><a href="{{ url('piutang') }}"><i class="icon-home2 position-left"></i> Piutang</a></li>
			@endif
			<li class="active">Daftar Bayar Tagihan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Daftar Pembayaran Tagihan</h5>
			<div class="heading-elements">
				<ul class="icons-list">
					@if($sbg == "piutang_awal")
					<li><a href="{{ url('piutangawal') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
					@else
					<li><a href="{{ url('piutang') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
					@endif
		      	</ul>
	       	</div>
	    </div>

	    <div class="panel-body">
	    	<form class="form-horizontal" method="get" action="#">
	    	
	    	<div class="col-sm-12 col-md-12">
	    		<div class="row col-sm-6 col-md-6">
		    		<div class="form-group">
		    			{{ Form::label("Label CV", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<div class="checkbox">
		    					<label>
			    					{{ Form::checkbox("cv", '1', ($penjualan->cv==1) ? "true":"false", ['class'=>'styled', 'disabled'=>'disabled']) }}
		    					</label>
		    				</div>
		    			</div>
		    		</div>
		    	</div>
	    	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Penyedia</label>
        			<div class="col-sm-8">
        				{{ Form::text("penyedia", $penjualan->nama_pelanggan, ['class'=>'form-control', 'readonly'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Input</label>
        			<div class="col-sm-8">
        				{{ Form::text("tanggal", date_db($penjualan->tanggal, true), ['class'=>'form-control', 'readonly'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Penjualan</label>
        			<div class="col-sm-8">
        				{{ Form::text("tanggal_faktur", date_db($penjualan->tanggal_faktur, true), ['class'=>'form-control', 'readonly'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Jatuh Tempo</label>
        			<div class="col-sm-8">
        				{{ Form::text("tanggal_tempo", date_db($penjualan->tanggal_tempo, true), ['class'=>'form-control', 'readonly'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No. Faktur</label>
        			<div class="col-sm-8 col-md-8">
        				{{ Form::text("nomor_faktur", $penjualan->no_faktur."".$penjualan->sufix_no_faktur, ['class'=>'form-control', 'readonly'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Keterangan</label>
        			<div class="col-sm-8">
        				{{ Form::textarea("keterangan", $penjualan->keterangan, ['class'=>'form-control', 'rows'=>5, 'readonly'=>'']) }}
        			</div>
        		</div>
        	</div>

        	@php
        		$pembayaran = $penjualan->total_bayar;
        		$tagihan = $penjualan->nominal_netto;
        		if(count($jur_peny_piutang) > 0) {
	        		foreach ($jur_peny_piutang as $key => $value) {
	        		    if ($value->tipe_akun == "D") {
	        		        $tagihan -= $value->nominal;
	        		    } else {
	        		        $tagihan += $value->nominal;
	        		    }
	        		}
	        	}
        		$retur_total = ($retur != null) ? $retur->nominal_total : 0;
        		$retur_tagihan_total = ($retur_tagihan_penjualan != null) ? $retur_tagihan_penjualan->nominal_total : 0;
        		$retur_tagihan_piutangawal_total = ($retur_tagihan_piutangawal != null) ? $retur_tagihan_piutangawal->nominal_total : 0;
        		$mata_uang_retur_total = ($retur != null) ? $retur->mata_uang : 1;
        		$mata_uang_retur_tagihan = ($retur_tagihan_penjualan != null) ? $retur_tagihan_penjualan->mata_uang : 1;
        		$mata_uang_retur_tagihan_piutangawal = ($retur_tagihan_piutangawal != null) ? $retur_tagihan_piutangawal->mata_uang : 1;
        		$sisa_tagihan = (($tagihan-$pembayaran)-$retur_total)+$retur_tagihan_total+$retur_tagihan_piutangawal_total;
        	@endphp

        	<div class="col-sm-6">
        		<div class="alert alert-info">
        			<table class="table">
        				<tbody>
        					<tr style="font-size: 20px;">
        						<th width="25%">Pembayaran</th>
        						<th class="text-right">{{ ($penjualan->mata_uang == "1") ? "Rp".rupiah2($pembayaran, 2) : "$".rupiah2($pembayaran, 2) }}</th>
        					</tr>
        					<tr>
        						<td>Tagihan</td>
        						<td class="text-right">{{ ($penjualan->mata_uang == "1") ? "Rp".rupiah2($tagihan, 2) : "$".rupiah2($tagihan, 2) }}</td>
        					</tr>
        					<tr>
        						<td>Retur Penjualan</td>
        						<td class="text-right">{{ ($mata_uang_retur_total == "1") ? "Rp".rupiah2($retur_total, 2) : "$".rupiah2($retur_total, 2) }}</td>
        					</tr>
        					@if($sbg == "piutang_penjualan")
        					<tr>
        						<td>Retur Tagihan</td>
        						<td class="text-right">{{ ($mata_uang_retur_tagihan == "1") ? "Rp".rupiah2($retur_tagihan_total, 2) : "$".rupiah2($retur_tagihan_total, 2) }}</td>
        					</tr>
        					@else
        					<tr>
        						<td>Retur Tagihan</td>
        						<td class="text-right">{{ ($mata_uang_retur_tagihan_piutangawal == "1") ? "Rp".rupiah2($retur_tagihan_piutangawal_total, 2) : "$".rupiah2($retur_tagihan_piutangawal_total, 2) }}</td>
        					</tr>
        					@endif
        					<tr style="font-size: 15px;">
        						<th class="text-left">Sisa Tagihan</th>
        						<th class="text-right">{{ ($penjualan->mata_uang == "1") ? "Rp".rupiah2($sisa_tagihan, 2) : "$".rupiah2($sisa_tagihan, 2) }}</th>
        					</tr>
        				</tbody>
        			</table>
        		</div>
        	</div>

	    	</form>

	    </div>

			<table class="table" id="tabel-daftar-bayar">
				<thead>
					<tr>
						<th>Tanggal</th>
						<th>No. Transaksi</th>
						<th>Akun Bayar</th>
						<th>Total Bayar</th>
						<th></th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		
		<div class="panel-footer">

		</div>

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

<!---Modal--->
<div class="modal fade modal-cetak" id="cetak-buktibayar"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm" role="document">
	    <div class="modal-content">
	    	<div class="modal-header with-border">
	    		<h5 class="modal-title" id="exampleModalLabel">Cetak</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      		<form class="form-horizontal" autocomplete="off" method="get" action="{{ url('penjualan/cetak_bukti_bayar') }}" id="form-cetak-buktibayar" target="_blank">
	      	<div class="modal-body">
	      			<div class="form-group">
	      				{{ Form::label("No. Transaksi", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-no-transaksi"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Tanggal", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-tanggal-transaksi"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::hidden("id", 0) }}
		    			{{ Form::label("Label CV", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<div class="checkbox">
		    					<label>
			    					{{ Form::checkbox("label_cv", 'true', true, ['class'=>'styled']) }}
		    					</label>
		    				</div>
		    			</div>
	      			</div>
	      			<div class="form-group" id="field_currency">
	      				<label class="control-label col-sm-4">Mata Uang</label>
	      				<div class="col-sm-8 col-md-8">
	      					<select class="form-control" name="currency" id="currency">
	      						@foreach($mata_uang as $key => $val)
	      							<option value="{{ $val->id_matauang }}" data-inisial="{{ $val->inisial }}" @if($penjualan->mata_uang == $val->id_matauang) selected @endif>{{ $val->nama_matauang }} - {{ $val->inisial }}</option>
	      						@endforeach
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Ukuran Kertas", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					{{ Form::select("uk_kertas", ['2'=>'Sedang','1'=>'Kecil'], 2, ['class'=>'form-control']) }}
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Set Tanggal Cetak", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					{{ Form::text("tanggal_cetak", "", ['class'=>'form-control datepicker']) }}
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::hidden("as", "") }}
	      			</div>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="reset" class="btn btn-default" data-dismiss="modal" id="btn-batal-cetak-buktibayar">Batal</button>
	        	<button type="submit" class="btn btn-primary" >Cetak</button>
	      	</div>
	      		</form>
	    </div>
	</div>
</div>

<script type="text/javascript">
	var tabelDaftarBayar;
	var id_ = "{{ $penjualan->id_penjualan }}";
	var mata_uang = "{{ $penjualan->mata_uang }}";

$(document).ready(function () {
	loadDaftarBayar();
	// console.log(mata_uang);
	if (mata_uang == 1) {
		$("#field_currency").hide();
	} else {
		$("#field_currency").show();
	}
});

function loadDaftarBayar() {
	tabelDaftarBayar = $("#tabel-daftar-bayar").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('penjualan/tabel_daftar_bayar/'.$penjualan->sebagai) }}/'+id_,
	        columns: [
	            {data: 'tgl_bayar', name: 'tgl_bayar'},
	            {data: 'no_transaksi_bayar', name: 'no_transaksi_bayar', render:function(row, type, data, meta){
	            	return data.data_transaksi_bayar.no_transaksi_bayar;
	            }},
	            {data: 'id_akun', name: 'id_akun', render:function(row, type, data, meta){
	            	return data.akun_bayar;
	            }},
	            {data: 'jum_bayar', name: 'jum_bayar'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
	        createdRow: function(row, data, index) {
	        	var id_bayar = data.id_jualbayar || data.id;
	        	var id_penjualan = data.id_penjualan || 0;
	        	var type_currency = typeof(data.data_penjualan);
	        	$( row ).attr("data-id", id_bayar);
	        	$( row ).attr("data-idpenjualan", id_penjualan);
	        	$( row ).attr("data-sebagai", '{{ $penjualan->sebagai }}');
	        	if (type_currency != 'undefined') {
	        		if (data.data_penjualan.mata_uang == "1") {
		        		accounting.settings = {
							currency: {
								symbol : "Rp",   // default currency symbol is '$'
								format: { pos : "%s%v", neg : "( %s%v )", zero : "%s%v"}, // controls output: %s = symbol, %v = value/number (can be object: see below)
								decimal : ",",  // decimal point separator
								thousand: ".",  // thousands separator
								precision : 2   // decimal places
							},
							number: {
								precision : 2,  // default precision on numbers is 0
								thousand: ".",
								decimal : ","
							}
						}
		        		$("td", row).eq(3).addClass("text-right").text(accounting.formatMoney(data.jum_bayar));
		        	} else {
		        		accounting.settings = {
							currency: {
								symbol : "$",   // default currency symbol is '$'
								format: { pos : "%s%v", neg : "( %s%v )", zero : "%s%v"}, // controls output: %s = symbol, %v = value/number (can be object: see below)
								decimal : ",",  // decimal point separator
								thousand: ".",  // thousands separator
								precision : 2   // decimal places
							},
							number: {
								precision : 2,  // default precision on numbers is 0
								thousand: ".",
								decimal : ","
							}
						}
		        		// $("td", row).eq(3).addClass("text-right").text(accounting.formatMoney(data.jum_bayar*data.data_penjualan.kurs_jual));
		        		$("td", row).eq(3).addClass("text-right").text(accounting.formatMoney(data.jum_bayar));
		        	}
	        	} else {
	        		accounting.settings = {
							currency: {
								symbol : "Rp",   // default currency symbol is '$'
								format: { pos : "%s%v", neg : "( %s%v )", zero : "%s%v"}, // controls output: %s = symbol, %v = value/number (can be object: see below)
								decimal : ",",  // decimal point separator
								thousand: ".",  // thousands separator
								precision : 2   // decimal places
							},
							number: {
								precision : 2,  // default precision on numbers is 0
								thousand: ".",
								decimal : ","
							}
						}
		        		$("td", row).eq(3).addClass("text-right").text(accounting.formatMoney(data.jum_bayar));
	        	}
            },
		});
}

function btnEditDetailBayar(this_) {
	var id = this_.parents("tr").data("id");
	var idpenjualan = this_.parents("tr").data("idpenjualan");
	var sebagai = this_.parents("tr").data("sebagai");
	alert(id+"/"+sebagai+"/"+idpenjualan);
}

function btnHapusDetailBayar(this_) {
	var id = this_.parents("tr").data("id");
	var sebagai = this_.parents("tr").data("sebagai");
	this_.attr("disabled", "");
	swal({
			title: 'Anda yakin akan menghapus data ini?',
			text: 'Menghapus data ini akan mempengaruhi data transaksi keuangan Anda.',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#3085d6',
			cancelButtonColor: '#d33',
			confirmButtonText: 'Ya',
			cancelButtonText: 'Tidak',
		}).then((result) => {
		 	if(result.value){
		 		$.ajax({
					url: "{{ url('penjualan/delete_detail_bayar') }}/"+id,
					type: 'post',
					data: { "_method":"delete", "sebagai":sebagai},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						location.reload();
					}
				})
		 	}else{
				this_.removeAttr("disabled");
		 	}
		})
}
function btnCetakBuktiBayar(this_, currency = "1") {
	var id = this_.parents('tr').data('id');
	var sebagai = this_.parents('tr').data('sebagai');
	// var href = this_.data("href");
	// $("#form-cetak-buktibayar").attr("action", href);
	$("#form-cetak-buktibayar").find("input[name=tanggal_cetak]").val('{{date("d-m-Y")}}');

	var row_data = tabelDaftarBayar.row(this_.parents("tr")).data();
	console.log(row_data);
	var set_tgl_bayar = moment(row_data.tgl_bayar, "YYYY-MM-DD").format("DD-MM-YYYY");
	$("#form-cetak-buktibayar input[name=id]").val(id);
	$("#form-cetak-buktibayar input[name=as]").val(sebagai);
	$("#form-cetak-buktibayar input[name=tanggal_cetak]").val(set_tgl_bayar).datepicker("setDate", set_tgl_bayar);
	$(".text-no-transaksi").html(row_data.data_transaksi_bayar.no_transaksi_bayar);
	$(".text-tanggal-transaksi").html(set_tgl_bayar);

	showModal($("#cetak-buktibayar"));
}
</script>

@endsection