@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> Produksi</a></li>
			<li class="active">Bon Bahan Kemasan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Bon Bahan Kemasan</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('barangkeluar_kemasan/simpan') }}" method="post" autocomplete="off">
	    		<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    		<input type="hidden" name="id_bonbahan" value="{{ $data['data']['id_barangkeluar_kemasan'] }}">
	    		<div class="col-sm-6">
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Tanggal Pengambilan</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="tanggal_ambil" class="form-control datepicker" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):$data['data']['tanggal'] }}" required>
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Proses Produksi</label>
	    				<div class="col-sm-8"> @php $proses = $data['data']['id_proses']; @endphp

	    					@php 
	    					$disabled = ''; $input = '';
	    					if($data['data']['id_barangkeluar_kemasan'] != ''){
	    						$disabled = 'disabled';
	    						$input = '<input type="hidden" name="proses_produksi" value="'.$proses.'">';
	    					}
	    					@endphp
	    					<select class="form-control select-search" name="proses_produksi" required {{$disabled}}>
	    						@php
	    						foreach($data['proses'] as $p){ @endphp
	    						<option value="{{ $p->id_proses }}" {{ ($proses == $p->id_proses) ? "selected":"" }}>{{ $p->nama_proses }}</option>
	    						@php
	    						}
	    						@endphp
	    					</select>
	    					@php echo $input; @endphp
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Tanggal Perkiraan Selesai</label>
	    				<div class="col-sm-8">
	    					<input type="text" name="tanggal_selesai" class="form-control datepicker" value="{{ ($data['data']['tanggal_perkiraan'] == '') ? date('d-m-Y'):$data['data']['tanggal_perkiraan'] }}" required>
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Jenis Produksi</label>
	    				<div class="col-sm-8"> @php $lokasi = $data['data']['id_lokasi']; @endphp

	    					@php 
	    					$disabled = ''; $input = '';
	    					if($data['data']['id_barangkeluar_kemasan'] != ''){
	    						$disabled = 'disabled';
	    						$input = '<input type="hidden" name="id_lokasi" value="'.$lokasi.'">';
	    					}
	    					@endphp
	    					<select class="form-control select-search" name="id_lokasi" required {{$disabled}}>
	    						@php
	    						foreach($data['lokasi'] as $l){ @endphp
	    						<option value="{{ $l->id_lokasi }}" {{ ($lokasi == $l->id_lokasi) ? "selected":"" }}>{{ $l->nama_lokasi }}</option>
	    						@php
	    						}
	    						@endphp
	    					</select>
	    					@php echo $input; @endphp
	    				</div>
	    			</div>
	    			
	    		</div>
	    		<div class="col-sm-6">
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Pegawai</label>
	    				<div class="col-sm-8"> @php $peg = $data['data']['id_pegawai']; @endphp

	    					@php 
	    					$disabled = ''; $input = '';
	    					if($data['data']['id_barangkeluar_kemasan'] != ''){
	    						$disabled = 'disabled';
	    						$input = '<input type="hidden" name="pegawai" value="'.$peg.'">';
	    					}
	    					@endphp
	    					<select class="form-control select-search" name="pegawai" {{$disabled}} required>
	    						<option value="">-- Pilih --</option>
	    						@php
	    						foreach($data['pegawai'] as $p){ @endphp
	    						<option value="{{ $p->id_pegawai }}" {{ ($peg == $p->id_pegawai) ? "selected":""  }}>{{ $p->nama_pegawai }}</option>
	    						@php
	    						}
	    						@endphp
	    					</select>
	    					@php echo $input; @endphp
	    				</div>
	    			</div>
	    			<div class="form-group">
	    				<label class="control-label col-sm-4">Rekanan</label>
	    				<div class="col-sm-8"> @php $rekanan = $data['data']['id_rekanan']; @endphp
	    					@php 
	    					$disabled = ''; $input = '';
	    					if($data['data']['id_barangkeluar_kemasan'] != ''){
	    						$disabled = 'disabled';
	    						$input = '<input type="hidden" name="rekanan" value="'.$rekanan.'">';
	    					}
	    					@endphp

	    					<select class="form-control select-search" name="rekanan" {{$disabled}} required>
	    						<option value="">-- Pilih --</option>
	    						@php
	    						foreach($data['rekanan'] as $r){ @endphp
	    						<option value="{{ $r->id_rekanan }}" {{($rekanan == $r->id_rekanan) ? "selected":"" }} >{{ $r->nama.' - '.$r->nama_kelompok }}</option>
	    						@php
	    						}
	    						@endphp
	    					</select>

	    					@php echo $input; @endphp
	    				</div>
	    			</div>
	    		</div>
	    		<div class="col-sm-12">
	    			<div class="panel panel-primary">
                
                	</div>
                	<a href="#" class="btn btn-success" style="margin-bottom: 1%;" id="btn_tambah_item">Tambah Barang</a>
	    			<div class="table-responsive">
		            <table class="table datatable-basic table-stripped" id="table_barang">
			    	<thead>
						<tr>
								<th>No</th>
								<th>Aksi</th>
								<th>No. Workorder</th>
								<th>Kode Barang</th>
								<th>Nama Barang</th>
								<th>Proses</th>
								<th>Warna</th>
								<th>Gudang</th>
								<th>Jumlah</th>
								<th>Kualitas Proses</th>
								<th>Corak</th>
								<th>Motif</th>
								<th>Kualitas</th>
								<th>Dudulan</th>
								<th>Kualitas Periksa</th>
								<th>Label</th>
								<th>Cap</th>
								<th>Kemasan</th>
								<th>Kemasan Satuan</th>
								<th>Kemasan 10 Potong</th>
								<th>Kemasan Dus Ball</th>
								<th class="text-center">Aksi</th>
						</tr>
					</thead>
					<tbody>
						
					</tbody>
			    </table>
				</div>
	    	</div>
	    	<div class="col-sm-12">
	    		
             	<button class="btn btn-primary pull-right" type="submit">Simpan</button>
             </div>
         </div>
	    	</form>
	    	
	    </div>

	</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">
	      			<input type="hidden" name="popup_list">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Proses Sebelumnya</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses">
	      						<option value="">-- Pilih --</option>
	      						@php
	      						foreach($data['proses_sebelum'] as $p){ @endphp
	      						<option value="{{ $p->id_proses }}">{{ $p->nama_proses }}</option>
	      						@php
	      						}
	      						@endphp
	      					</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_kode">
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah Stok</label>
	      				<div class="col-sm-8">
	      					<label class="control-label" name="popup_stok"></label>
	      				</div>
	      			</div>  			
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlah" class="form-control number-only" onkeyup="maksimal($(this))">
	      				</div>
	      			</div>
	      			<div class="form-group" id="div_menghasilkan">
                        <label class="control-label col-sm-4">Menghasilkan ( <b id="satuan_menghasilkan"></b> )</label>
                        <div class="col-sm-8">
                            <input type="text" name="popup_menghasilkan" 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_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<input type="hidden" name="store_rubahsatuan">


<!----Modal---->
<script type="text/javascript">
	$("#btn_tambah_item").click(function(){
		$("[name=popup_id]").val('');
		$("[name=popup_list]").val('');
		$("[name=popup_proses]").val(null).trigger('change');
		$("[name=popup_kode]").val(null).trigger('change');
		$("[name=popup_jumlah]").val('');
		$("[name=popup_stok]").text('');
		$("[name=popup_menghasilkan]").val('');
		$("#satuan_menghasilkan").text('');

		$("#exampleModal").modal('show');
	})
	var id;
	var table_barang;
	var store_bonbahan;
	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();

        $("#div_menghasilkan").hide();
        $("#satuan_menghasilkan").text('');
	})

	store_bonbahan = $("[name=id_bonbahan]").val();
	if(store_bonbahan != ''){
		console.log(store_bonbahan);
		get_edit(store_bonbahan);
	}

	$("[name=pegawai]").change(function(){
		if($(this).val() != ''){
			disabled('[name=rekanan]', true);
			$('[name=rekanan]').attr('required', false);
		}else{
			disabled('[name=rekanan]', false);
			$('[name=rekanan]').attr('required', true);
		}
	})

	$("[name=rekanan]").change(function(){
		if($(this).val() != ''){
			disabled('[name=pegawai]', true);
			$('[name=pegawai]').attr('required', false);
		}else{
			disabled('[name=pegawai]', false);
			$('[name=pegawai]').attr('required', true);
		}
	})

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

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

    $("[name=popup_menghasilkan]").keyup(function(){
        var jum = parseFloat($("[name=popup_menghasilkan]").val());
        if(isNaN(jum) || jum == 0 || jum == ''){
            jum = parseFloat(0);
        }
        var nilai = parseFloat($("[name=store_rubahsatuan]").val());

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

    function popup_jumlah(id){
    	var jum = parseFloat($("#tabel_jumlah"+id).val());
        if(isNaN(jum) || jum == 0 || jum == ''){
            jum = parseFloat(0);
        }
        var nilai = parseFloat($("[name=store_rubahsatuan]").val());

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


	$("[name=popup_proses]").change(function(){
		var id_proses = $("[name=popup_proses] :selected").val();

		$.ajax({
			url: "{{ url('barangkeluar_kemasan/get_barang_proses') }}",
			type: 'post',
			dataType: 'json',
			data: { id_proses : id_proses},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				$("[name=popup_kode]").html('');
				$("[name=popup_stok]").text('');
				if(respon.length > 0){
					$("[name=popup_kode]").append('<option value="">-- Pilih Barang --</option>');
					for(i in respon){
						if(respon[i].stok > 0){
								var id_gudang ='';
								var nama_gudang = ''; 
								var name_gudang= '';
								var id_komposisi = '';
								var nama_komposisi = '';
								var name_komposisi = '';
								var id_corak = ''; 
								var nama_corak = '';
								var name_corak = '';
								var id_motif = '';
								var nama_motif = '';
								var name_motif = '';
                                var id_kual = '';
                                var nama_kual = '';
                                var name_kual = '';
                                var id_dudul = '';
                                var nama_dudul = '';
                                var name_dudul = '';
                                var id_label = '';
                                var nama_label = '';
                                var name_label = '';
                                var id_kemasan = '';
                                var nama_kemasan = '';
                                var name_kemasan = '';
                                var id_kemasansatuan = '';
                                var nama_kemasansatuan = '';
                                var name_kemasansatuan = '';
                                var id_kemasansepuluh = '';
                                var nama_kemasansepuluh = '';
                                var name_kemasansepuluh = '';
                                var id_kemasanseratus = '';
                                var nama_kemasanseratus = '';
                                var name_kemasanseratus = '';
                                var id_proses = '';
                                var nama_proses = '';
                                var name_proses = '';
                                var spk = '';
                                var no_spk = '';
                                var nomor_spk = '';
                                var workorder = '';
                                var no_workorder = '';
                                var nomor_workorder = '';
                                var id_cap = '', nama_cap = '', cap = '', id_kual_proses = '', nama_kual_proses = '', kual_proses = '', id_kual_periksa = '', nama_kual_periksa = '', kual_periksa = '', id_rencana = '', nama_rencana = '', jumlah_rencana = '', nama_satuanrencana = '' ;

							if(respon[i].id_gudang != ''){
								id_gudang = respon[i].id_gudang;
								nama_gudang = ' - '+respon[i].nama_gudang;
								name_gudang = respon[i].nama_gudang;
							}
							if(respon[i].id_komposisi != ''){
								id_komposisi = respon[i].id_komposisi;
								nama_komposisi = ' - '+respon[i].nama_komposisi;
								name_komposisi = respon[i].nama_komposisi;
							}
							if(respon[i].id_corak != ''){
								id_corak = respon[i].id_corak; 
								nama_corak = ' - '+respon[i].nama_corak;
								name_corak = respon[i].nama_corak; 
							}
							if(respon[i].id_motif != ''){
								id_motif = respon[i].id_motif; 
								nama_motif = ' - '+respon[i].nama_motif;
								name_motif = respon[i].nama_motif;
							}
                            if(respon[i].id_kualitas != ''){
                                id_kual = respon[i].id_kualitas; 
                                nama_kual = ' - '+respon[i].nama_kualitas; 
                                name_kual = respon[i].nama_kualitas;
                            }
                            if(respon[i].id_kualitas_dudul != ''){
                                id_dudul = respon[i].id_kualitas_dudul;
                                nama_dudul = ' - Dudulan '+respon[i].kualitas_dudul;
                                name_dudul = respon[i].kualitas_dudul;
                            }

                            if(respon[i].id_label !=''){
                                id_label = respon[i].id_label;
                                nama_label = ' - '+respon[i].nama_label;
                                name_label = respon[i].nama_label;
                            }

                            if(respon[i].id_kemasan != ''){
                                id_kemasan = respon[i].id_kemasan;
                                nama_kemasan = ' - '+respon[i].nama_kemasan;
                                name_kemasan = respon[i].nama_kemasan
                            }

                            if(respon[i].id_kemasansatuan != ''){
                                id_kemasansatuan = respon[i].id_kemasansatuan;
                                nama_kemasansatuan = ' - '+respon[i].nama_kemasansatuan;
                                name_kemasansatuan = respon[i].nama_kemasansatuan;
                            }

                            if(respon[i].id_kemasansepuluh != ''){
                                id_kemasansepuluh = respon[i].id_kemasansepuluh;
                                nama_kemasansepuluh = ' - '+respon[i].nama_kemasansepuluh;
                                name_kemasansepuluh = respon[i].nama_kemasansepuluh;
                            }

                            if(respon[i].id_kemasanseratus != ''){
                                id_kemasanseratus = respon[i].id_kemasanseratus;
                                nama_kemasanseratus = ' - '+respon[i].nama_kemasanseratus;
                                name_kemasanseratus = respon[i].nama_kemasanseratus;
                            }

                            if(respon[i].id_proses != ''){
                            	id_proses = respon[i].id_proses;
                            	nama_proses = ' - '+respon[i].nama_proses;
                            	name_proses = respon[i].nama_proses;
                            }

                            if(respon[i].id_workorder != ''){
                            	spk = respon[i].id_workorder;
                            	no_spk = ' - '+respon[i].no_workorder;
                            	nomor_spk = respon[i].no_workorder;
                            }

                            if(respon[i].id_workorder_barang != ''){
                            	workorder = respon[i].id_workorder_barang;
                            	no_workorder = ' - '+respon[i].no_workorder_barang;
                            	nomor_workorder = respon[i].no_workorder_barang;
                            }

                            if(respon[i].id_cap != ''){
                            	id_cap = respon[i].id_cap;
                            	nama_cap = ' - '+respon[i].nama_cap;
                            	cap = respon[i].nama_cap;
                            }

                            if(respon[i].id_kualitas_proses != ''){
                            	id_kual_proses = respon[i].id_kualitas_proses;
                            	nama_kual_proses = ' - Proses '+respon[i].nama_kualitas_proses;
                            	kual_proses = respon[i].nama_kualitas_proses;
                            }

                            if(respon[i].id_kualitas_periksa != ''){
                            	id_kual_periksa = respon[i].id_kualitas_periksa;
                            	nama_kual_periksa = ' - Periksa '+respon[i].nama_kualitas_periksa;
                            	kual_periksa = respon[i].nama_kualitas_periksa;
                            }

                            if(respon[i].id_perubahansatuan != ''){
                            	id_rencana = respon[i].id_perubahansatuan;
                            	nama_rencana = ' - Untuk '+respon[i].jumlah_rencana+' '+respon[i].nama_perubahansatuan;
                            	nama_satuanrencana = respon[i].nama_perubahansatuan;
                            	jumlah_rencana = respon[i].jumlah_rencana;
                            }

                            $("[name=popup_kode]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].id_satuan+'" nama_satuan="'+respon[i].nama_satuan+'" gudang="'+id_gudang+'" nama_gudang="'+name_gudang+'" komposisi="'+id_komposisi+'" nama_komposisi="'+name_komposisi+'" corak="'+id_corak+'" nama_corak="'+name_corak+'" motif="'+id_motif+'" nama_motif="'+name_motif+'" kualitas="'+id_kual+'" nama_kualitas="'+name_kual+'" dudulan="'+id_dudul+'" nama_dudulan="'+name_dudul+'" label="'+id_label+'" nama_label="'+name_label+'" kemasan="'+id_kemasan+'" nama_kemasan="'+name_kemasan+'" kemasan_satuan="'+id_kemasansatuan+'" nama_kemasansatuan="'+name_kemasansatuan+'" kemasan_sepuluh="'+id_kemasansepuluh+'" nama_kemasansepuluh="'+name_kemasansepuluh+'" kemasan_seratus="'+id_kemasanseratus+'" nama_kemasanseratus="'+name_kemasanseratus+'" proses="'+id_proses+'" nama_proses="'+name_proses+'" spk="'+spk+'" no_spk="'+nomor_spk+'" workorder="'+workorder+'" no_workorder="'+nomor_workorder+'" id_cap="'+id_cap+'" cap="'+cap+'" id_kual_proses="'+id_kual_proses+'" kual_proses="'+kual_proses+'" id_kual_periksa="'+id_kual_periksa+'" kual_periksa="'+kual_periksa+'" rencana="'+id_rencana+'" jumlah_rencana="'+jumlah_rencana+'" nama_satuanrencana="'+nama_satuanrencana+'">'+respon[i].sub_kategori+' - '+respon[i].kategori+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+nama_gudang+nama_komposisi+nama_kual_proses+nama_corak+nama_motif+nama_kual+nama_dudul+nama_kual_periksa+nama_label+nama_cap+nama_kemasan+nama_kemasansatuan+nama_kemasansepuluh+nama_kemasanseratus+no_workorder+nama_proses+nama_rencana+'</option>');
						}
					}
				}else{
					$("[name=popup_kode]").html('');
					$("[name=popup_kode]").append('<option value=""> Stok Kosong </option>');
					$("[name_popup_stok]").text('');		
				}

				$("[name=popup_menghasilkan]").val('');
				$("#satuan_menghasilkan").text('');
			}
		})
		
	})

	$("[name=popup_kode]").change(function(){
		
		var id_tabel = $("[name=popup_id]").val();
		var id_gudang =$("[name=popup_kode] :selected").attr('gudang');
		var id_barang = $(this).val();
		var id_corak = $("[name=popup_kode] :selected").attr('corak');
		var id_satuan = $("[name=popup_kode] :selected").attr('satuan');
		var nilai = parseInt(0);
		var id_komposisi = $("[name=popup_kode] :selected").attr('komposisi');
        var id_kualitas = $("[name=popup_kode] :selected").attr('kualitas');
        var id_dudul = $("[name=popup_kode] :selected").attr('dudulan');
        var id_motif = $("[name=popup_kode] :selected").attr('motif');
        var id_label = $("[name=popup_kode] :selected").attr('label');
        var id_kemasan = $("[name=popup_kode] :selected").attr('kemasan');
        var id_kemasansatuan = $("[name=popup_kode] :selected").attr('kemasan_satuan');
        var id_kemasansepuluh = $("[name=popup_kode] :selected").attr('kemasan_sepuluh');
        var id_kemasanseratus = $("[name=popup_kode] :selected").attr('kemasan_seratus');
        var id_proses = $("[name=popup_kode] :selected").attr('proses');
        var spk = $("[name=popup_kode] :selected").attr('spk');
        var workorder = $("[name=popup_kode] :selected").attr('workorder');
        var id_cap = $("[name=popup_kode] :selected").attr('id_cap');
        var id_kual_proses = $("[name=popup_kode] :selected").attr('id_kual_proses');
        var id_kual_periksa = $("[name=popup_kode] :selected").attr('id_kual_periksa');
        var id_rencana = $("[name=popup_kode] :selected").attr('rencana');
        var jumlah_rencana = $("[name=popup_kode] :selected").attr('jumlah_rencana');

        $("[name=popup_jumlah]").val('');
		$("[name=popup_stok]").text('');
		$.ajax({
			url: '{{ url('barangkeluar_kemasan/get_barang') }}',
			type: 'post',
			dataType: 'json',
			data: {id : id_gudang, id_barang : id_barang, id_komposisi:id_komposisi,id_corak:id_corak, id_kualitas:id_kualitas, id_dudul:id_dudul, id_motif:id_motif, id_label:id_label, id_kemasan:id_kemasan, id_kemasansatuan:id_kemasansatuan, id_kemasansepuluh:id_kemasansepuluh, id_kemasanseratus:id_kemasanseratus, id_proses:id_proses, spk:spk, workorder:workorder, id_cap : id_cap, id_kualitasproses : id_kual_proses, id_kualitasperiksa : id_kual_periksa, id_rencana : id_rencana, jumlah_rencana : jumlah_rencana},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				var total = parseFloat(0);
						$('input[id^="tabel_jumlah"]').each(function() {
				              
				        var val = ($(this).attr('id'));
				        val = val.replace('tabel_jumlah', '');

				        

				        if($("#tabel_idbarang"+val).val() == id_barang && $("#tabel_idwarna"+val).val() == id_komposisi && $("#tabel_idcorak"+val).val() == id_corak && $("#tabel_idkualitas"+val).val()  == id_kualitas && $("#tabel_idkualitas_dudul"+val).val() == id_dudul && $("#tabel_idmotif"+val).val() == id_motif && $("#tabel_idlabel"+val).val() == id_label && $("#tabel_idkemasan"+val).val() == id_kemasan && $("#tabel_idkemasan_satuan"+val).val() == id_kemasansatuan && $("#tabel_idkemasan_sepuluh"+val).val() == id_kemasansepuluh && $("#tabel_idkemasan_seratus"+val).val() == id_kemasanseratus && $("#tabel_spk"+val).val() == spk && $("#tabel_workorder"+val).val() == workorder && $("#tabel_idproses"+val).val() == id_proses && $("#tabel_idgudang"+val).val() == id_gudang && $("#tabel_idcap"+val).val() == id_cap && $("#tabel_idkualitas_periksa"+val).val() == id_kual_periksa && $("#tabel_idkualitas_proses"+val).val() == id_kual_proses && $("#tabel_idsatuan"+val).val() == id_satuan && $("#tabel_idrencana"+val).val() == id_rencana && $("#tabel_rencanajumlah"+val).val() == jumlah_rencana){
						    
						      	var jum = $("#tabel_jumlah"+val).val();
						        total += parseFloat(jum);
						    }
                    	 
				            
				        });

				        
				        
						if(respon.length > 0){
							if($("[name=popup_list]").val() == ''){
								nilai = parseFloat(respon[0].stok)-total;
							//	console.log("stok "+respon[0].stok+"\nTotal "+total);
							}else{
								/*nilai = (respon[0].stok-total)+parseFloat($("[name=popup_jumlah]").val());*/
								var jum = 0;
								if($("[name=popup_jumlah]").val() != ''){
									jum = $("[name=popup_jumlah]").val();
								}
								if(id_tabel == ''){
									
									nilai = (parseFloat(respon[0].stok)-total)+parseFloat(jum);
								}else{
									
									nilai = parseFloat(respon[0].stok)+(parseFloat(jum));	
								}

								console.log("stok "+respon[0].stok+"\nTotal "+total+"\nJumlah "+jum);
							}
							$("[name=popup_stok]").text(nilai+' '+respon[0].nama_satuan);
							$("[name=popup_jumlah]").attr('min', 0);
							$("[name=popup_jumlah]").attr('max', nilai);

							if(respon[0].id_perubahansatuan != ''){
	                            $("#div_menghasilkan").show();
	                            $("#satuan_menghasilkan").text(respon[0].nama_perubahansatuan);
	                            $("[name=store_rubahsatuan]").val(respon[0].jumlah_rencana);

	                            $("[name=popup_menghasilkan]").attr('min', 0);
	                            if(respon[0].jumlah_rencana < 1){
	                            	$("[name=popup_menghasilkan]").attr('max', (1/(parseFloat(respon[0].jumlah_rencana))/respon[0].stok ) );	
	                            }else{
	                            	$("[name=popup_menghasilkan]").attr('max', respon[0].jumlah_rencana*respon[0].stok);
	                            }
	                            

	                            if($("[name=popup_list]").val() != ''){
	                            	
	                            	popup_jumlah($("[name=popup_list]").val());
	                            }
	                        }else{
	                        	$("[name=popup_menghasilkan]").val('');
								$("#satuan_menghasilkan").text('');
	                            $("#div_menghasilkan").hide();
	                        }

						}
			}
		})
	})

	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id]").val();
		var id_list = $("[name=popup_list]").val();
		var id_barang = $("[name=popup_kode]").val();
		var nama_barang = $("[name=popup_kode] :selected").attr('barang');
		var kode_barang = $("[name=popup_kode] :selected").attr('kode');
		var id_komposisi = $("[name=popup_kode] :selected").attr('komposisi');
		var nama_komposisi = $("[name=popup_kode] :selected").attr('nama_komposisi'); 
        var id_kualitas = $("[name=popup_kode] :selected").attr('kualitas');
        var nama_kualitas = $("[name=popup_kode] :selected").attr('nama_kualitas');
        var id_dudul = $("[name=popup_kode] :selected").attr('dudulan');
        var nama_dudul = $("[name=popup_kode] :selected").attr('nama_dudulan');
        var id_corak = $("[name=popup_kode] :selected").attr('corak');
        var nama_corak = $("[name=popup_kode] :selected").attr('nama_corak');
        var id_motif = $("[name=popup_kode] :selected").attr('motif');
        var nama_motif = $("[name=popup_kode] :selected").attr('nama_motif');
        var id_label = $("[name=popup_kode] :selected").attr('label');
        var nama_label = $("[name=popup_kode] :selected").attr('nama_label');
        var id_kemasan = $("[name=popup_kode] :selected").attr('kemasan');
        var nama_kemasan = $("[name=popup_kode] :selected").attr('nama_kemasan');
        var id_kemasansatuan = $("[name=popup_kode] :selected").attr('kemasan_satuan');
        var nama_kemasansatuan = $("[name=popup_kode] :selected").attr('nama_kemasansatuan');
        var id_kemasansepuluh = $("[name=popup_kode] :selected").attr('kemasan_sepuluh');
        var nama_kemasansepuluh = $("[name=popup_kode] :selected").attr('nama_kemasansepuluh');
        var id_kemasanseratus = $("[name=popup_kode] :selected").attr('kemasan_seratus');
        var nama_kemasanseratus = $("[name=popup_kode] :selected").attr('nama_kemasanseratus');
        var id_proses = $("[name=popup_kode] :selected").attr('proses');
        var nama_proses = $("[name=popup_kode] :selected").attr('nama_proses');
        var spk = $("[name=popup_kode] :selected").attr('spk');
        var no_spk = $("[name=popup_kode] :selected").attr('no_spk');
        var workorder = $("[name=popup_kode] :selected").attr('workorder');
        var no_workorder = $("[name=popup_kode] :selected").attr('no_workorder');
        var id_gudang = $("[name=popup_kode] :selected").attr('gudang');
        var nama_gudang = $("[name=popup_kode] :selected").attr('nama_gudang');
		var jumlah = $("[name=popup_jumlah]").val();
		var satuan = $("[name=popup_kode] :selected").attr('satuan');
		var nama_satuan = $("[name=popup_kode] :selected").attr('nama_satuan');
		var id_cap = $("[name=popup_kode] :selected").attr('id_cap');
		var nama_cap = $("[name=popup_kode] :selected").attr('cap');
		var id_kual_proses = $("[name=popup_kode] :selected").attr('id_kual_proses');
		var kual_proses = $("[name=popup_kode] :selected").attr('kual_proses');
		var id_kual_periksa = $("[name=popup_kode] :selected").attr('id_kual_periksa');
		var kual_periksa = $("[name=popup_kode] :selected").attr('kual_periksa');
		var id_rencana = $("[name=popup_kode] :selected").attr('rencana');
		var nama_satuanrencana = $("[name=popup_kode] :selected").attr('nama_satuanrencana');
		var jumlah_rencana = $("[name=popup_kode] :selected").attr('jumlah_rencana');

		var total = parseInt(jumlah);
		if(id_barang != '' && jumlah != '0'){
			if(id_list == ''){
				table_barang.row.add(['<div><center></center></div>',
					'<div class="btn-group"><button type="button" class="btn bg-warning-400" onclick="edit_table('+tb_no+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>',
					'<div id="list_workorder'+tb_no+'">'+no_workorder+'</div>',
					'<div id="list_kode'+tb_no+'">'+kode_barang+'</div>',
					'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
					'<div id="list_proses'+tb_no+'">'+nama_proses+'</div>',
					'<div id="list_warna'+tb_no+'">'+nama_komposisi+'</div>',
					'<div id="list_gudang'+tb_no+'">'+nama_gudang+'</div>',
					'<div id="list_jumlah'+tb_no+'">'+jumlah+'</div>',
					'<div id="list_kualitas_proses'+tb_no+'">'+kual_proses+'</div>',
					'<div id="list_corak'+tb_no+'">'+nama_corak+'</div>',
					'<div id="list_motif'+tb_no+'">'+nama_motif+'</div>',
					'<div id="list_kualitas'+tb_no+'">'+nama_kualitas+'</div>',
					'<div id="list_dudulan'+tb_no+'">'+nama_dudul+'</div>',
					'<div id="list_kualitas_periksa'+tb_no+'">'+kual_periksa+'</div>',
					'<div id="list_label'+tb_no+'">'+nama_label+'</div>',
					'<div id="list_cap'+tb_no+'">'+nama_cap+'</div>',
					'<div id="list_kemasan'+tb_no+'">'+nama_kemasan+'</div>',
					'<div id="list_kemasansatuan'+tb_no+'">'+nama_kemasansatuan+'</div>',
					'<div id="list_kemasansepuluh'+tb_no+'">'+nama_kemasansepuluh+'</div>',
					'<div id="list_kemasanseratus'+tb_no+'">'+nama_kemasanseratus+'</div>',
					'<div><button type="button" class="btn bg-warning-400" onclick="edit_table('+tb_no+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>'+
					'<input type="hidden" id="tabel_id'+tb_no+'" name="tabel_id[]" value="">'+
					'<input type="hidden" id="tabel_idsatuan'+tb_no+'" name="tabel_idsatuan[]" value="'+satuan+'">'+
					'<input type="hidden" id="tabel_idbarang'+tb_no+'" name="tabel_kode[]" value="'+id_barang+'">'+
					'<input type="hidden" id="tabel_namabarang'+tb_no+'" name="tabel_nama[]" value="'+nama_barang+'">'+
					'<input type="hidden" id="tabel_idproses'+tb_no+'" name="tabel_idproses[]" value="'+id_proses+'">'+
					'<input type="hidden" id="tabel_idwarna'+tb_no+'" name="tabel_idwarna[]" value="'+id_komposisi+'">'+
					'<input type="hidden" id="tabel_idgudang'+tb_no+'" name="tabel_idgudang[]" value="'+id_gudang+'">'+
					'<input type="hidden" id="tabel_idkualitas_proses'+tb_no+'" name="tabel_idkualitas_proses[]" value="'+id_kual_proses+'">'+
					'<input type="hidden" id="tabel_idcorak'+tb_no+'" name="tabel_idcorak[]" value="'+id_corak+'">'+
					'<input type="hidden" id="tabel_idmotif'+tb_no+'" name="tabel_idmotif[]" value="'+id_motif+'">'+
					'<input type="hidden" id="tabel_idkualitas'+tb_no+'" name="tabel_idkualitas[]" value="'+id_kualitas+'">'+
					'<input type="hidden" id="tabel_idkualitas_dudul'+tb_no+'" name="tabel_idkualitas_dudul[]" value="'+id_dudul+'">'+
					'<input type="hidden" id="tabel_idkualitas_periksa'+tb_no+'" name="tabel_idkualitas_periksa[]" value="'+id_kual_periksa+'">'+
					'<input type="hidden" id="tabel_idlabel'+tb_no+'" name="tabel_idlabel[]" value="'+id_label+'">'+
					'<input type="hidden" id="tabel_idcap'+tb_no+'" name="tabel_idcap[]" value="'+id_cap+'">'+
					'<input type="hidden" id="tabel_idkemasan'+tb_no+'" name="tabel_idkemasan[]" value="'+id_kemasan+'">'+
					'<input type="hidden" id="tabel_idkemasan_satuan'+tb_no+'" name="tabel_idkemasan_satuan[]" value="'+id_kemasansatuan+'">'+
					'<input type="hidden" id="tabel_idkemasan_sepuluh'+tb_no+'" name="tabel_idkemasan_sepuluh[]" value="'+id_kemasansepuluh+'">'+
					'<input type="hidden" id="tabel_idkemasan_seratus'+tb_no+'" name="tabel_idkemasan_seratus[]" value="'+id_kemasanseratus+'">'+
					'<input type="hidden" id="tabel_spk'+tb_no+'" name="tabel_spk[]" value="'+spk+'">'+
					'<input type="hidden" id="tabel_workorder'+tb_no+'" name="tabel_workorder[]" value="'+workorder+'">'+
					'<input type="hidden" id="tabel_jumlah'+tb_no+'" name="tabel_jumlah[]" value="'+jumlah+'">'+
					'<input type="hidden" id="tabel_total'+tb_no+'" name="tabel_total[]" value="'+total+'">'+
					'<input type="hidden" id="tabel_idrencana'+tb_no+'" name="tabel_idrencana[]" value="'+id_rencana+'">'+
					'<input type="hidden" id="tabel_rencanajumlah'+tb_no+'" name="tabel_rencanajumlah[]" value="'+jumlah_rencana+'">'
					]).draw(false);
			}else{
				// $("#list_spk"+id_list).text(no_spk);
				$("#list_workorder"+id_list).text(no_workorder);
				$("#list_kode"+id_list).text(kode_barang);
				$("#list_nama"+id_list).text(nama_barang);
				$("#list_proses"+id_list).text(nama_proses);
				$("#list_warna"+id_list).text(nama_komposisi);
				$("#list_gudang"+id_list).text(nama_gudang);
				$("#list_corak"+id_list).text(nama_corak);
				$("#list_motif"+id_list).text(nama_motif);
				$("#list_kualitas"+id_list).text(nama_kualitas);
				$("#list_dudulan"+id_list).text(nama_dudul);
				$("#list_label"+id_list).text(nama_label);
				$("#list_cap"+id_list).text(nama_cap);
				$("#list_kemasan"+id_list).text(nama_kemasan);
				$("#list_kemasansatuan"+id_list).text(nama_kemasansatuan);
				$("#list_kemasansepuluh"+id_list).text(nama_kemasansepuluh);
				$("#list_kemasanseratus"+id_list).text(nama_kemasanseratus);
				$("#list_jumlah"+id_list).text(jumlah);
				$("#list_kualitas_proses"+id_list).text(kual_proses);
				$("#list_kualitas_periksa"+id_list).text(kual_periksa);

				$("#tabel_id"+id_list).val(id_list);
				$("#tabel_idsatuan"+id_list).val(satuan);
				$("#tabel_idbarang"+id_list).val(id_barang);
				$("#tabel_namabarang"+id_list).val(nama_barang);				
				$("#tabel_idproses"+id_list).val(id_proses);
				$("#tabel_idwarna"+id_list).val(id_komposisi);
				$("#tabel_idgudang"+id_list).val(id_gudang);
				$("#tabel_idcorak"+id_list).val(id_corak);
				$("#tabel_idmotif"+id_list).val(id_motif);
				$("#tabel_idkualitas"+id_list).val(id_kualitas);
				$("#tabel_idkualitas_dudul"+id_list).val(id_dudul);
				$("#tabel_idlabel"+id_list).val(id_label);
				$("#tabel_idcap"+id_list).val(id_cap);
				$("#tabel_idkemasan"+id_list).val(id_kemasan);
				$("#tabel_idkemasan_satuan"+id_list).val(id_kemasansatuan);
				$("#tabel_idkemasan_sepuluh"+id_list).val(id_kemasansepuluh);
				$("#tabel_idkemasan_seratus"+id_list).val(id_kemasanseratus);
				$("#tabel_spk"+id_list).val(spk);
				$("#tabel_workorder"+id_list).val(workorder);
				$("#tabel_jumlah"+id_list).val(jumlah);
				$("#tabel_total"+id_list).val(total);
				$("#tabel_idkualitas_proses"+id_list).val(id_kual_proses);
				$("#tabel_idkualitas_periksa"+id_list).val(id_kual_periksa);
				$("#tabel_idrencana"+id_list).val(id_rencana);
				$("#tabel_rencanajumlah"+id_list).val(jumlah_rencana);
			}
		
		$("#exampleModal").modal('hide');
		tb_no++;
		}
	//	console.log(id_barang+"\n"+nama_barang+"\n"+jumlah+"\n"+harga);
	})

	function edit_table(id){

		var id_table = $("#tabel_id"+id).val();
		var id_barang = $("#tabel_idbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var id_gudang = $("#tabel_idgudang"+id).val();

		var id_corak = $("#tabel_idcorak"+id).val()
		var id_komposisi = $("#tabel_idwarna"+id).val();
        var id_kualitas = $("#tabel_idkualitas"+id).val();
        var id_dudul = $("#tabel_idkualitas_dudul"+id).val();
        var id_motif = $("#tabel_idmotif"+id).val();
        var id_label = $("#tabel_idlabel"+id).val();
        var id_kemasan = $("#tabel_idkemasan"+id).val();
        var id_kemasansatuan = $("#tabel_idkemasan_satuan"+id).val();
        var id_kemasansepuluh = $("#tabel_idkemasan_sepuluh"+id).val();
        var id_kemasanseratus = $("#tabel_idkemasan_seratus"+id).val();
        var workorder = $("#tabel_spk"+id).val();
        var workorder_barang = $("#tabel_workorder"+id).val();
        var id_proses = $("#tabel_idproses"+id).val();
        var id_satuan = $("#tabel_idsatuan"+id).val();
        var id_cap = $("#tabel_idcap"+id).val();
        var id_kualitas_proses = $("#tabel_idkualitas_proses"+id).val();
        var id_kualitas_periksa = $("#tabel_idkualitas_periksa"+id).val();
        var id_rencana = $("#tabel_idrencana"+id).val();
        var jumlah_rencana = $("#tabel_rencanajumlah"+id).val();

		
		$("[name=popup_id]").val(id_table);
		$("[name=popup_list]").val(id);

	

		var FunctionOne = function () {
		  var r = $.Deferred();
		  $("[name=popup_proses]").val(id_proses).trigger('change');

		  if(store_bonbahan != '' && id_table != ''){
		  	$.ajax({
				url: '{{ url('barangkeluar_kemasan/get_barang') }}',
				type: 'post',
				dataType: 'json',
				data: {id : id_gudang, id_barang : id_barang, id_komposisi:id_komposisi,id_corak:id_corak, id_kualitas:id_kualitas, id_dudul:id_dudul, id_motif:id_motif, id_label:id_label, id_kemasan:id_kemasan, id_kemasansatuan:id_kemasansatuan, id_kemasansepuluh:id_kemasansepuluh, id_kemasanseratus:id_kemasanseratus, id_proses:id_proses, spk:workorder, workorder:workorder_barang, id_cap : id_cap, id_kualitasproses : id_kualitas_proses, id_kualitasperiksa : id_kualitas_periksa, id_rencana : id_rencana, jumlah_rencana : jumlah_rencana},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					var jum = $("[name=popup_kode]").find("option[komposisi='" + id_komposisi + "'][corak='" + id_corak + "'][motif='" + id_motif + "'][kualitas='" + id_kualitas + "'][dudulan='" + id_dudul + "'][label='" + id_label + "'][kemasan='" + id_kemasan + "'][kemasan_satuan='" + id_kemasansatuan + "'][kemasan_sepuluh='" + id_kemasansepuluh + "'][kemasan_seratus='" + id_kemasanseratus + "'][spk='" + workorder + "'][workorder='" + workorder_barang + "'][value='"+ id_barang +"'][satuan='"+id_satuan+"'][gudang='"+id_gudang+"'][proses='"+id_proses+"'][id_cap='"+id_cap+"'][id_kual_proses='"+id_kualitas_proses+"'][id_kual_periksa='"+id_kualitas_periksa+"'][rencana='"+id_rencana+"'][jumlah_rencana='"+jumlah_rencana+"'] ");

					

					console.log(jum.length);
						if(jum.length == '0'){
							for(i in respon){
								var id_gudang_ ='',nama_gudang = '',name_gudang= '',id_komposisi_ = '',nama_komposisi = '',name_komposisi = '',id_corak_ = '',nama_corak = '',name_corak = '',id_motif_ = '',nama_motif = '',name_motif = '',id_kual_ = '',nama_kual = '',name_kual = '',id_dudul_ = '',nama_dudul = '',name_dudul = '',id_label_ = '',nama_label = '',name_label = '',id_kemasan_ = '',nama_kemasan = '',name_kemasan = '',id_kemasansatuan_ = '',nama_kemasansatuan = '',name_kemasansatuan = '',id_kemasansepuluh_ = '',nama_kemasansepuluh = '',name_kemasansepuluh = '',id_kemasanseratus_ = '',nama_kemasanseratus = '',name_kemasanseratus = '',id_proses_ = '',nama_proses = '',name_proses = '',spk_ = '',no_spk = '',nomor_spk = '',workorder_ = '',no_workorder = '',nomor_workorder = '',id_cap_ = '', nama_cap = '', cap = '', id_kual_proses = '', nama_kual_proses = '', kual_proses = '', id_kual_periksa = '', nama_kual_periksa = '', kual_periksa = '', id_rencana = '', nama_rencana = '', jumlah_rencana = '', nama_satuanrencana = '';

								if(respon[i].id_gudang != ''){
									id_gudang_ = respon[i].id_gudang;
									nama_gudang = ' - '+respon[i].nama_gudang;
									name_gudang = respon[i].nama_gudang;
								}
								if(respon[i].id_komposisi != ''){
									id_komposisi_ = respon[i].id_komposisi;
									nama_komposisi = ' - '+respon[i].nama_komposisi;
									name_komposisi = respon[i].nama_komposisi;
								}
								if(respon[i].id_corak != ''){
									id_corak_ = respon[i].id_corak; 
									nama_corak = ' - '+respon[i].nama_corak;
									name_corak = respon[i].nama_corak; 
								}
								if(respon[i].id_motif != ''){
									id_motif_ = respon[i].id_motif; 
									nama_motif = ' - '+respon[i].nama_motif;
									name_motif = respon[i].nama_motif;
								}
	                            if(respon[i].id_kualitas != ''){
	                                id_kual_ = respon[i].id_kualitas; 
	                                nama_kual = ' - '+respon[i].nama_kualitas; 
	                                name_kual = respon[i].nama_kualitas;
	                            }
	                            if(respon[i].id_kualitas_dudul != ''){
	                                id_dudul_ = respon[i].id_kualitas_dudul;
	                                nama_dudul = ' - Dudulan '+respon[i].kualitas_dudul;
	                                name_dudul = respon[i].kualitas_dudul;
	                            }

	                            if(respon[i].id_label !=''){
	                                id_label_ = respon[i].id_label;
	                                nama_label = ' - '+respon[i].nama_label;
	                                name_label = respon[i].nama_label;
	                            }

	                            if(respon[i].id_kemasan != ''){
	                                id_kemasan_ = respon[i].id_kemasan;
	                                nama_kemasan = ' - '+respon[i].nama_kemasan;
	                                name_kemasan = respon[i].nama_kemasan
	                            }

	                            if(respon[i].id_kemasansatuan != ''){
	                                id_kemasansatuan_ = respon[i].id_kemasansatuan;
	                                nama_kemasansatuan = ' - '+respon[i].nama_kemasansatuan;
	                                name_kemasansatuan = respon[i].nama_kemasansatuan;
	                            }

	                            if(respon[i].id_kemasansepuluh != ''){
	                                id_kemasansepuluh_ = respon[i].id_kemasansepuluh;
	                                nama_kemasansepuluh = ' - '+respon[i].nama_kemasansepuluh;
	                                name_kemasansepuluh = respon[i].nama_kemasansepuluh;
	                            }

	                            if(respon[i].id_kemasanseratus != ''){
	                                id_kemasanseratus_ = respon[i].id_kemasanseratus;
	                                nama_kemasanseratus = ' - '+respon[i].nama_kemasanseratus;
	                                name_kemasanseratus = respon[i].nama_kemasanseratus;
	                            }

	                            if(respon[i].id_proses != ''){
	                            	id_proses_ = respon[i].id_proses;
	                            	nama_proses = ' - '+respon[i].nama_proses;
	                            	name_proses = respon[i].nama_proses;
	                            }

	                            if(respon[i].id_workorder != ''){
	                            	spk_ = respon[i].id_workorder;
	                            	no_spk = ' - '+respon[i].no_workorder;
	                            	nomor_spk = respon[i].no_workorder;
	                            }

	                            if(respon[i].id_workorder_barang != ''){
	                            	workorder_ = respon[i].id_workorder_barang;
	                            	no_workorder = ' - '+respon[i].no_workorder_barang;
	                            	nomor_workorder = respon[i].no_workorder_barang;
	                            }

	                            if(respon[i].id_cap != ''){
	                            	id_cap_ = respon[i].id_cap;
	                            	nama_cap = ' - '+respon[i].nama_cap;
	                            	cap = respon[i].nama_cap;
	                            }

	                            if(respon[i].id_kualitas_proses != ''){
                            	id_kualitas_proses = respon[i].id_kualitas_proses;
                            	nama_kual_proses = ' - '+respon[i].nama_kualitas_proses;
                            	kual_proses = respon[i].nama_kualitas_proses;
                            }

                            if(respon[i].id_kualitas_periksa != ''){
                            	id_kualitas_periksa = respon[i].id_kualitas_periksa;
                            	nama_kual_periksa = ' - '+respon[i].nama_kualitas_periksa;
                            	kual_periksa = respon[i].nama_kualitas_periksa;
                            }

                            if(respon[i].id_perubahansatuan != ''){
                            	id_rencana = respon[i].id_perubahansatuan;
                            	nama_rencana = ' - Untuk '+respon[i].jumlah_rencana+' '+respon[i].nama_perubahansatuan;
                            	jumlah_rencana = respon[i].jumlah_rencana;
                            }
								$("[name=popup_kode]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].id_satuan+'" gudang="'+id_gudang_+'" nama_gudang="'+name_gudang+'" komposisi="'+id_komposisi_+'" nama_komposisi="'+name_komposisi+'" corak="'+id_corak_+'" nama_corak="'+name_corak+'" motif="'+id_motif_+'" nama_motif="'+name_motif+'" kualitas="'+id_kual_+'" nama_kualitas="'+name_kual+'" dudulan="'+id_dudul_+'" nama_dudulan="'+name_dudul+'" label="'+id_label_+'" nama_label="'+name_label+'" kemasan="'+id_kemasan_+'" nama_kemasan="'+name_kemasan+'" kemasan_satuan="'+id_kemasansatuan_+'" nama_kemasansatuan="'+name_kemasansatuan+'" kemasan_sepuluh="'+id_kemasansepuluh_+'" nama_kemasansepuluh="'+name_kemasansepuluh+'" kemasan_seratus="'+id_kemasanseratus_+'" nama_kemasanseratus="'+name_kemasanseratus+'" proses="'+id_proses_+'" nama_proses="'+name_proses+'" spk="'+spk_+'" no_spk="'+nomor_spk+'" workorder="'+workorder_+'" no_workorder="'+nomor_workorder+'" id_cap="'+id_cap_+'" cap="'+cap+'" id_kual_proses="'+id_kualitas_proses+'" kual_proses="'+kual_proses+'" id_kual_periksa="'+id_kualitas_periksa+'" kual_periksa="'+kual_periksa+'" rencana="'+id_rencana+'" jumlah_rencana="'+jumlah_rencana+'">'+respon[i].kategori+' - '+respon[i].sub_kategori+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+nama_gudang+nama_komposisi+kual_proses+nama_corak+nama_motif+nama_kual+nama_dudul+kual_periksa+nama_label+nama_cap+nama_kemasan+nama_kemasansatuan+nama_kemasansepuluh+nama_kemasanseratus+no_workorder+nama_proses+nama_rencana+'</option>');	
								}
							}
				}
			})
		  }
  			
		  setTimeout(function () {
		    r.resolve();
		  }, 2500);
		  return r;
		};

		var FunctionTwo = function () {
			// $("[name=popup_kode]").val(id_barang).trigger('change');


		$("[name=popup_kode]").find("option[komposisi='" + id_komposisi + "'][corak='" + id_corak + "'][motif='" + id_motif + "'][kualitas='" + id_kualitas + "'][gudang='" + id_gudang + "'][dudulan='" + id_dudul + "'][label='" + id_label + "'][kemasan='" + id_kemasan + "'][kemasan_satuan='" + id_kemasansatuan + "'][kemasan_sepuluh='" + id_kemasansepuluh + "'][kemasan_seratus='" + id_kemasanseratus + "'][spk='" + workorder + "'][workorder='" + workorder_barang + "'][value='"+ id_barang +"'][satuan='"+id_satuan+"'][gudang='"+id_gudang+"'][proses='"+id_proses+"'][id_cap='"+id_cap+"'][id_kual_proses='"+id_kualitas_proses+"'][id_kual_periksa='"+id_kualitas_periksa+"'][rencana='"+id_rencana+"'][jumlah_rencana='"+jumlah_rencana+"'] ").prop('selected', true).trigger("change");

		console.log("id barang "+id_barang+"\nid satuan "+id_satuan+"\nid gudang "+id_gudang+"\nid corak "+id_corak+"\nid proses "+id_proses+"\nid komposisi "+id_komposisi+"\nid kualitas "+id_kualitas+"\nid dudul "+id_dudul+"\nid motif "+id_motif+"\nid label "+id_label+"\nid kemasan "+id_kemasan+"\nid kemasan_satuan "+id_kemasansatuan+"\nid kemasan_sepuluh "+id_kemasansepuluh+"\nid kemasan_seratus "+id_kemasanseratus+"\n no_workorder "+workorder+"\nworkorder barang "+workorder_barang+"\ncap "+id_cap+"\nid kualitas proses "+id_kualitas_proses+"\nid kualitas periksa "+id_kualitas_periksa+"\nid rencana "+id_rencana+"\njumlah rencana "+jumlah_rencana); 

	

			$("[name=popup_jumlah]").val(jumlah);

		};

		FunctionOne().done(FunctionTwo);


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

	function get_edit(id){
		$.ajax({
			url: '{{ url('barangkeluar_kemasan/get_edit') }}',
			type: 'post',
			dataType: 'json',
			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 class="btn-group"><button type="button" class="btn bg-warning-400" onclick="edit_table('+respon[i].id_barangkeluar_kb+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>',
							'<div id="list_workorder'+respon[i].id_barangkeluar_kb+'">'+respon[i].no_workorder_barang+'</div>',
							'<div id="list_kode'+respon[i].id_barangkeluar_kb+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_proses'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_proses+'</div>',
							'<div id="list_warna'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_komposisi+'</div>',
							'<div id="list_gudang'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_gudang+'</div>',
							'<div id="list_jumlah'+respon[i].id_barangkeluar_kb+'">'+respon[i].jumlah+'</div>',
							'<div id="list_kualitas_proses'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kualitas_proses+'</div>',
							'<div id="list_corak'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_corak+'</div>',
							'<div id="list_motif'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_motif+'</div>',
							'<div id="list_kualitas'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kualitas+'</div>',
							'<div id="list_dudulan'+respon[i].id_barangkeluar_kb+'">'+respon[i].kualitas_dudul+'</div>',
							'<div id="list_kualitas_periksa'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kualitas_periksa+'</div>',
							'<div id="list_label'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_label+'</div>',
							'<div id="list_cap'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_cap+'</div>',
							'<div id="list_kemasan'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kemasan+'</div>',
							'<div id="list_kemasansatuan'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kemasansatuan+'</div>',
							'<div id="list_kemasansepuluh'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kemasansepuluh+'</div>',
							'<div id="list_kemasanseratus'+respon[i].id_barangkeluar_kb+'">'+respon[i].nama_kemasanseratus+'</div>',
							'<div><button type="button" class="btn bg-warning-400" onclick="edit_table('+respon[i].id_barangkeluar_kb+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>'+
							'<input type="hidden" id="tabel_id'+respon[i].id_barangkeluar_kb+'" name="tabel_id[]" value="'+respon[i].id_barangkeluar_kb+'">'+
							'<input type="hidden" id="tabel_idsatuan'+respon[i].id_barangkeluar_kb+'" name="tabel_idsatuan[]" value="'+respon[i].id_satuan+'">'+
							'<input type="hidden" id="tabel_idbarang'+respon[i].id_barangkeluar_kb+'" name="tabel_kode[]" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" id="tabel_namabarang'+respon[i].id_barangkeluar_kb+'" name="tabel_nama[]" value="'+respon[i].nama_barang+'">'+
							'<input type="hidden" id="tabel_idproses'+respon[i].id_barangkeluar_kb+'" name="tabel_idproses[]" value="'+respon[i].id_proses+'">'+
							'<input type="hidden" id="tabel_idwarna'+respon[i].id_barangkeluar_kb+'" name="tabel_idwarna[]" value="'+respon[i].id_komposisi+'">'+
							'<input type="hidden" id="tabel_idgudang'+respon[i].id_barangkeluar_kb+'" name="tabel_idgudang[]" value="'+respon[i].id_gudang+'">'+
							'<input type="hidden" id="tabel_idkualitas_proses'+respon[i].id_barangkeluar_kb+'" name="tabel_idkualitas_proses[]" value="'+respon[i].id_kualitas_proses+'">'+
							'<input type="hidden" id="tabel_idcorak'+respon[i].id_barangkeluar_kb+'" name="tabel_idcorak[]" value="'+respon[i].id_corak+'">'+
							'<input type="hidden" id="tabel_idmotif'+respon[i].id_barangkeluar_kb+'" name="tabel_idmotif[]" value="'+respon[i].id_motif+'">'+
							'<input type="hidden" id="tabel_idkualitas'+respon[i].id_barangkeluar_kb+'" name="tabel_idkualitas[]" value="'+respon[i].id_kualitas+'">'+
							'<input type="hidden" id="tabel_idkualitas_dudul'+respon[i].id_barangkeluar_kb+'" name="tabel_idkualitas_dudul[]" value="'+respon[i].id_kualitas_dudul+'">'+
							'<input type="hidden" id="tabel_idkualitas_periksa'+respon[i].id_barangkeluar_kb+'" name="tabel_idkualitas_periksa[]" value="'+respon[i].id_kualitas_periksa+'">'+
							'<input type="hidden" id="tabel_idlabel'+respon[i].id_barangkeluar_kb+'" name="tabel_idlabel[]" value="'+respon[i].id_label+'">'+
							'<input type="hidden" id="tabel_idcap'+respon[i].id_barangkeluar_kb+'" name="tabel_idcap[]" value="'+respon[i].id_cap+'">'+
							'<input type="hidden" id="tabel_idkemasan'+respon[i].id_barangkeluar_kb+'" name="tabel_idkemasan[]" value="'+respon[i].id_kemasan+'">'+
							'<input type="hidden" id="tabel_idkemasan_satuan'+respon[i].id_barangkeluar_kb+'" name="tabel_idkemasan_satuan[]" value="'+respon[i].id_kemasansatuan+'">'+
							'<input type="hidden" id="tabel_idkemasan_sepuluh'+respon[i].id_barangkeluar_kb+'" name="tabel_idkemasan_sepuluh[]" value="'+respon[i].id_kemasansepuluh+'">'+
							'<input type="hidden" id="tabel_idkemasan_seratus'+respon[i].id_barangkeluar_kb+'" name="tabel_idkemasan_seratus[]" value="'+respon[i].id_kemasanseratus+'">'+
							'<input type="hidden" id="tabel_spk'+respon[i].id_barangkeluar_kb+'" name="tabel_spk[]" value="'+respon[i].id_workorder+'">'+
							'<input type="hidden" id="tabel_workorder'+respon[i].id_barangkeluar_kb+'" name="tabel_workorder[]" value="'+respon[i].id_workorder_barang+'">'+
							'<input type="hidden" id="tabel_jumlah'+respon[i].id_barangkeluar_kb+'" name="tabel_jumlah[]" value="'+respon[i].jumlah+'">'+
							'<input type="hidden" id="tabel_total'+respon[i].id_barangkeluar_kb+'" name="tabel_total[]" value="'+respon[i].jumlah+'">'+
							'<input type="hidden" id="tabel_idrencana'+respon[i].id_barangkeluar_kb+'" name="tabel_idrencana[]" value="'+respon[i].id_perubahansatuan+'">'+
							'<input type="hidden" id="tabel_rencanajumlah'+respon[i].id_barangkeluar_kb+'" name="tabel_rencanajumlah[]" value="'+respon[i].jumlah_rencana+'">'
						]).draw(false);
					}
				}
			}
		})
	}
	
	function hapus(d){
		table_barang.row(d.parents('tr')).remove().draw();
		
		//  alert(d);
	}

</script>
@endsection