@extends('layout')


@section('content')
<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> Inventori</a></li>
			<li class="active">Penerimaan Barang</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Penerimaan Barang</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<form class="form-horizontal" action="{{url('penerimaan/simpan')}}" method="post" autocomplete="off" enctype="multipart/form-data">
	    		<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    		<input type="hidden" name="id" value="{{ $data['pembelian']->id_pembelian }}">
	    		<input type="hidden" name="id_induk" value="{{ $data['penerimaan_induk']['id_induk'] }}">
	    		<div class="col-sm-6">
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">No. Faktur Pembelian</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="no_pembelian" class="form-control" value="{{$data['pembelian']->no_faktur.$data['pembelian']->sufix_no_faktur}}" readonly>
	    					<input type="hidden" name="id_pembelian" value="{{$data['pembelian']->id_pembelian}}">
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Tanggal Input</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="tgl_diterima" class="form-control" value="{{ date('d-m-Y', strtotime($data['pembelian']->tanggal)) }}" readonly>
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Tanggal Faktur</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="tgl_transaksi" class="form-control" value="{{ date('d-m-Y', strtotime($data['pembelian']->tanggal_faktur)) }}" readonly>
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Penyedia</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="penyedia" class="form-control" value="{{ $data['pembelian']->nama_penyedia }}" readonly>
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Lokasi Pembelian</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="lokasi" class="form-control" value="{{ $data['pembelian']->nama_lokasi }}" readonly>
	    				</div>
	    			</div>
	    		</div>
	    		<div class="col-sm-6">
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Tanggal Penerimaan</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="tgl_terima" class="form-control datepicker" onchange="get_faktur($(this))" value="{{ ($data['penerimaan_induk']['tanggal_terima'] == '') ? date('d-m-Y'):tgl_full($data['penerimaan_induk']['tanggal_terima'], '') }}" readonly>
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="col-sm-4 control-label">Nomor Faktur</label>
	    				<div class="col-sm-3">
	    					<input type="text" name="nomor_faktur" class="form-control" value="{{ $data['penerimaan_induk']['no_faktur'] }}" required>
	    				</div>
	    				<div class="col-sm-5">
	    					<input type="text" name="faktur" class="form-control" value="{{ $data['penerimaan_induk']['faktur'] }}" required>
	    				</div>
	    			</div>

	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Nomor Surat Jalan</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="nomor_sj" class="form-control" value="{{ $data['penerimaan_induk']['no_suratjalan'] }}">
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Nomor Kendaraan</label>
	    				<div class="col-sm-2">
	    					<input type="text" name="nopol" class="form-control" value="{{ $data['penerimaan_induk']['nopol'] }}">
	    				</div>

	    				<label class="control-label col-sm-2">Nama Supir</label>
	    				<div class="col-sm-4">
	    					<input type="text" name="supir" class="form-control" value="{{ $data['penerimaan_induk']['nama_supir'] }}">
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Status Penerimaan</label>
	    				<div class="col-sm-8">
	    					<select class="form-control select-search" name="status_pembelian">
	    						<option value="0" @php if($data['pembelian']->status == '0'){ echo "selected"; } @endphp>Belum Diterima</option>
	    						<option value="2" @php if($data['pembelian']->status == '2'){ echo "selected"; } @endphp>Diterima Sebagian</option>
	    						<option value="1" @php if($data['pembelian']->status == '1'){ echo "selected"; } @endphp>Sudah Diterima</option>
	    					</select>
	    				</div>
	    			</div>
	    			
	    		</div>
	    		<div class="col-sm-6">
	    			
	    		</div>
	    		<div class="col-sm-12">
	    			<div class="panel panel-primary">
                
                	</div>
                	<h5>Detail Barang</h5>
	    
		            <table class="table table-stripped table_carabayar">
			    	<thead>
						<tr>
							<th>Nama Barang</th>
		                    <th>Qty. Nota</th>
		                    <th>Nama Satuan</th>
							<th>Qty. Terima</th>
		                    <th>Gudang</th>
						</tr>
					</thead>
					<tbody>
						@foreach($data['detail'] as $row) 
						<tr>
							<td>{{$row['kode_barang'].' - '.$row['nama_barang']}}</td>
							<td>{{$row['jml_barang']}}</td>
							<td>{{$row['nama_satuan']}}</td>
							<td><input type='text'  value='{{ ($row['jml_terima'] == '') ? $row['jml_barang']:$row['jml_terima'] }}' class='form-control text-right number-only' name='jml_terima[]'  min='0' max="{{ $row['jml_barang'] }}" id="jml_barang{{ $row['jml_barang'] }}" onkeyup="maksimal($(this))">
							
							<input type='hidden'  value='{{$row['id_beli_barang']}}' class='form-control' name='id_beli_barang[]'>
							<input type='hidden'  value='{{$row['id_penerimaan']}}' class='form-control' name='id_penerimaan[]'>

							</td>
							<input type="hidden" name="gudang_store">
							<td align="center">
								<select name="gudang[]" class="form-control select-search gudang" required>
									@foreach($data['gudang'] as $d) 
									<option value="{{ $d->id_gudang }}" {{ ($row['gudang'] == $d->id_gudang) ? "selected":"" }} >{{ $d->nama_gudang }}</option>

									@endforeach
								</select>
	                      </td>
						</tr>
						@endforeach
						
					</tbody>
			    </table>
	    		</div>
	    		<div class="col-sm-12">
	    		
	    		@php echo ($data['pembelian']->status != '1') ? '<button type="submit" class="btn btn-success btn-md pull-right">Simpan</button>':"" @endphp
	    		</div>
	    		</div>
	    	</form>
	    	
	    </div>

	</div>
</div>

<!---Modal--->


<!----Modal---->

<script type="text/javascript">
	var tb;
	$(document).ready(function(){
	
		tb = $(".table_carabayar").DataTable({
			"paging": false
		});

		// tb.on( 'order.dt search.dt', function () {
  //           tb.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
  //               cell.innerHTML = i+1;
  //           } );
  //       } ).draw();
  //       tb.clear().draw();

		
	// function get_gudang(){
	// 	$.ajax({
	// 		url: "{{ url('penerimaan/get_gudang') }}",
	// 		type: 'get',
	// 		dataType: 'json',
	// 		success: function(result){
	// 			if(result.status == 1){
	// 				$(".gudang").html('');
	// 				$(".gudang").append('<option value="">--- Pilih Gudang ---</option>');
	// 				for(i in result.data){
	// 					$(".gudang").append('<option value="'+result.data[i].id_gudang+'">'+result.data[i].nama_gudang+'</option>');
	// 				}
					
	// 			}
	// 		}
	// 	})
	// }

	// function get_detail(){
	// 	var id = $('[name=id]').val();
	// 	$.ajax({
	// 		url: "{{ url('penerimaan/tampil/')}}",
	// 		dataType : {id:id},
	// 		type: 'get',
	// 		dataType: 'json',
	// 		success: function(respon){
	// 			tb.clear().draw();
	// 			for(i in respon){
	// 				tb.row.add(['<div></div>',
	// 					'<div>'+respon[i].nama+'</div>',
	// 					'<div>'+respon[i].telepon+'</div>',
	// 					'<div>'+respon[i].alamat+'</div>',
	// 					'<div><center>'+respon[i].aksi+'</center></div>'+
	// 					'<input type="hidden" id="table_id'+respon[i].id_penyedia+'" value="'+respon[i].id_penyedia+'">'+
	// 					'<input type="hidden" id="table_nama'+respon[i].id_penyedia+'" value="'+respon[i].nama+'">'+
	// 					'<input type="hidden" id="table_pic'+respon[i].id_penyedia+'" value="'+respon[i].pic+'">'+
	// 					'<input type="hidden" id="table_npwp'+respon[i].id_penyedia+'" value="'+respon[i].npwp+'">'+
	// 					'<input type="hidden" id="table_email'+respon[i].id_penyedia+'" value="'+respon[i].email+'">'+
	// 					'<input type="hidden" id="table_telepon'+respon[i].id_penyedia+'" value="'+respon[i].telepon+'">'+
	// 					'<input type="hidden" id="table_alamat'+respon[i].id_penyedia+'" value="'+respon[i].alamat+'">'+
	// 					'<input type="hidden" id="table_kota'+respon[i].id_penyedia+'" value="'+respon[i].id_kab+'">'+
	// 					'<input type="hidden" id="table_no_rek'+respon[i].id_penyedia+'" value="'+respon[i].no_rek+'">'						
	// 					]).draw(false);
	// 			}
	// 		}
	// 	})
	// }
		$("[name=tgl_terima]").trigger('change');
	})

	function maksimal(x) {
    	var min = parseInt(x.attr('min'));
    	var max = parseInt(x.attr('max'));

    	if(x.val() > max) {
    		x.val(max);
    	}else if(x.val() < min){
    		x.val(min);
    	}
	}

	$("[name=status_pembelian]").change(function(){
		var value = $(this).val();
		
	})

	function get_faktur(this_){
		var tgl = this_.val();
		$.ajax({
			url: "{{ url('penerimaan/get_faktur') }}",
			type: 'get',
			dataType: 'json',
			data: {id : $("[name=id_induk]").val() , tgl : tgl},
			success: function(respon){
				$("[name=nomor_faktur]").val(respon.no_faktur);
				$("[name=faktur]").val(respon.faktur);
			}
		})


	}
</script>
@endsection