@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> Pembelian</a></li>
			<li class="active">Order Pembelian</li>

		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Order Pembelian</h5>
            
			<div class="heading-elements">
				<a href="{{ url('orderbeli') }}" class="btn btn-sm btn-warning">Kembali</a>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<form class="form-horizontal" method="post" action="{{ url('orderbeli/simpan') }}" autocomplete="off" >
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_orderbeli" value="{{ $data['data']['id_orderbeli'] }}">
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Input</label>
        			<div class="col-sm-8">
        				<input class="form-control datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):$data['data']['tanggal'] }}" required readonly />
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No Order</label>
        			<div class="col-sm-8">
        				{{ Form::text('no_order', $data['data']['no_order'], ['class'=>'form-control', 'readonly'=>'']) }}
        			</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" readonly="">{{ $data['data']['keterangan'] }}</textarea>
        			</div>
        		</div>
        	</div>

        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Penyedia</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="penyedia" readonly="" disabled="">
        					<option value="">--- Pilih Penyedia ---</option>
        					@php foreach($data['supplier'] as $s){ 
        					$sel = $data['data']['id_penyedia'];
        					@endphp

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

        					@php } @endphp
        				</select>
        			</div>
        		</div>
        	</div>

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

        			<div class="form-group">
        				{{ Form::label("Tempat Penyerahan", "", ['class'=>'control-label col-sm-4']) }}
        				<div class="col-sm-8">
        					{{ Form::text("tempat_serah", $data['data']['tempat_serah'], ['class'=>'form-control', 'readonly'=>'']) }}
        				</div>
        			</div>

        			<div class="form-group">
        				{{ Form::label("Tanggal Penyerahan", "", ['class'=>'control-label col-sm-4']) }}
        				<div class="col-sm-8">
        					{{ Form::text("tanggal_serah", $data['data']['tanggal_serah'], ['class'=>'form-control datepicker', 'readonly'=>'']) }}
        				</div>
        			</div>
        		</div>
        	</div>

        	<div class="col-sm-12 form-group">
	        	<h5 class="panel-title">Detail Barang</h5>
        	</div>
        	<div class="col-ms-12">
        	<table class="table table-stripped" id="table_barang">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Kode Barang</th>
					<th>Nama Barang</th>
					<th>Harga Satuan</th>
					<th>Satuan</th>
					<th>Jumlah</th>
                    <th>Nominal</th>
					<th class="text-center">&nbsp;</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
        	</div>
        	
        	
             <div class="col-md-6"></div>
             <div class="form-group col-md-6">
             
              <div class="panel-body">
	    	
           <table class="table table-stripped">
	    	<tbody>
				<tr>
					<td>Total</td>
					<td></td>
                    <td id="td_total" class="pull-right"></td>
				</tr>
			</tbody>
	    </table>
             </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--->
<div class="modal fade" id="exampleModal"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</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">
	      			<input type="hidden" name="popup_id_table">
	      			<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_barang">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah <b id="popup_satuanawal"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlah" class="form-control number-only">
	      				</div>
	      			</div>{{-- 
	      			<div class="form-group" id="div_konversi">
	      				<label class="control-label col-sm-4">Jumlah <b id="popup_satuanakhir"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlahakhir" class="form-control number-only">
	      				</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" class="form-control text-right number-only">
	      					<p class="help-block pull-right" id="help_popup_harga"></p>
	      				</div>
	      			</div>
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<input type="hidden" name="store_konversi_awal" class="form-control">
<input type="hidden" name="store_konversi_akhir" class="form-control">
<input type="hidden" name="store_konversi" class="form-control">

<!----Modal---->
<script type="text/javascript">
	$("#btn_tambah_item").click(function(){
		$("[name=popup_id_table]").val('');
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_jumlah]").val('');
		$("[name=popup_harga]").val('');
		$("#help_popup_harga").text('');
		$("#div_konversi").addClass('hide');
		$("#exampleModal").modal('show');
	})

	var table_barang;
	var tb_no = parseInt(10000);
	$(document).ready(function(){
		table_barang = $("#table_barang").DataTable({
			"paging": false
		});
		table_barang.on( 'order.dt search.dt', function () {
            table_barang.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

        // get_barang();
        var id_orderbeli = '{{ $data['data']['id_orderbeli'] }}';
        get_edit(id_orderbeli);
	})

	function get_barang(this_){
		var transaksi = "";
		$.ajax({
			type : 'get',
			url: '{{ url('orderbeli/get_barang') }}',
			data : {id_transaksi : transaksi},
			dataType : 'json',
			success: function(respon){
				$("[name=popup_barang]").html('');
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_barang]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].satuan+'">'+respon[i].kategori+' - '+respon[i].nama_sub+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+'</option>');
					}
				}
			}
		});
	}

	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id_table]").val();
		var id_barang = $("[name=popup_barang]").val();
		var nama_barang = $("[name=popup_barang] :selected").attr('barang');
		var kode = $("[name=popup_barang] :selected").attr('kode');
		var jumlah = $("[name=popup_jumlah]").val();
		var satuan = $("[name=popup_barang] :selected").attr('satuan');

		
		var harga = $("[name=popup_harga]").val();
		var total = parseInt(jumlah)*parseInt(harga);
		if(id_barang != '' && jumlah != '' && harga != ''){
			if(id_data == ''){
				table_barang.row.add(['<div><center></center></div>',
					'<div id="list_kode'+tb_no+'">'+kode+'</div>',
					'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
					'<div id="list_harga'+tb_no+'">'+curr(harga)+'</div>',
					'<div id="list_satuan'+tb_no+'">'+satuan+'</div>',
					'<div id="list_jumlah'+tb_no+'">'+jumlah+'</div>',					
					'<div id="list_total'+tb_no+'">'+curr(total)+'</div>',
					'<div></div>'+
					'<input type="hidden" name="tabel_id[]" id="tabel_id'+tb_no+'" value="">'+
					'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+tb_no+'" value="'+id_barang+'">'+
					'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+tb_no+'" value="'+jumlah+'">'+					
					'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+tb_no+'" value="'+harga+'">'+
					'<input type="hidden" name="tabel_total[]" id="tabel_total'+tb_no+'" value="'+total+'">'+
					'<input type="hidden" name="tabel_satuan[]" id="tabel_satuan'+tb_no+'" value="'+satuan+'">'
					]).draw(false);
			}else{
				$("#list_kode"+id_data).text(kode);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_harga"+id_data).text(curr(harga));
				$("#list_jumlah"+id_data).text(jumlah);
				$("#list_satuan"+id_data).text(satuan);
				$("#list_total"+id_data).text(curr(total));

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);				
				$("#tabel_jumlah"+id_data).val(jumlah);
				
				$("#tabel_harga"+id_data).val(harga);
				$("#tabel_total"+id_data).val(total);
			}


			$("#exampleModal").modal('hide');
			tb_no++;
			get_jum();
		}
	
	})

	function get_edit(id){
		$.ajax({
			url: "{{ url('orderbeli/get_edit')}} ",
			type: 'post',
			data: {id : id,},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					for(i in respon){
						table_barang.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id_orderbeli_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_orderbeli_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_harga'+respon[i].id_orderbeli_barang+'">'+curr(respon[i].harga_satuan)+'</div>',
							'<div id="list_satuan'+respon[i].id_orderbeli_barang+'">'+respon[i].satuan+'</div>',
							'<div id="list_jumlah'+respon[i].id_orderbeli_barang+'">'+respon[i].jml_barang+'</div>',					
							'<div id="list_total'+respon[i].id_orderbeli_barang+'">'+curr(respon[i].total)+'</div>',
							'<div></div>'+
							'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon[i].id_orderbeli_barang+'" value="'+respon[i].id_orderbeli_barang+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id_orderbeli_barang+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+respon[i].id_orderbeli_barang+'" value="'+respon[i].jml_barang+'">'+					
							'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+respon[i].id_orderbeli_barang+'" value="'+respon[i].harga_satuan+'">'+
							'<input type="hidden" name="tabel_total[]" id="tabel_total'+respon[i].id_orderbeli_barang+'" value="'+respon[i].total+'">'+
							'<input type="hidden" name="tabel_satuan[]" id="tabel_satuan'+respon[i].id_orderbeli_barang+'" value="'+respon[i].satuan+'">'
							]).draw(false);
					}

					get_jum();
					netto();
				}
				
			}
		})
	}

	function edit_table(id){

		var id_barang = $("#tabel_idbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var satuan = $("#tabel_satuan"+id).val();
		var harga = $("#tabel_harga"+id).val();

		$("[name=popup_id_table]").val(id);
		$("[name=popup_barang]").val(id_barang).trigger('change');
		$("[name=popup_jumlah]").val(jumlah);
		$("[name=popup_satuanawal]").val(satuan);
		$("[name=popup_harga]").val(harga);
		$("#help_popup_harga").text(curr(harga));

		$("#exampleModal").modal('show');
	}

	$("[name=popup_barang]").change(function(){
		var satuan = $("[name=popup_barang] :selected").attr('satuan');

		$("#popup_satuanawal").text(satuan);

	})

	$("[name=popup_harga]").keyup(function(){
		var rp = curr($("[name=popup_harga]").val());
		$("#help_popup_harga").text(rp);
	})

	function get_jum(){
		var jum = parseInt(0);
		$('input[id^="tabel_total"]').each(function() {
              // alert($(this).val());
              var val = ($(this).val());
              jum += parseInt(val);
              
            });
		$("#td_total").text(curr(jum));
		$("[name=td_total]").val(jum);
		netto();
	}

	$("[name=td_bunga]").keyup(function(){
		var rp = curr($("[name=td_bunga]").val());
		$("#help_bunga").text(rp);
		netto();
	})

	function netto(){
		var total = parseInt($("[name=td_total]").val());
	//	var pot = parseInt($("[name=td_potongan]").val());
		var bunga = parseInt($("[name=td_bunga]").val());
		var netto = parseInt(0);
		if(bunga == '' || bunga == 0 || isNaN(bunga)){
			bunga = parseInt(0);
		}

		if(total == '' || total == 0 || isNaN(total)){
			total = parseInt(0);
		}

		netto = total+bunga; //+bunga
		$("#td_netto").text(curr(netto));
		$("[name=td_netto]").val(netto);
	//	console.log(total+"\n"+pot+"\n"+bunga+"\n"+netto);
	}

	function hapus(d){
		table_barang.row(d.parents('tr')).remove().draw();
		get_jum();
		netto();
		//  alert(d);
	}
</script>

@endsection