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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Input Pembelian</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" method="post" autocomplete="off" >
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_pembelian" value="{{ $data['data']['id_pembelian']}}">
        	<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'] }}" disabled />
        			</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 datepicker" type="text" name="tanggal_faktur" placeholder="Tanggal Faktur" value="{{ $data['data']['tanggal_faktur'] }}" disabled/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No. Faktur</label>
        			<div class="col-sm-8">
        				<input class="form-control text-uppercase" type="text" name="no_faktur" placeholder="Nomer Faktur" value="{{ $data['data']['no_faktur'] }}" 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>{{ $data['data']['keterangan'] }}</textarea>
        			</div>
        		</div>
        	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Jenis Transaksi</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="transaksi" disabled>
        					@php foreach($data['transaksi'] as $s){ 
        					$sel = $data['data']['id_transaksi'];
        					@endphp

        					<option value="@php echo $s->id_transaksi @endphp" {{ ($s->id_transaksi == $sel) ? "selected":"" }} >@php echo  $s->nama_transaksi.' - '.$s->tipe; @endphp</option>

        					@php } @endphp
        				</select>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Cara Bayar</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="cara_bayar" disabled>
        					@php foreach($data['cara'] as $s){ 
        					$sel = $data['data']['id_carabayar'];
        					@endphp

        					<option value="@php echo $s->id_carabayar @endphp" {{ ($s->id_carabayar == $sel) ? "selected":"" }}>@php echo  $s->cara.' - '.$s->tipe; @endphp</option>

        					@php } @endphp
        				</select>
        			</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" 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="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>Jumlah</th>
					<th>Jumlah Konversi</th>
                    <th>Nominal</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 Pembelian</td>
					<td></td>
                    <td id="td_total" class="pull-right"></td>
				</tr>
                <tr>
					<td>Potongan Tunai</td>
					<td></td>
                    <td><input type="hidden" name="td_potongan" class="form-control no-symbol text-right" value="{{ $data['data']['potongan'] }}" disabled>
                    	<p class="help-block pull-right" id="help_popup_potongan">{{ "Rp ".rupiah($data['data']['potongan']) }}</p>
                    </td>
				</tr>
                <tr>
					<td>Ppn</td>
					<td></td>
                    <td><input type="hidden" name="td_ppn" class="form-control no-symbol text-right" value="{{ $data['data']['bunga'] }}" disabled>
                    	<p class="help-block pull-right" id="help_popup_ppn">{{ "Rp ".rupiah($data['data']['bunga']) }}</p>
                    </td>
				</tr>
                <tr>
					<td>Netto</td>
					<td></td>
                    <td id="td_netto" class="pull-right"></td>
				</tr>
			</tbody>
	    </table>
             </div>
             </div>
             <a href="{{url('pembelian')}}" class="btn bg-warning-400">Kembali</a>
             
	    </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>

<div class="modal fade" id="exampleModal2"  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_tableorder">
	      			<input type="hidden" name="popup_idlist">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Order Pembelian</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_order">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah </label>
	      				<div class="col-sm-8">
	      					<label class="control-label" name="popup_jum_pesan"></label>

	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah Diterima <b id="popup_satuanawal2"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jum_terima" class="form-control number-only" min="" max="" onkeyup="maksimal($(this))">
	      				</div>
	      			</div>
	      			<div class="form-group" id="div_konversiorder">
	      				<label class="control-label col-sm-4">Jumlah <b id="popup_satuanakhir2"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jum_terimaakhir" class="form-control number-only" onkeyup="maksimal($(this))">
	      				</div>
	      			</div>
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_ambil">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">

<input type="hidden" name="store_konversiorder" class="form-control">

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

		$("[name=popup_jumlahakhir]").val('');
		$("#popup_satuanawal").text('');
		$("#popup_satuanakhir").text('');
		$("#exampleModal").modal('show');	
	})

	var table_barang;
	var penyedia;
	var tb_no = parseInt(10000);
	var id_pembelian;
	$(document).ready(function(){
		table_barang = $("#table_barang").DataTable();
		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();

		id_pembelian = '{{ $data['data']['id_pembelian']}}';
		if(id_pembelian != ''){
			get_edit(id_pembelian);
			get_editorder(id_pembelian);
			$("[name=penyedia]").attr('disabled', true);
		}

		penyedia = $("[name=penyedia]").val();
		get_order(penyedia);
	})

	function get_barang(){
		$.ajax({
			url: '{{ url('pembelian/get_barang') }}',
			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 jumlah_konv = $("[name=popup_jumlahakhir]").val();
		if(isNaN(jumlah_konv) || jumlah_konv == ''){
			jumlah_konv = jumlah;
		}

		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_jumlah'+tb_no+'">'+jumlah+'</div>',
					'<div id="list_jumlah_konv'+tb_no+'">'+jumlah_konv+'</div>',
					'<div id="list_total'+tb_no+'">'+curr(total)+'</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_jumlah_konv[]" id="tabel_jum_konv'+tb_no+'" value="'+jumlah_konv+'">'+
					'<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_idorderbarang[]" id="tabel_id_orderbarang'+tb_no+'" value="">'
					]).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_jumlah_konv"+id_data).text(jumlah_konv);
				$("#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_jumlah_konv"+id_data).val(jumlah_konv);
				$("#tabel_harga"+id_data).val(harga);
				$("#tabel_total"+id_data).val(total);
			}


			$("#exampleModal").modal('hide');
			tb_no++;
			get_jum();
		}
	//	console.log(id_barang+"\n"+nama_barang+"\n"+jumlah+"\n"+harga);
	})

	function edit_table(id){

		var id_barang = $("#tabel_idbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var jumlah_konv = $("#tabel_jumlah_konv"+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_jumlahakhir]").val(jumlah_konv);
		$("[name=popup_harga]").val(harga);
		$("#help_popup_harga").text(curr(harga));

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

	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_potongan]").keyup(function(){
		var rp = curr($("[name=td_potongan]").val());
		$("#help_popup_potongan").text(rp);
		netto();
	})

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

	// $("[name=td_bunga]").keyup(function(){
	// 	netto();
	// })

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

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

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

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

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

	function get_edit(id){
		$.ajax({
			url: "{{ url('pembelian/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_beli_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_beli_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_harga'+respon[i].id_beli_barang+'">'+curr(respon[i].harga_satuan)+'</div>',
							'<div id="list_jumlah'+respon[i].id_beli_barang+'">'+respon[i].jml_barang+'</div>',
							'<div id="list_jumlah_konv'+respon[i].id_beli_barang+'">'+respon[i].jml_barang_konv+'</div>',
							'<div id="list_total'+respon[i].id_beli_barang+'">'+curr(respon[i].total)+'</div>'+
							'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon[i].id_beli_barang+'" value="'+respon[i].id_beli_barang+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id_beli_barang+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang+'">'+
							'<input type="hidden" name="tabel_jumlah_konv[]" id="tabel_jum_konv'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang_konv+'">'+
							'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+respon[i].id_beli_barang+'" value="'+respon[i].harga_satuan+'">'+
							'<input type="hidden" name="tabel_total[]" id="tabel_total'+respon[i].id_beli_barang+'" value="'+respon[i].total+'">'+
							'<input type="hidden" name="tabel_idorderbarang[]" id="tabel_id_orderbarang'+tb_no+'" value="">'
							]).draw(false);
					}

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


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

	$("[name=popup_barang]").change(function(){
		var id_barang = $("[name=popup_barang]").val();
		
		$.ajax({
			url: "{{ url('pembelian/get_konversi')}} ",
			type: 'post',
			data: {id : id_barang},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					$("#div_konversi").removeClass('hide');

					$("#popup_satuanawal").text(respon[0].satuan_awal);
					$("#popup_satuanakhir").text(respon[0].satuan_akhir);
					var nilai = parseInt(respon[0].nilai_akhir)/parseInt(respon[0].nilai_awal);
					$("[name=store_konversi]").val(nilai);
				}else{
					$("#div_konversi").addClass('hide');
					$("[name=popup_jumlahakhir]").val('');
					$("#popup_satuanawal").text(respon.satuan_awal);
					$("#popup_satuanakhir").text('');				
				}
			}
		})
	})

	$("[name=popup_jumlah]").keyup(function(){
		var jum = parseInt($("[name=popup_jumlah]").val());
		if(isNaN(jum) || jum == 0 || jum == ''){
			jum = parseInt(0);
		}
		var nilai = parseInt($("[name=store_konversi]").val());

		var tot = jum*nilai;
		$("[name=popup_jumlahakhir]").val(tot);
	})

	//Ambil Barang
	$("#btn_ambil_barang").click(function(){
		$("[name=popup_id_tableorder]").val('');
		$("[name=popup_order]").val(null).trigger('change');
		$("[name=popup_jum_pesan]").text('');
		$("[name=popup_jum_terima]").attr('min', 0);
		$("[name=popup_jum_terima]").attr('max', 0);
		$("[name=popup_jum_terima]").val('');
		
		$("#popup_satuanawal2").text('');
		$("#popup_satuanakhir2").text('');
		$("[name=popup_jum_terimaakhir]").val('');

		$("#div_konversiorder").addClass('hide');
		$("#exampleModal2").modal('show');
	})

	$("[name=penyedia]").change(function(){
		var id_penyedia = $("[name=penyedia]").val();
		table_barang.clear().draw();
		get_order(id_penyedia);
		var rp = parseInt(0);
		$("#td_total").text(curr(rp));
		$('[name=td_potongan]').val(rp);
		$('[name=td_ppn]').val(rp);
		$("#td_netto").text(curr(rp));
		$("#help_popup_potongan").text(curr(rp));
		$("#help_popup_ppn").text(curr(rp));

	})

	function get_order(id){
		$.ajax({
			url: "{{ url('pembelian/get_order')}} ",
			type: 'post',
			data: {id_penyedia : id},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				$("[name=popup_order]").html('');
				if(respon.length > 0){
					$("[name=popup_order]").append('<option value="">-- Pilih Barang --</option>');
					for(i in respon){
						$("[name=popup_order]").append('<option value="'+respon[i].id_orderbeli_barang+'" id_barang="'+respon[i].id_barang+'" nama_barang="'+respon[i].nama_barang+'" kode_barang="'+respon[i].kode_barang+'" harga_satuan="'+respon[i].harga_satuan+'">'+respon[i].no_faktur+' - '+respon[i].nama_penyedia+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+'</option>');
					}
				}
			}
		})
	}

	$("[name=popup_order]").change(function(){
		var id_order = $(this).val();
		var id_penyedia = $("[name=penyedia]").val();
		var id_barang = $("[name=popup_order] :selected").attr('id_barang');
		var nilai = 0;
		var id;
		if(id_penyedia != '' && id_order != ''){
			$.ajax({
				url: "{{ url('pembelian/get_order')}} ",
				type: 'post',
				data: {id_penyedia : id_penyedia, id_orderbeli : id_order},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){

					// $('input[id^=tabel_id]').each(function(){
					// //	id_order;
					// 	id = [($(this).val())];
					// })

					$.ajax({
						url: "{{ url('pembelian/get_konversi')}} ",
						type: 'post',
						data: {id : id_barang},
						headers : {
							'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
						},
						success: function(result){
							if(result.length > 0){
								$("#div_konversiorder").removeClass('hide');

								$("#popup_satuanawal2").text(result[0].satuan_awal);
								$("#popup_satuanakhir2").text(result[0].satuan_akhir);
								var nilai = parseInt(result[0].nilai_akhir)/parseInt(result[0].nilai_awal);
								$("[name=store_konversiorder]").val(nilai);
							}else{
								$("#div_konversiorder").addClass('hide');
								$("[name=popup_jumlahakhir2]").val('');
								$("#popup_satuanawal2").text(result.satuan_awal);
								$("#popup_satuanakhir2").text('');				
							}

							var total = parseInt(0);
							$('input[id^="tabel_jumlah"]').each(function() {
				              
				              var val = ($(this).attr('id'));
				              val = val.replace('tabel_jumlah', '');
				              if($("#tabel_idorderbarang"+val).val() == id_order){
					              var jum = $("#tabel_jumlah"+val).val();
					              	total += parseInt(jum);
				              }
				              
				            });

				        //    console.log(total);
							if(respon.length > 0){
								if($("[name=popup_id_tableorder]").val() == ''){
									nilai = respon[0].sisa-total;
								}else{
									nilai = (respon[0].sisa-total)+parseInt($("[name=popup_jum_terima]").val());
								}
								$("[name=popup_jum_pesan]").text(nilai+' '+respon[0].nama_satuan);
								$("[name=popup_jum_terima]").attr('min', 0);
								$("[name=popup_jum_terima]").attr('max', nilai);

								$("[name=popup_jum_terimaakhir]").attr('min', 0);
								$("[name=popup_jum_terimaakhir]").attr('max', nilai*$("[name=store_konversiorder]").val());
							}
						}
					})

					


				}
			})

		}else{
			$("[name=popup_jum_pesan]").text('');
			$("[name=popup_jum_terima]").attr('min', 0);
			$("[name=popup_jum_terima]").attr('max', 0);
			$("[name=popup_jum_terima]").val('');
		}
	})

	$("[name=popup_jum_terima]").keyup(function(){
		var jum = parseInt($("[name=popup_jum_terima]").val());
		if(isNaN(jum) || jum == 0 || jum == ''){
			jum = parseInt(0);
		}
		var nilai = parseInt($("[name=store_konversiorder]").val());

		var tot = jum*nilai;
		$("[name=popup_jum_terimaakhir]").val(tot);
	})

	$("[name=popup_satuanakhir]").keyup(function(){
		var jum = parseInt($("[name=popup_satuanakhir]").val());
		if(isNaN(jum) || jum == 0 || jum == ''){
			jum = parseInt(0);
		}
		var nilai = parseInt($("[name=store_konversiorder]").val());

		var tot = jum/nilai;
		$("[name=popup_jum_terima]").val(tot);
	})

	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);
    	}
	}

	$("#btn_popup_ambil").click(function(){
		var id_data = $("[name=popup_id_tableorder]").val();
		var id_barang = $("[name=popup_order] :selected").attr('id_barang');
		var nama_barang = $("[name=popup_order] :selected").attr('nama_barang');
		var kode = $("[name=popup_order] :selected").attr('kode_barang');
		var jumlah = $("[name=popup_jum_terima]").val();
		var jumlah_konv = $("[name=popup_jum_terimaakhir]").val();
		if(isNaN(jumlah_konv) || jumlah_konv == ''){
			jumlah_konv = jumlah;
		}

		var harga = $("[name=popup_order] :selected").attr('harga_satuan');
		var total = parseInt(jumlah)*parseInt(harga);
		var id_orderbarang = $("[name=popup_order]").val();

		if(id_orderbarang != '' && jumlah != '' && jumlah != 0){
			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_jumlah'+tb_no+'">'+jumlah+'</div>',
						'<div id="list_jumlah_konv'+tb_no+'">'+jumlah_konv+'</div>',
						'<div id="list_total'+tb_no+'">'+curr(total)+'</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_jumlah_konv[]" id="tabel_jum_konv'+tb_no+'" value="'+jumlah_konv+'">'+
						'<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_idorderbarang[]" id="tabel_idorderbarang'+tb_no+'" value="'+id_orderbarang+'">'
						]).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_jumlah_konv"+id_data).text(jumlah_konv);
				$("#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_jumlah_konv"+id_data).val(jumlah_konv);
				$("#tabel_harga"+id_data).val(harga);
				$("#tabel_total"+id_data).val(total);
				$("#tabel_idorderbarang"+id_data).val(id_orderbarang);
			}		

			tb_no++;
			get_jum();
			$("#exampleModal2").modal('hide');
		}else{

		}

	})

	function edit_tableorder(id){

		var id_orderbarang = $("#tabel_idorderbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var jum_kon = $("#tabel_jum_konv"+id).val();

		$("[name=popup_id_tableorder]").val(id);
		$("[name=popup_order]").val(id_orderbarang).trigger('change');
		$("[name=popup_jum_terima]").val(jumlah);
		$("[name=popup_jum_terimaakhir]").val(jum_kon);
		console.log(jum_kon);

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

	function get_editorder(id){
		$.ajax({
			url: "{{ url('pembelian/get_editorder')}} ",
			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_beli_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_beli_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_harga'+respon[i].id_beli_barang+'">'+curr(respon[i].harga_satuan)+'</div>',
							'<div id="list_jumlah'+respon[i].id_beli_barang+'">'+respon[i].jml_barang+'</div>',
							'<div id="list_jumlah_konv'+respon[i].id_beli_barang+'">'+respon[i].jml_barang_konv+'</div>',
							'<div id="list_total'+respon[i].id_beli_barang+'">'+curr(respon[i].total)+'</div>'+
							'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon[i].id_beli_barang+'" value="'+respon[i].id_beli_barang+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id_beli_barang+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang+'">'+
							'<input type="hidden" name="tabel_jumlah_konv[]" id="tabel_jum_konv'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang_konv+'">'+
							'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+respon[i].id_beli_barang+'" value="'+respon[i].harga_satuan+'">'+
							'<input type="hidden" name="tabel_total[]" id="tabel_total'+respon[i].id_beli_barang+'" value="'+respon[i].total+'">'+
							'<input type="hidden" name="tabel_idorderbarang[]" id="tabel_idorderbarang'+respon[i].id_beli_barang+'" value="'+respon[i].id_obb+'">'
							]).draw(false);
					}

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

	function hapus_order(d){
		table_barang.row(d.parents('tr')).remove().draw();
		get_jum();
		netto();
		
	}

</script>

@endsection