@extends('layout')


@section('content')

<style type="text/css">
	 .table-form>tbody>tr>th, .table-form>tfoot>tr>th, .table-form>thead>tr>td, .table-form>tbody>tr>td, .table-form>tfoot>tr>td{
		    padding: 5px;
		   -- vertical-align: middle;
		}
		table.table-form tbody>tr.selected, table.table-form tbody>tr.selected td, table.table-form tbody>tr>.selected{
		    color: white !important;
		    background-color: #49b6d6 !important;
		}

	 table, th, td, input, textarea, select {
            color:  black !important; 
        }
        .help-block {
 
    margin-top: 0px!important;
    margin-bottom: 0px!important;
}
</style>

<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> Penjualan</a></li>
			<li class="active">Piutang Awal</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Input Piutang Awal</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('piutangawal') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<form class="form-horizontal" method="post" action="{{ url('piutangawal/simpan') }}" autocomplete="off" >
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id" value="{{ $data['data']['id']}}">
        	<div class="col-sm-12 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', $data['data']['cv'], ['class'=>'styled']) }}
	    					</label>
	    				</div>
	    			</div>
	    		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Pelanggan</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="pelanggan">
        					@php foreach($data['pelanggan'] as $s){ 
        					$sel = $data['data']['id_pelanggan'];
        					@endphp

        					<option value="@php echo $s->id_pelanggan @endphp" {{ ($s->id_pelanggan == $sel) ? "selected":"" }}>@php echo  $s->pic.' - '.$s->nama @endphp</option>

        					@php } @endphp
        				</select>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Input</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):$data['data']['tanggal'] }}" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Faktur</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal_faktur" placeholder="Tanggal Faktur" value="{{ $data['data']['tanggal_faktur'] }}" onchange="get_nomor($(this)), set_jatuh_tempo($(this))" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Jatuh Tempo</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal_tempo" placeholder="Tanggal Jatuh Tempo" value="{{ $data['data']['tanggal_tempo'] }}" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No. Faktur</label>
        			<div class="col-sm-3 col-md-3">
        				{{ Form::text('no_faktur', $data['data']['no_faktur'], ['class'=>'form-control text-right', 'placeholder'=>'Nomor']) }}
        			</div>
        			<div class="col-sm-5 col-md-5">
        				{{ Form::text("sufix_no_faktur", $data['data']['sufix_no_faktur'], ['class'=>'form-control text-uppercase']) }}
        			</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">{{ $data['data']['keterangan'] }}</textarea>
        			</div>
        		</div>
        	</div>

        	<div class="col-sm-12 col-md-6">
        		<div class="form-group">
        			&nbsp;
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4 col-md-4">Jumlah</label>
        			<div class="col-sm-8 col-md-8">
        				<input class="form-control text-right" type="text" name="td_nominal" value="{{ $data['data']['nominal_piutang'] }}" onkeyup="setFormatNumber($(this), 'div'), get_jum()" min="0" required="" />
                    	<p class="help-block pull-right">Rp 0</p>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4 col-md-4">PPN</label>
        			<div class="col-sm-8 col-md-8">
                    	<select class="form-control select-search" name="pajak">
        					<option value="0" >Tidak</option>
        					<option value="1" >Iya</option>
        				</select>
                    	<input type="hidden" name="td_pajak" class="form-control number-only text-right" value="0">
                    	<p class="help-block pull-right" id="help_td_pajak">Rp 0</p>
        			</div>
        		</div>
        		<div class="form-group" style="font-weight: bold !important;">
        			<label class="control-label col-sm-4 col-md-4">Total</label>
        			<div class="col-sm-8 col-md-8">
                    	<input type="hidden" name="td_total" class="form-control text-right" value="0">
        				<p class="form-control-static text-right" id="help_td_total">Rp0</p>
        			</div>
        		</div>
        	</div>

	    </div>

	    <div class="panel-footer">
	    	<div class="col-sm-12 col-md-12">
	            <div class="form-group pull-right">
	             	<button class="btn btn-primary" type="submit">Simpan</button>
	            </div>

	    	</div>

	    </div>


		<input type="hidden" name="td_total" class="form-control">
		<input type="hidden" name="td_netto" class="form-control">
	    </form>
	</div>
</div>

<!----Modal---->
<script type="text/javascript">

	var table_barang;
	var id = '';
	var tb_no = parseInt(10000);
	$(document).ready(function(){

		id = '{{ $data['data']['id']}}';
		if(id != ''){
			// get_edit(id);
		}

		// $("input[name=td_nominal_bayar]").val("{{--$data['data']['nominal_bayar']--}}").trigger("keyup");
		// $("input[name=td_ongkir]").val("{{--$data['data']['nominal_ongkir']--}}").trigger("keyup");
		$("select[name=pajak]").change();
		$(document).on("change","select[name=pajak]",function(e){
            if($(this).val()==1){
        		$("input[name=td_pajak]").val(0);    	
        		$("input[name=td_pajak]").removeAttr('readonly');
            }else{
            	$("input[name=td_pajak]").val(0);    	
            	$("input[name=td_pajak]").attr('readonly','readonly');
            }
            get_jum();
            netto();
        });

        $("select[name=pajak]").val("{{$data['data']['pajak']}}").change();
        $("input[name=tanggal_faktur]").trigger("change");
        $("input[name=td_nominal]").trigger("keyup");
	})

	function get_nomor(this_){
		var id = $("input[name=id]").val();
		var date = this_.val();
		var s_date = date.split("/");
		var sufix_no_faktur = "";
		$.ajax({
			url : "{{ url('piutangawal/get_nomor') }}",
			type : "get",
			data : {id:id, tanggal:date},
			dataType : "json",
			success : function(result){
				if(id!=""){
					sufix_no_faktur = result.sufix_no_faktur;
				}
				
				$("input[name=no_faktur]").val(result.no_faktur);
				$("input[name=sufix_no_faktur]").val(sufix_no_faktur);
			}
		});
	}

	function set_jatuh_tempo(this_) {
		var id = $("input[name=id]").val();
		var date = this_.val();
		var s_date = date.split("/");
		var _date = new Date(s_date[2], s_date[1], s_date[0]);
		_date.setMonth(_date.getMonth()+1);

		var dd = _date.getDate();
		var mm = _date.getMonth()+1;
		var yyyy = _date.getFullYear();
		if(mm < 10) mm = '0'+mm;
		if(dd < 10) dd = '0'+dd;

		var set = [dd, mm, yyyy];
		$("input[name=tanggal_tempo]").val(set.join("/"));
	}	

	function get_jum(){
		var jum = $("input[name=td_nominal]").val();
		
		$("#help_td_pajak").text(accounting.formatMoney($("[name=td_pajak]").val()));
		$("#help_td_total").text(accounting.formatMoney(jum));
		$("[name=td_total]").val(jum);
		netto();
	}

	function setFormatNumber(this_, p) {
		var parentz = this_.parent(p);
		var data = this_.val();
		parentz.find(".help-block").text(accounting.formatMoney(data));
	}

	function netto(){
		// var total = parseFloat($("[name=td_total]").val());
		var jumlah = parseFloat($("[name=td_nominal]").val());
		var potongan = 0;
		var ppn = parseFloat($("[name=td_pajak]").val());

		var netto = parseFloat(0);
		var ppn = parseFloat(0);

		if($('select[name=pajak]').val()==1){
			ppn = parseFloat((jumlah-potongan)*(10/100));
		}
			
		netto = jumlah-potongan+ppn; //+bunga
		$("[name=td_pajak]").val(ppn);
		$("#help_td_pajak").text(accounting.formatMoney(ppn));

		$("[name=td_total]").val(netto);
		$("#help_td_total").text(accounting.formatMoney(netto));
	}


</script>

@endsection