@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>Sales Contract</li>
			<li class="active">Tagihan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Tagihan</h5>

			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('sales_contract_tagihan') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>
	    
	    <form class="form-horizontal" role="form" method="POST" action="#" id="form-sales-contract-tagihan">

	    <div class="panel-body">

	    	<div class="col-sm-12 col-md-12">
	    		<div class="row col-sm-6 col-md-6">
		    		<div class="form-group">
		    			{{ Form::label("Lokasi", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<select name="lokasi" class="form-control select-search" required="" disabled="">
		    					@foreach($lokasi AS $key => $value)
		    					<option value="{{ $value->id_lokasi }}">{{ $value->nama_lokasi }}</option>
		    					@endforeach
		    				</select>
		    			</div>
		    		</div>
		    	</div>
	    	</div>

	    	<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', true, ['class'=>'styled', 'disabled'=>'']) }}
		    					</label>
		    				</div>
		    			</div>
		    		</div>
		    	</div>
	    	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Rekanan</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="rekanan" required="" onchange="loadDataKontrak($(this)), loadDataLaporanHasil($(this))" disabled="">
        					<option value="">--- Pilih Rekanan ---</option>
        					@foreach($rekanan AS $key=>$value)
        						<option value="{{ $value->id_kelompok }}">{{ $value->nama }}</option>
        					@endforeach
        				</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" type="text" name="tanggal" placeholder="Tanggal" value="{{ date('d/m/Y') }}" required="" readonly="" disabled="" />
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Tagihan</label>
        			<div class="col-sm-8">
        				<input class="form-control" type="text" name="tanggal_tagihan" placeholder="Tanggal Tagihan" value="{{ date('d/m/Y') }}" required="" readonly="" disabled="">
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4 col-md-4">No. Tagihan</label>
        			<div class="col-sm-8 col-md-8">
        				{{ Form::text('no_tagihan', "", ['class'=>'form-control', 'placeholder'=>'Nomor Tagihan', "required"=>"", 'disabled'=>'']) }}
        			</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" disabled=""></textarea>
        			</div>
        		</div>
        		
        	</div>

        	<div class="col-sm-6">
        		
        		<div class="form-group">
        			<label class="control-label col-sm-4">Sales Kontrak</label>
        			<div class="col-sm-8">
        				<select name="id_sales_contract" class="form-control select-search" placeholder="Pilih Kontrak" disabled="">

        				</select>
        			</div>
        		</div>
        		
        		<div class="form-group">
        			<label class="control-label col-sm-4">Laporan Hasil</label>
        			<div class="col-sm-8">
        				<select name="id_laporan_hasil" multiple="multiple" class="form-control set-select" placeholder="Pilih Laporan Hasil" disabled="">

        				</select>
        			</div>
        		</div>

				<div class="form-group">
    				{{ Form::label("Bayar dari", "", ['class'=>'control-label col-sm-4']) }}
	    			<div class="col-sm-8">
	    				{{ Form::select("dari_akun", $akun, [], ['class'=>'form-control select-search', 'disabled'=>'']) }}
	    				{{ Form::hidden("tipe_dari_akun", "K") }}
	    			</div>
    			</div>

				<div class="form-group">
    				{{ Form::label("No. Transaksi", "", ['class'=>'control-label col-sm-4']) }}
	    			<div class="col-sm-8">
	    				{{ Form::text("nomor_transaksi", '', ['class'=>'form-control', 'placeholder'=>'Nomor Transaksi', 'required'=>'', 'disabled'=>'']) }}
	    			</div>
    			</div>

	    		<!-- <div class="form-group">
	    			{{ Form::label("&nbsp;", "", ['class'=>'control-label col-sm-4']) }}
	    			<div class="col-sm-8">
	    				<div class="checkbox">
	    					<label>
		    					{{ Form::checkbox("bayar_nanti", '1', false, ['class'=>'styled']) }} Bayar Nanti
	    					</label>
	    				</div>
	    			</div>
	    		</div> -->

        	</div>


    		<div class="col-sm-12 col-md-12">
    			<table class="table table-striped" id="tabel-transaksi-pengeluaran">
    				<thead>
    					<tr>
    						<th>Pengeluaran untuk</th>
    						<th>Keterangan</th>
    						<th>Nominal</th>
    						<th>Tipe</th>
    					</tr>
    				</thead>
    				<tbody>

	    				@if(sizeOf($tagihan_detail) > 0)
	    				@foreach($tagihan_detail AS $key=>$value)

	    				<tr>
				          <td style='vertical-align:top;' width='35%'>
				          	@php
				          		$akun = \App\Perkiraan::find($value->ke_akun);
				          	@endphp
				          	{{ format_akun_perkiraan($akun) }}
				          </td>
				          <td style='vertical-align:top;'>
				          	{{ $value->keterangan }}
				          </td>
				          <td style='vertical-align:top; text-align: right;'>
				          	{{ "Rp".rupiah2($value->nominal_transaksi, 2) }}
				          </td>
				          <td style='vertical-align:top;' width='10%'>
				          	<?php 
				          	$arr_tipe = ['D'=>'Debet', 'K'=>'Kredit'];
				          	echo $arr_tipe[$value->tipe_id_akun];
				          	?>
				          </td>
				        </tr>"

	    				@endforeach
	    				@endif

    				</tbody>
    				<tfoot>

    				</tfoot>
    			</table>
    		</div>

	    </div>

	    <div class="panel-footer">
			<div class="col-sm-12 col-md-12 text-right">
		    	<a href="{{ url('sales_contract_tagihan') }}" class="btn btn-md btn-warning">Kembali</a>
		    	<a href="{{ url('sales_contract_tagihan/'.$tagihan->id.'/edit') }}" class="btn btn-md btn-default"><i class="fa fa-pencil"></i> Edit</a>
			</div>
	    </div>

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

<script type="text/javascript">
	var selectIdKontrak = $("#form-sales-contract-tagihan select[name=id_sales_contract]");
	var selectIdLaporanHasil = $("#form-sales-contract-tagihan select[name=id_laporan_hasil]");
	var detailKontrak;

	$(document).ready(function(){

		$('.set-select').select2({
	        minimumResultsForSearch: Infinity
	    });

	    $('.set_select2').select2();

	    $("#form-sales-contract-tagihan input[name=bayar_nanti]").click(function(){
	    	var nomor_transaksi = $("#form-sales-contract-tagihan input[name=nomor_transaksi]");
	    	if($(this).is(":checked")){
	    		if(nomor_transaksi.is(':required'))	{
	    			nomor_transaksi.removeAttr('required');
	    		}
	    	}else{

	    		if(!nomor_transaksi.is(':required'))	{
	    			nomor_transaksi.attr('required', '');
	    		}
	    	}
	    	
	    });

		@if(isset($tagihan))
		edit_data_tagihan();
		$("input[name='nominal_transaksi[]']").trigger("keyup");
		@endif
	});

	function loadDataKontrak(this_) {
		var id_rekanan = this_.val();
		$.ajax({
			url : "{{ url('sales_contract_tagihan/data_kontrak') }}",
			type : "get",
			data : {id_rekanan : id_rekanan},
			dataType : 'json',
			success : function(result){
				selectIdKontrak.html("");
				selectIdKontrak.append(new Option("-", "", true, true));
				if(result.length > 0){
					$.each(result, function(id, val){
						var opt = new Option(val.no_kontrak, val.id, false, false);
						selectIdKontrak.append(opt);
					});	

				}
				selectIdKontrak.trigger("change");
			},
			complete : function(){
				@if(isset($tagihan))
					$("#form-sales-contract-tagihan select[name=id_sales_contract]").val('{{$tagihan->id_sales_contract}}').trigger("change");
				@endif
			}
		});
	}

	function loadDataLaporanHasil(this_){
		var id_rekanan = this_.val();
		$.ajax({
			url : "{{ url('sales_contract_tagihan/data_laporan_hasil') }}",
			type : "get",
			data : {id_rekanan : id_rekanan},
			dataType : 'json',
			success : function(result){
				selectIdLaporanHasil.html("");
				if(result.length > 0){
					$.each(result, function(id, val){
						var opt = new Option(val.noAuto+""+val.noAuto_ext, val.id_barangmasuk, false, false);
						selectIdLaporanHasil.append(opt);
					});	

				}
				selectIdLaporanHasil.trigger("change");
			},
			complete : function(){
				@if(isset($tagihan))
					$("#form-sales-contract-tagihan select[name=id_laporan_hasil]").val('{{$tagihan->id_barangmasuk}}').trigger("change");
				@endif
			}
		});
	}

	function tambah_row_akun() {
		var rows = "<tr>"+
	          "<td class='hide'>"+
	          	"<input type='text' name='id_tagihan_detail[]' value=''  class='form-control' />"+
	          	// "<input type='text' name='tipe_ke_akun[]' value='D'  class='form-control' />"+
	          "</td>"+
	          "<td style='vertical-align:top;' width='35%'><select name='ke_akun[]' class='form-control set_select2'><?php foreach ($id_akun as $key => $value){ echo "<option value='$key'>$value</option>"; } ?></select></td>"+
	          "<td style='vertical-align:top;'><input type='text' name='keterangan_detail[]' value='-'  class='form-control' /></td>"+
	          "<td style='vertical-align:top;'><input type='text' name='nominal_transaksi[]' value='0'  class='form-control text-right' onkeyup='setFormatMoney($(this), \"td\")' /><strong><span class='help-block text-right'>0,00</span></strong></td>"+
	          "<td style='vertical-align:top;' width='8%'><select name='tipe_ke_akun[]' class='form-control set_select2'><?php $arr_tipe = ['D'=>'Debet', 'K'=>'Kredit']; foreach ($arr_tipe as $key => $value){ echo "<option value='$key'>$value</option>"; } ?></select></td>"+
	          "<td style='vertical-align:top;'><button type='button' class='btn btn-xs btn-link text-danger' onclick='hapusRowAkun($(this))'><i class='icon-trash'></i></button></td>"+
	        "</tr>";
	  	$("table#tabel-transaksi-pengeluaran > tbody").append(rows);
		$(".set_select2").select2();
	}

	@if(isset($tagihan))
	function edit_data_tagihan() {
		$("#form-sales-contract-tagihan [name=lokasi]").val('{{$tagihan->lokasi}}').trigger("change");
		@if($tagihan->cv==1)
			$("#form-sales-contract-tagihan [name=cv]").prop("checked", true).trigger("change");
		@else
			$("#form-sales-contract-tagihan [name=cv]").prop("checked", false).trigger("change");
		@endif
		$("#form-sales-contract-tagihan [name=rekanan]").val('{{$tagihan->id_rekanan}}').trigger("change");
		$("#form-sales-contract-tagihan [name=tanggal]").val('{{date_db($tagihan->tanggal, true)}}');
		$("#form-sales-contract-tagihan [name=tanggal_tagihan]").val('{{date_db($tagihan->tanggal_tagihan, true)}}');
		$("#form-sales-contract-tagihan [name=nomor_transaksi]").val('{{$tagihan->nomor_transaksi}}');
		$("#form-sales-contract-tagihan [name=no_tagihan]").val('{{$tagihan->nomor_tagihan}}');
		$("#form-sales-contract-tagihan [name=keterangan]").val('{{$tagihan->keterangan}}');
		// detail_data_kontrak('{{ $tagihan->id }}');
	}
	@endif

	function lihatDetailKontrak() {
		var id_sales_contract = selectIdKontrak.val();
		$.ajax({
			url : "{{ url('sales_contract_tagihan/detail_kontrak') }}",
			type : "get",
			data : {id_kontrak : id_sales_contract},
			dataType : 'json',
			success : function(result){
				detailKontrak = result;
				console.log(detail);
			}
		});
	}

	function hapusRowAkun(this_) {
		var row = this_.parents("tr");
		row.remove();
	}

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

@endsection