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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Data Kontrak</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('sales_contract') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>

	    <form class="form-horizontal" role="form" method="post" id="form-data-kontrak" action="{{ $url_submit }}">
	    {{ csrf_field() }}
	    {{ method_field($method)}}
	    @if(isset($kontrak))
	    {{ Form::hidden("id_kontrak", $kontrak->id) }}
	    @endif
	    <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">
		    					@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']) }}
		    					</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="">
        					<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 set_datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ date('d/m/Y') }}" required="" readonly="" />
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Kontrak</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal_kontrak" placeholder="Tanggal Kontrak" value="{{ date('d/m/Y') }}" required="" readonly="">
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4 col-md-4">No. Kontrak</label>
        			<div class="col-sm-8 col-md-8">
        				{{ Form::text('no_kontrak', "", ['class'=>'form-control', 'placeholder'=>'Nomor Kontrak']) }}
        			</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 class="col-sm-6">
    			<div class="form-group">
    				{{ Form::label("Jadwal Pengiriman", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::text("tanggal_kirim", date("d/m/Y"), ['class'=>'form-control set_datepicker', 'readonly'=>'']) }}
    				</div>
    			</div>

        		<div class="form-group">
    				{{ Form::label("Syarat Pembayaran", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::textarea("syarat_bayar", '-', ['class'=>'form-control', 'rows'=>2]) }}
    				</div>
    			</div>

        	</div>

	    	<!-- Detail Barang -->
	    	<div class="col-sm-12 col-md-12">
	    		<h5 class="panel-title">Detail Barang</h5>
	    		<button type="button" class="btn btn-sm btn-success" id="btn_tambah_barang">Tambah Barang</button>

	    		<table class="table" id="tabel-detail-barang">
	    			<thead>
	    				<tr>
	    					<th>#</th>
	    					<th>Warna</th>
	    					<th>Barang</th>
	    					<th>Jml</th>
	    					<th>Satuan</th>
	    					<th>Harga Satuan</th>
	    					<th>Total</th>
	    					<th>Keterangan</th>
	    					<th>Pajak</th>
	    					<th>&nbsp;</th>
	    				</tr>
	    			</thead>
	    			<tbody>
	    				
	    			</tbody>
	    			<tfoot>
	    			</tfoot>
	    		</table>
	    	</div>

	    </div>

	    <div class="panel-footer text-right">
    		<div class="col-sm-12 col-md-12">
    			<a href="{{ url('sales_contract') }}" class="btn btn-md btn-default">Batal</a>
    			<button type="submit" class="btn btn-md btn-success">Simpan</button>
    		</div>
	    </div>

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

<!---Modal--->
<div class="modal fade" id="modal-data-barang"  role="dialog" aria-labelledby="modalDataBarang" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="modalDataBarang">Data Barang</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" autocomplete="off" id="form-data-barang">
	      			<input type="hidden" name="popup_id_detail">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Warna</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_id_warna_komposisi" required="">
	      						<option value="">-- Pilih --</option>
	      						@foreach($warna_komposisi AS $key=>$value)
	      						<option value="{{ $value->id_komposisi }}" data-optname="{{ $value->nama.'('.$value->inisial.')' }}">{{ $value->nama." (".$value->inisial.")" }}</option>
	      						@endforeach
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Barang</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_id_barang" required="">
	      						<option value="">-- Pilih --</option>
	      						@foreach($barang AS $key=>$value)
	      						@php
	      							$opt_name = $value['kategori'].' - '.$value['sub_kategori'].' - '.$value['kode_barang'].' - '.$value['nama_barang'];
									if($value['id_kelompok']==4){
										$opt_name = $value['kode_barang'].' - '.$value['nama_barang'];
									}
	      						@endphp
	      						<option value="{{ $value['id_barang'] }}" data-optname="{{ $opt_name }}" data-optkode="{{ $value['kode_barang'] }}" data-optbarang="{{ $value['nama_barang'] }}">{{ $opt_name }}</option>
	      						@endforeach
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah</label>
	      				<div class="col-sm-3">
	      					<input type="text" name="popup_jumlah" class="form-control text-right number-only" value="0" onkeyup="setFormatNumber($(this), 'div')">
	      					<p class="help-block text-right">0</p>
	      				</div>

	      				<label class="control-label col-sm-1">Satuan</label>
	      				<div class="col-sm-2">
	      					<select name="popup_id_satuan" class="form-control select-search">
	      						<option value="">-- Pilih --</option>
	      						@foreach ($satuan AS $key=>$value)
	      						<option value="{{ $value->id_satuan }}" data-optname="{{ $value->nama_satuan }}" data-optsatuan="{{ $value->satuan }}">{{ $value->nama_satuan }}</option>
	      						@endforeach
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Harga Satuan</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_harga_satuan" class="form-control text-right number-only" value="0" onkeyup="setFormatMoney($(this), 'div')">
	      					<p class="help-block pull-right">Rp 0,00</p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Keterangan</label>
	      				<div class="col-sm-8">
	      					<textarea name="popup_keterangan" class="form-control" rows="3"></textarea>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Termasuk PPN</label>
	      				<div class="col-sm-8">
		    				<div class="checkbox">
		    					<label>
			    					{{ Form::checkbox("popup_termasuk_pajak", '1', false, ['class'=>'styled', 'onchange'=>'termasukPPN($(this))']) }}
		    					</label>
		    				</div>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">PPN</label>
	      				<div class="col-sm-8">
	      					<select name="popup_pajak" class="form-control">
	      						<option value="0">Tidak</option>
	      						<option value="1">Ya</option>
	      					</select>
	      				</div>
	      			</div>
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	    		<button type="button" class="btn btn-default" data-dismiss="modal" aria-label="Batal">Batal</button>
	        	<button type="button" class="btn btn-primary" id="popup_btn_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<script type="text/javascript">
	var tabeldatabarang;
	var temp_no = parseInt(10000);

	$(document).ready(function () {

		tabeldatabarang = $("#tabel-detail-barang").DataTable({
			filter : false,
			paginate : false,
		});
		tabeldatabarang.on( 'order.dt search.dt', function () {
            tabeldatabarang.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

		@if(isset($kontrak))
		edit_data_kontrak();
		@endif

		$("#btn_tambah_barang").click(function(e){
			resetForm();
			showModal($("#modal-data-barang"));
		});

		$("#popup_btn_simpan").click(function(){
			var id_detail = $("[name=popup_id_detail]").val();
			var id_barang = $("[name=popup_id_barang]").val();
			var nama_barang = $("[name=popup_id_barang] :selected").data('optname');
			var barang = $("[name=popup_id_barang] :selected").data('optbarang');
			var kode = $("[name=popup_id_barang] :selected").data('optkode');
			var jumlah = $("[name=popup_jumlah]").val() || 0;
			var id_satuan = $("[name=popup_id_satuan]").val();
			var satuan = $("[name=popup_id_satuan] :selected").data('optsatuan');
			var harga_satuan = $("[name=popup_harga_satuan]").val() || 0;
			var id_warna = $("[name=popup_id_warna_komposisi]").val();
			var nama_warna = $("[name=popup_id_warna_komposisi] :selected").data('optname');
			var keterangan = $("[name=popup_keterangan]").val();
			var termasuk_pajak = $("[name=popup_termasuk_pajak]:checked").val() || 0;
			var pajak = $("[name=popup_pajak]").val() || 0;

			var total = parseFloat(jumlah)*parseFloat(harga_satuan);
			if(id_barang != '' && jumlah != '' && harga_satuan != '' && id_satuan != ''){
				var label_ppn = "";
				// if(termasuk_pajak==1){
				// 	label_ppn = "<span class='badge badge-success'><i class='fa fa-plus'></i> PPN</span>";
				// }
				if(termasuk_pajak==1){
					label_ppn = "Termasuk PPN";
				}else if(termasuk_pajak==0 && pajak==1){
					label_ppn = "PPN";
				}
				console.log(id_detail);
				if(id_detail == ''){
					tabeldatabarang.row.add([
						'<div><center></center></div>',
						'<div id="list_warna_'+temp_no+'">'+nama_warna+'</div>',
						'<div id="list_barang_'+temp_no+'">'+barang+'</div>',
						'<div id="list_jumlah_'+temp_no+'">'+jumlah+'</div>',
						'<div id="list_satuan_'+temp_no+'">'+satuan+'</div>',
						'<div id="list_harga_satuan_'+temp_no+'">'+accounting.formatMoney(harga_satuan)+'</div>',
						'<div id="list_total_'+temp_no+'">'+accounting.formatMoney(total)+'</div>',
						'<div id="list_keterangan_'+temp_no+'">'+keterangan+'</div>',
						'<div id="list_pajak_'+temp_no+'">'+label_ppn+'</div>',
						'<div class="btn-group">'+
							'<button type="button" class="btn btn-sm btn-warning" onclick="edit_baris($(this), '+temp_no+')"><i class="fa fa-pencil"></i></button>'+
							'<button type="button" class="btn btn-sm btn-danger" onclick="hapus_baris($(this))"><i class="fa fa-trash"></i></button>'+
							/* === */
							'<input type="hidden" name="detailbarang[id_detail][]" id="tabel_id_detail_'+temp_no+'" value="'+temp_no+'">'+
							'<input type="hidden" name="detailbarang[id_warna_komposisi][]" id="tabel_id_warna_komposisi_'+temp_no+'" value="'+id_warna+'">'+
							'<input type="hidden" name="detailbarang[id_barang][]" id="tabel_id_barang_'+temp_no+'" value="'+id_barang+'">'+
							'<input type="hidden" name="detailbarang[jumlah][]" id="tabel_jumlah_'+temp_no+'" value="'+jumlah+'">'+
							'<input type="hidden" name="detailbarang[id_satuan][]" id="tabel_id_satuan_'+temp_no+'" value="'+id_satuan+'">'+
							'<input type="hidden" name="detailbarang[harga_satuan][]" id="tabel_harga_satuan_'+temp_no+'" value="'+harga_satuan+'">'+
							'<input type="hidden" name="detailbarang[keterangan][]" id="tabel_keterangan_'+temp_no+'" value="'+keterangan+'">'+
							'<input type="hidden" name="detailbarang[termasuk_pajak][]" id="tabel_termasuk_pajak_'+temp_no+'" value="'+termasuk_pajak+'">'+
							'<input type="hidden" name="detailbarang[pajak][]" id="tabel_pajak_'+temp_no+'" value="'+pajak+'">'+
						'</div>',
						]).draw(false);
				}else{
					$("#list_warna_"+id_detail).text(nama_warna);
					$("#list_barang_"+id_detail).text(barang);
					$("#list_jumlah_"+id_detail).html(jumlah);
					$("#list_satuan_"+id_detail).text(satuan);
					$("#list_harga_satuan_"+id_detail).text(accounting.formatMoney(harga_satuan));
					$("#list_total_"+id_detail).text(accounting.formatMoney(total));
					$("#list_keterangan_"+id_detail).text(keterangan);
					$("#list_termasuk_pajak_"+id_detail).text(label_ppn);

					$("#tabel_id_detail_"+id_detail).val(id_detail);
					$("#tabel_id_warna_komposisi_"+id_detail).val(id_warna);
					$("#tabel_id_barang_"+id_detail).val(id_barang);
					$("#tabel_jumlah_"+id_detail).val(jumlah);
					$("#tabel_id_satuan_"+id_detail).val(id_satuan);
					$("#tabel_harga_satuan_"+id_detail).val(harga_satuan);
					$("#tabel_keterangan_"+id_detail).val(keterangan);
					$("#tabel_termasuk_pajak_"+id_detail).val(termasuk_pajak);
					$("#tabel_pajak_"+id_detail).val(pajak);
				}

				temp_no++;
				
				$("#modal-data-barang").modal('hide');
			}

		});

	});
	
	@if(isset($kontrak))
	function edit_data_kontrak() {
		$("#form-data-kontrak [name=lokasi]").val('{{$kontrak->lokasi}}').trigger("change");
		@if($kontrak->cv==1)
			$("#form-data-kontrak [name=cv]").prop("checked", true).trigger("change");
		@else
			$("#form-data-kontrak [name=cv]").prop("checked", false).trigger("change");
		@endif
		$("#form-data-kontrak [name=rekanan]").val('{{$kontrak->id_rekanan}}').trigger("change");
		$("#form-data-kontrak [name=tanggal]").val('{{date_db($kontrak->tanggal, true)}}');
		$("#form-data-kontrak [name=tanggal_kontrak]").val('{{date_db($kontrak->tanggal_kontrak, true)}}');
		$("#form-data-kontrak [name=no_kontrak]").val('{{$kontrak->no_kontrak}}');
		$("#form-data-kontrak [name=keterangan]").val('{{$kontrak->keterangan}}');
		$("#form-data-kontrak [name=tanggal_kirim]").val('{{date_db($kontrak->tanggal_kirim, true)}}');
		$("#form-data-kontrak [name=syarat_bayar]").val('{{$kontrak->syarat_bayar}}');
		detail_data_kontrak('{{ $kontrak->id }}');
	}

	function detail_data_kontrak(id) {
		$.ajax({
			type : "get",
			url : "{{ url('sales_contract/get_detail_kontrak') }}/"+id,
			dataType : "json",
			success : function(result){
				console.log(result);
				var label_ppn = "";
				for(i in result){

					label_ppn = "";
					if(result[i].termasuk_pajak == 1){
						label_ppn = "Termasuk PPN";
					}else if(result[i].termasuk_pajak == 0 && result[i].pajak == 1){
						label_ppn = "PPN";
					}

					tabeldatabarang.row.add([
						'<div><center></center></div>',
						'<div id="list_warna_'+result[i].id+'">'+result[i].warna_komposisi+' ('+result[i].inisial_komposisi+')'+'</div>',
						'<div id="list_barang_'+result[i].id+'">'+result[i].nama_barang+'</div>',
						'<div id="list_jumlah_'+result[i].id+'">'+result[i].jumlah+'</div>',
						'<div id="list_satuan_'+result[i].id+'">'+result[i].satuan+'</div>',
						'<div id="list_harga_satuan_'+result[i].id+'">'+accounting.formatMoney(result[i].harga_satuan)+'</div>',
						'<div id="list_total_'+result[i].id+'">'+accounting.formatMoney(parseFloat(result[i].jumlah)*parseFloat(result[i].harga_satuan))+'</div>',
						'<div id="list_keterangan_'+result[i].id+'">'+result[i].keterangan+'</div>',
						'<div id="list_pajak_'+result[i].id+'">'+label_ppn+'</div>',
						'<div class="btn-group">'+
							'<button type="button" class="btn btn-sm btn-warning" onclick="edit_baris($(this), '+result[i].id+')"><i class="fa fa-pencil"></i></button>'+
							'<button type="button" class="btn btn-sm btn-danger" onclick="hapus_baris($(this))"><i class="fa fa-trash"></i></button>'+
							/* === */
							'<input type="hidden" name="detailbarang[id_detail][]" id="tabel_id_detail_'+result[i].id+'" value="'+result[i].id+'">'+
							'<input type="hidden" name="detailbarang[id_warna_komposisi][]" id="tabel_id_warna_komposisi_'+result[i].id+'" value="'+result[i].id_warna_komposisi+'">'+
							'<input type="hidden" name="detailbarang[id_barang][]" id="tabel_id_barang_'+result[i].id+'" value="'+result[i].id_barang+'">'+
							'<input type="hidden" name="detailbarang[jumlah][]" id="tabel_jumlah_'+result[i].id+'" value="'+result[i].jumlah+'">'+
							'<input type="hidden" name="detailbarang[id_satuan][]" id="tabel_id_satuan_'+result[i].id+'" value="'+result[i].id_satuan+'">'+
							'<input type="hidden" name="detailbarang[harga_satuan][]" id="tabel_harga_satuan_'+result[i].id+'" value="'+result[i].harga_satuan+'">'+
							'<input type="hidden" name="detailbarang[keterangan][]" id="tabel_keterangan_'+result[i].id+'" value="'+result[i].keterangan+'">'+
							'<input type="hidden" name="detailbarang[termasuk_pajak][]" id="tabel_termasuk_pajak_'+result[i].id+'" value="'+result[i].termasuk_pajak+'">'+
							'<input type="hidden" name="detailbarang[pajak][]" id="tabel_pajak_'+result[i].id+'" value="'+result[i].pajak+'">'+
						'</div>',
						]).draw(false);
				}

			}
		})
		
	}
	@endif

	function edit_baris(this_, id) {
		var row = this_.parents("tr");

		var id_detail = $("#tabel_id_detail_"+id).val();
		var id_warna = $("#tabel_id_warna_komposisi_"+id).val();
		var id_barang = $("#tabel_id_barang_"+id).val();
		var jumlah = $("#tabel_jumlah_"+id).val();
		var id_satuan = $("#tabel_id_satuan_"+id).val();
		var harga_satuan = $("#tabel_harga_satuan_"+id).val();
		var keterangan = $("#tabel_keterangan_"+id).val();
		var termasuk_pajak = $("#tabel_termasuk_pajak_"+id).val();
		var pajak = $("#tabel_pajak_"+id).val();

		$("[name=popup_id_detail]").val(id_detail);
		$("[name=popup_id_warna_komposisi]").val(id_warna).trigger('change');
		$("[name=popup_id_barang]").val(id_barang).trigger('change');
		$("[name=popup_jumlah]").val(jumlah).trigger("keyup");
		$("[name=popup_id_satuan]").val(id_satuan).trigger("change");
		$("[name=popup_harga_satuan]").val(harga_satuan).trigger("keyup");
		$("[name=popup_keterangan]").html(keterangan);
		if(termasuk_pajak==1){
			$("[name=popup_termasuk_pajak]").prop('checked', true);
		}else{
			$("[name=popup_termasuk_pajak]").prop('checked', false);
		}
		$("[name=popup_pajak]").val(pajak).trigger("change");


		$("#modal-data-barang").modal('show');

	}

	function hapus_baris(this_) {
		tabeldatabarang.row(this_.parents('tr')).remove().draw();
	}

	function resetForm() {
		$("#form-data-barang")[0].reset();

		$("[name=popup_id_detail]").val("");
		$("[name=popup_id_warna_komposisi]").val("").trigger('change');
		$("[name=popup_id_barang]").val("").trigger('change');
		$("[name=popup_jumlah]").val(0).trigger("keyup");
		$("[name=popup_id_satuan]").val("").trigger("change");
		$("[name=popup_harga_satuan]").val(0).trigger("keyup");
		$("[name=popup_keterangan]").html("");
		$("[name=popup_termasuk_pajak]").attr('checked', false).trigger("change");
		$("[name=popup_pajak]").val(0);
	}

	function termasukPPN(this_) {
		var is_checked = this_.prop("checked");
		if(is_checked==true){
			$("[name=popup_pajak]").val(1).trigger("change");
		}else{
			$("[name=popup_pajak]").val(0).trigger("change");
		}
	}

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

	function setFormatNumber(this_, p) {
		var parent = this_.parent(p);
		var data = this_.val();
		parent.find(".help-block").text(accounting.formatNumber(data));
	}
</script>

@endsection