@extends('layout')


@section('content')

<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 class="active">Retur Tagihan Piutang</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="row">
		<div class="col-lg-12">
			@if(Session::has('flash_message_error'))
			<div class="alert alert-error alert-block">
				<button type="button" class="close" data-dismiss="alert">x</button>
				<strong>{!! session('flash_message_error') !!}</strong>
			</div>
			@endif
			@if(Session::has('flash_message_success'))
			<div class="alert alert-success alert-block">
				<button type="button" class="close" data-dismiss="alert">x</button>
				<strong>{!! session('flash_message_success') !!}</strong>
			</div>
			@endif
		</div>
	</div>
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Retur Tagihan Piutang</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('piutang') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>
	    
	    <div class="panel-body">
	    	<form class="form-horizontal" id="form_retur" method="POST" action="{{ route('rtp.createPiutang') }}" autocomplete="off" >
	    		{{ csrf_field() }}

	    	<input type="hidden" name="id_penjualan" value="{{ $data->id_penjualan }}">
	    	<input type="hidden" name="id_pelanggan" value="{{ $data->id_pelanggan }}">
	    	<input type="hidden" name="cv" value="{{ $data->cv }}">

        	<div class="col-sm-6">
        		<div class="form-group">
	    			{{ Form::label("No. Faktur", "", ['class'=>'control-label col-sm-4']) }}
	    			<div class="col-sm-8">
	    				<label class="form-control">{{ $data->no_faktur.$data->sufix_no_faktur }} ({{ $data->nama_pelanggan }})</label>
	    			</div>
	    		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Lokasi</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="lokasi">
        					@foreach($lokasi as $key=>$value)
        					<option value="{{ $value->id_lokasi }}">{{ $value->nama_lokasi}}</option>
        					@endforeach
        				</select>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Retur</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal_retur" placeholder="Tanggal Retur" data-mask="99/99/9999" value="{{ date('d/m/Y') }}" onchange="get_nomor($(this))" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No. Retur</label>
        			<div class="col-sm-3 col-md-3">
        				{{ Form::text('no_retur', 0, ['class'=>'form-control text-right', 'placeholder'=>'Nomor']) }}
        			</div>
        			<div class="col-sm-5 col-md-5">
        				{{ Form::text("sufix_no_retur", 0, ['class'=>'form-control text-uppercase']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Mata Uang</label>
        			<div class="col-sm-6 col-md-6">
        				<select class="form-control select-search" name="currency" id="currency">
        					@foreach($mata_uang as $key => $value)
        						<option value="{{ $value->id_matauang }}" data-inisial="{{ $value->inisial }}">{{ $value->nama_matauang }} - {{ $value->inisial }}</option>
        					@endforeach
        				</select>
        			</div>
        		</div>
        		<div class="form-group" id="field_kurs">
        			<label class="control-label col-sm-4">Kurs (IDR/USD)</label>
        			<div class="col-sm-8">
        				{{ Form::label("Kurs IDR/USD", "", ["class"=>"control-label"]) }}
	    				{{ Form::number("kurs", 1, ["class"=>"form-control","min"=>"0","id"=>"kurs","step"=>0.01]) }}
	    				<span class="help-block text-right" style="font-weight: bold;" id="nominal_kurs">Rp0</span>
        			</div>
        		</div>
        		<div class="form-group">
	    			{{ Form::label("Akun Bayar", "", ['class'=>'control-label col-sm-4']) }}
	    			<div class="col-sm-8">
	    				{{ Form::select("id_akun", $akun, [], ['class'=>'form-control select-search']) }}
	    			</div>
	    		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Nominal</label>
        			<div class="col-sm-8">
        				<input type="number" step="0.01" min="0" name="nominal" id="nominal" class="form-control text-right" onkeyup="setFormatMoney($(this), 'div')" value="{{ $sisa }}">
						<span class="help-block currency text-right" style="font-weight: bold;">Rp{{ rupiah2($sisa, 2) }}</span>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Keterangan</label>
        			<div class="col-sm-8">
        				<textarea class="form-control" name="keterangan" rows="2"></textarea>
        			</div>
        		</div>
        	</div>
	    </div>

	    <div class="panel-footer">
	    	<div class="col-sm-12 col-md-12">
	            <div class="form-group pull-right">
	            	<a href="{{ route('rtp.index') }}" class="btn btn-default">Batal</a>
	             	<button class="hidden" type="submit" id="btn-submit">Simpan</button>
	             	<button class="btn btn-success" type="button" id="btn_simpan">Simpan</button>
	            </div>

	    	</div>

	    </div>

	    </form>
	</div>
</div>


<script type="text/javascript">
var stok_barang_selected = 0;
var tabel;

$(document).ready(function(){
	// init element
	$(".set_select2").select2();

	$("input[name='tanggal_retur']").trigger("change");

	$("#id_penjualan").on("change", function(evt){
		evt.preventDefault();
		var sisa = $(this).val().split('_')[3];
		$("#nominal").val(sisa);
		$("#nominal").trigger('keyup');
	})

	$("#btn_simpan").on("click", function(evt){
		if ($("#id_penjualan").val() == "") {
			$("#id_penjualan").select2('open');
			alert('Pilih no faktur yang kelebihan pembayaran!');
		} else if ($("#currency").val() != "1") {
			if ($("#kurs").val() == "" || $("#kurs").val() == "0" || $("#kurs").val() == 0 || $("#kurs").val() == "1" || $("#kurs").val() == 1) {
				$("#kurs").focus();
				alert('Masukkan jumlah kurs (IDR/USD) saat ini');
			} else {
				$("#form_retur_penjualan#btn-submit").trigger("click");
				$("#btn_simpan").attr('disabled', true);
			}
		} else if ($("#nominal").val() == "" || $("#nominal").val() == 0) {
			$("#nominal").focus();
			alert('Nominal pengembalian harus disi!');
		} else {
			$("#btn-submit").trigger("click");
			$("#btn_simpan").attr('disabled', true);
		}
	})

    setCurrency($("#currency").val());
	checkKurs();
	if ($("#currency").val() == "1") {
		$("#field_kurs").hide('slow');
		$("#kurs").val(1);
	} else {
		$("#field_kurs").show('slow');
		$("#kurs").val(1);
	}

});

$("#currency").on("change", function(evt){
	evt.preventDefault();
	if ($(this).val() == 1) {
		$("#field_kurs").hide('slow');
		$("#kurs").val(1);
		$("#kurs").trigger('keyup');
	} else {
		$("#field_kurs").show('slow');
		$("#kurs").val(1);
		$("#kurs").trigger('keyup');
	}
	setCurrency($(this).val());
})

$("#kurs").on("keyup", function(evt){
	evt.preventDefault();
	setKurs($(this));
})

function checkKurs(){
	if ($("#kurs").val() != "") {
		setKurs($("#kurs"));
		$("#nominal").trigger("keyup");
	}
}

function setKurs(this_){
	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 : ","
		}
	}

	var data = this_.val();
	if(data==""){
		data = 0;
	}

	$("#nominal_kurs").html(accounting.formatMoney(data));
}

function setCurrency(curr){
	accounting.settings = {
		currency: {
			symbol : (curr == "1") ? "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 : ","
		}
	}

	$(".currency").html(accounting.formatMoney());
}

function get_nomor(this_){
	var date = this_.val();
	$.ajax({
		url : "{{ route('rtp.getNomor') }}",
		type : "get",
		data : {tanggal:date},
		dataType : "json",
		success : function(result){			
			$("input[name=no_retur]").val(result.no_retur);
			$("input[name=sufix_no_retur]").val(result.sufix_no_faktur);
		}
	});
}

function setFormatMoney(this_, p) {
	var div = this_.parents(p);
	var data = this_.val();
	if(data==""){
		data = 0;
	}

	accounting.settings = {
		currency: {
			symbol : ($("#currency").val() == "1") ? "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 : ","
		}
	}

	div.find(".currency").html(accounting.formatMoney(data));
}

</script>

@endsection