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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Input Laporan Hasil Lanjutan</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" action="{{ url('barangmasuk_atm/simpan_tenun') }}" autocomplete="off">
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_barangmasuk" value="{{ $data['detail']['id_barangmasuk'] }}">
	    	<input type="hidden" name="id_barangkeluar" value="{{ $data['detail']['id_barangkeluar'] }}">
	    	<input type="hidden" name="barangkeluar" value="{{ $data['detail']['barangkeluar'] }}">
        	<table class="table table-striped">
        		<tr>
        			<td width="20%">Tanggal Selesai</td>
        			<td width="5%">:</td>
        			<td width="25%"><input class="form-control datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ $data['detail']['tanggal'] }}" required/></td>

        			<td width="20%">Rekanan</td>
        			<td width="5%">:</td>
        			<td width="25%">{{ $data['detail']['rekanan'] }}</td>
        		</tr>
        		<tr>
        			<td>Tanggal Pengambilan</td>
        			<td>:</td>
        			<td>{{ $data['detail']['tanggal_pengambilan'] }}</td>

        			<td width="20%">Mesin</td>
        			<td width="5%">:</td>
        			<td width="25%">{{ $data['detail']['mesin'] }}</td>
        		</tr>
        		<tr>
        			<td>Tanggal Perkiraan Selesai</td>
        			<td>:</td>
        			<td>{{ $data['detail']['tanggal_perkiraan'] }}</td>

        			<td width="20%">Proses Produk</td>
        			<td width="5%">:</td>
        			<td width="25%">{{ $data['detail']['proses'] }}</td>
        		</tr>
        		<tr>
        			<!-- <td>No. SPK</td>
        			<td>:</td>
        			<td>{{ $data['detail']['no_workorder'] }}</td> -->

        			<td width="20%">No. Order</td>
        			<td width="5%">:</td>
        			<td width="25%">{{ $data['detail']['no_workorder_barang'] }}</td>
        		</tr>
        		<tr>
					<td>Habis Beem</td>
        			<td>:</td>
        			<td> @php $stat = $data['barangkeluar']->status_beem; @endphp
        			<select name="status_beem" class="form-control select-search">
        				<option value="0" {{ ($stat == '0') ? "selected":"" }}>Belum Selesai</option>
        				<option value="1" {{ ($stat == '1') ? "selected":"" }}>Selesai</option>
        			</select>
        			</td>
        			
        		</tr>
        	</table>
        	
        	<div class="col-sm-12 form-group">
	        	<h5 class="panel-title">Barang Diambil</h5>
	        	<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>Warna</th>
								<th>Gudang</th>
								<th>Jumlah</th>
								<th>Corak</th>
								<th>Motif</th>
								<th>Kualitas</th>
						</thead>
						<tbody>
							@php $no=1; @endphp
							@foreach($data['barang'] as $d) 
							<tr>
								<td>{{$no++}}</td>
								<td>{{ $d->kode_barang }}</td>
								<td>{{ $d->nama_barang }}</td>
								<td>{{ $d->nama_komposisi }}</td>
								<td>{{ $d->nama_gudang }}</td>
								<td>{{ $d->jumlah }}</td>
								<td>{{ $d->motif_keterangan }}</td>
								<td>{{ $d->nama_motif }}</td>
								<td>{{ $d->nama_kualitas }}</td>
							</tr>
							@endforeach
						</tbody>
				    </table>
	        	</div>
        	</div>
            
            <div class="col-sm-12 form-group">
	        	<h5 class="panel-title">Barang Disetor</h5>
		        <table class="table table-stripped" id="table_hasil">
				   	<thead>
						<tr>
							<th>No</th>
							<th>Kode Barang</th>
							<th>Nama Barang</th>
							<th>Warna</th>
							<th>Jumlah</th>
							<th>Satuan</th>
							<th>Gudang</th>
						</tr>
					</thead>
					<tbody id="body_setor">
						@php $no = 1000; @endphp
						@foreach($data['setor'] as $d) 
						<tr>
							<td></td>
							<td>{{ $d['tabel_kode'] }}</td>
							<td>{{ $d['tabel_nama'] }}</td>
							<td>{{ $d['tabel_komposisi'] }}</td>
							<td>{{ $d['tabel_jumlah'] }}</td>
							<td>{{ $d['tabel_namasatuan'] }}</td>
							<td>{{ $d['tabel_namagudang'] }}</td>
							<input type="hidden" id="td_idbarang{{$no}}" value="{{ $d['tabel_idbarang'] }}">
							<input type="hidden" id="td_kodebarang{{$no}}" value="{{ $d['tabel_kode'] }}">
							<input type="hidden" id="td_jumlah{{$no}}" value="{{ $d['tabel_jumlah'] }}">
							<input type="hidden" id="td_idkomposisi{{$no}}" value="{{ $d['tabel_idkomposisi'] }}">
						</tr>
						@php $no++; @endphp
						@endforeach
					</tbody>
			    </table>
        	</div>

        	<div class="col-sm-12 form-group">
        		<button class="btn btn-success" type="button" id="btn_pembagi">Tambah</button>
	        	<h5 class="panel-title">Penilaian Barang</h5>
		        	<table class="table table-stripped" id="table_pembagi">
				    	<thead>
							<tr>
								<th>No</th>
								<th>Kode Barang</th>
								<th>Nama Barang</th>
								<th>Motif</th>
								<th>Jumlah</th>
								<th>Satuan</th>
								<th>Gudang</th>
								<th>Panjang (cm)</th>
								<th>Lebar (cm)</th>
								<th>Pick</th>
								<th>Kualitas</th>
								<th>Aksi</th>
							</tr>
						</thead>
						<tbody id="body_setor">
						</tbody>
			    	</table>
        	</div>

        	<div class="col-sm-12">
        	{{-- <a href="{{url('barangmasuk_atm')}}" class="btn bg-warning-400">Kembali</a> --}}
            <div class="form-group pull-right">
             	<button type="submit" class="btn btn-primary">Simpan</button>
            </div>
            </div>
	    </div>
	    </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">Gudang</label>
	      				<div class="col-sm-8">
	      			 	<select class="form-control select-search" name="popup_gudang">
	      			 		
	      			 	</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Stok</b></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">
	      				<label class="control-label col-sm-4">Satuan</label>
	      				<div class="col-sm-8">
	      			 		<select class="form-control select-search" name="popup_satuan">
	      			 		</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Motif</label>
	      				<div class="col-sm-8">	
	      			 		<select name="popup_motif" class="form-control select-search">
	      			 		</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Panjang (cm)</label>
	      				<div class="col-sm-8">	
	      			 		<input type="text" name="popup_panjang" class="form-control number-only">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Lebar (cm)</label>
	      				<div class="col-sm-8">	
	      			 		<input type="text" name="popup_lebar" class="form-control number-only">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Pick</label>
	      				<div class="col-sm-8">	
	      			 		<input type="text" name="popup_pick" class="form-control number-only">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Kualitas</label>
	      				<div class="col-sm-8">	
	      			 		<select name="popup_kualitas" class="form-control select-search">
	      			 		
	      			 		</select>
	      				</div>
	      			</div>	
	      			<!---- Shift ---->
	        		<div class="form-group">
	        			<label class="control-label col-sm-4">Shit</label>
	        			<div class="col-sm-8">
	        				<select class="form-control select-search" name="popup_shift">
	        					
	        				</select>
	        			</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_motif" value="{{ $data['motif']->id_motif }}">

<script type="text/javascript">
	var motif = $("[name=store_motif]").val();

	$("#btn_pembagi").click(function(){
		$("[name=popup_id_table]").val('');
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_gudang]").val(null).trigger('change');
		$("[name=popup_jumlah]").val('');
		$("[name=popup_satuan]").val(null).trigger('change');
		$("[name=popup_panjang]").val('');
		$("[name=popup_lebar]").val('');
		$("[name=popup_pick]").val('');
		$("[name=popup_stok]").text('');
		$("[name=popup_kualitas]").val(null).trigger('change');
		$("[name=popup_motif]").val(motif).trigger('change');
		$("[name=popup_shift]").val(null).trigger('change');

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

	var table_barang;
	var table_hasil;
	var table_pembagi;
	var id_keluar;
	var id_masuk;

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

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

        table_pembagi = $("#table_pembagi").DataTable({
        	"paging": false
        });
		table_pembagi.on( 'order.dt search.dt', function () {
            table_pembagi.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();
        
        id_keluar = $("[name=barangkeluar]").val();
        id_masuk = $("[name=id_barangmasuk]").val();


        if(id_masuk != ''){
        	get_edit(id_masuk);
        }
		get_barang();
        get_gudang();
        get_kualitas();
        get_satuan();
        get_komposisi();
        get_motif();
        get_shift();

	})

	function get_kualitas(){
		$.ajax({
			url: '{{ url('barangmasuk/get_kualitas') }}'+'/'+1,
			success: function(respon){
				$("[name=popup_kualitas]").html('');
				$("[name=popup_kualitas]").append('<option value="">--- Pilih Kualitas ---</option>')
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_kualitas]").append('<option value="'+respon[i].id_kualitas+'" nama="'+respon[i].nama_kualitas+'">'+respon[i].nama_kualitas+'</option>');
					}
				}
			}
		})
	}	

	function get_barang(){
		$.ajax({
			url: '{{ url('barangmasuk/get_barang') }}',
			success: function(respon){
				$("[name=popup_barang]").html('');
				$("[name=popup_barang]").append('<option value="">--- Pilih Barang ---</option>')
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_barang]").append('<option value="'+respon[i].id_barang+'" nama="'+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>');
					}
				}
			}
		})
	}

	function get_gudang(){
		$.ajax({
			url: '{{ url('barangmasuk/get_gudang') }}',
			success: function(respon){
				$("[name=popup_gudang]").html('');
				$("[name=popup_gudang]").append('<option value="">--- Pilih Gudang ---</option>');
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_gudang]").append('<option value="'+respon[i].id_gudang+'" nama="'+respon[i].nama_gudang+'">'+respon[i].nama_gudang+'</option>');
					}
				}
			}
		})
	}

	function get_satuan(){
		$.ajax({
			url: '{{ url('barangmasuk/get_satuan') }}',
			success: function(respon){
				$("[name=popup_satuan]").html('');
				$("[name=popup_satuan]").append('<option value="">--- Pilih Satuan ---</option>');
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_satuan]").append('<option value="'+respon[i].id_satuan+'" nama="'+respon[i].nama_satuan+'">'+respon[i].nama_satuan+'</option>');
					}
				}
			}
		})
	}

	function get_komposisi(){
		$.ajax({
			url: '{{ url('barangmasuk/get_komposisi') }}',
			success: function(respon){
				$("[name=popup_komposisi]").html('');
				$("[name=popup_komposisi]").append('<option value="">--- Pilih Warna ---</option>');
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_komposisi]").append('<option value="'+respon[i].id_komposisi+'" nama="'+respon[i].nama_komposisi+'">'+respon[i].nama_komposisi+'</option>');
					}
				}
			}
		})
	}

	function get_motif(){
		$.ajax({
			url: '{{ url('barangmasuk/get_motif_kualitas') }}',
			success: function(respon){
				$("[name=popup_motif]").html('');
				$("[name=popup_motif]").append('<option value="">--- Pilih motif ---</option>');
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_motif]").append('<option value="'+respon[i].id_motif+'" nama="'+respon[i].nama_motif+'">'+respon[i].nama_motif+'</option>');
					}
				}
			}
		})
	}

	// --- shift ------
	function get_shift(){
		$.ajax({
			url: '{{ url('barangmasuk_atm/get_shift') }}',
			success: function(respon){
				$("[name=popup_shift]").html('');
				$("[name=popup_shift]").append('<option value="">--- Pilih shift ---</option>');
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_shift]").append('<option value="'+respon[i].id_shift+'" nama="'+respon[i].nama_shift+'" jam="'+respon[i].jam+'">'+respon[i].nama_shift+' ('+respon[i].jam_masuk+' - '+respon[i].jam_pulang+')</option>');
					}
				}
			}
		})
	}

	function get_edit(id){
		$.ajax({
			url: '{{ url('barangmasuk_atm/get_tenunedit') }}',
			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_pembagi.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_motif'+respon[i].id+'">'+respon[i].nama_motif+'</div>',
							'<div id="list_jumlah'+respon[i].id+'">'+respon[i].jumlah+'</div>',
							'<div id="list_namasatuan'+respon[i].id+'">'+respon[i].nama_satuan+'</div>',
							'<div id="list_gudang'+respon[i].id+'">'+respon[i].nama_gudang+'</div>',
							'<div id="list_panjang'+respon[i].id+'">'+respon[i].panjang+'</div>',
							'<div id="list_lebar'+respon[i].id+'">'+respon[i].lebar+'</div>',
							'<div id="list_pick'+respon[i].id+'">'+respon[i].pick+'</div>',
							'<div id="list_kualitas'+respon[i].id+'">'+respon[i].nama_kualitas+'</div>',
							'<div><button type="button" id="btnedit'+respon[i].id+'" class="btn bg-warning-400" onclick="edit_table('+respon[i].id+')"><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_idtable'+respon[i].id+'" name="tabel_idtable[]" value="'+respon[i].id+'">'+
							'<input type="hidden" name="tabel_id[]" class="tabel_id" id="tabel_id'+respon[i].id+'" value="'+respon[i].id+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_idgudang[]" id="tabel_idgudang'+respon[i].id+'" value="'+respon[i].id_gudang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+respon[i].id+'" value="'+respon[i].jumlah+'">'+
							'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+respon[i].id+'" value="'+respon[i].id_satuan+'">'+
							'<input type="hidden" name="tabel_idmotif[]" id="tabel_idmotif'+respon[i].id+'" value="'+respon[i].id_motif+'">'+
							'<input type="hidden" name="tabel_idkualitas[]" id="tabel_idkualitas'+respon[i].id+'" value="'+respon[i].id_kualitas+'">'+
							'<input type="hidden" name="tabel_panjang[]" id="tabel_panjang'+respon[i].id+'" value="'+respon[i].panjang+'">'+
							'<input type="hidden" name="tabel_lebar[]" id="tabel_lebar'+respon[i].id+'" value="'+respon[i].lebar+'">'+
							'<input type="hidden" name="tabel_pick[]" id="tabel_pick'+respon[i].id+'" value="'+respon[i].pick+'">'+
							'<input type="hidden" name="tabel_idshift[]" id="tabel_idshift'+respon[i].id+'" value="'+respon[i].id_shift+'">'
							]).draw(false);
					}
				}
			}
		})
	}

	// function get_wo(id){
	// 	$.ajax({
	// 		url: '{{ url('barangmasuk/get_detail_kualitas') }}',
	// 		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_pembagi.row.add(['<div><center></center></div>',
	// 						'<div id="list_kode'+tb_no+'">'+respon[i].kode_barang+'</div>',
	// 						'<div id="list_nama'+tb_no+'">'+respon[i].nama_barang+'</div>',
	// 						'<div id="list_motif'+tb_no+'">'+respon[i].nama_motif+'</div>',
	// 						'<div id="list_jumlah'+tb_no+'"></div>',
	// 						'<div id="list_namasatuan'+tb_no+'"></div>',
	// 						'<div id="list_gudang'+tb_no+'"></div>',
	// 						'<div id="list_kualitas'+tb_no+'"></div>',
	// 						'<div><button type="button" id="btnedit'+tb_no+'" 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_idtable'+tb_no+'" name="tabel_idtable[]" value="">'+
	// 						'<input type="hidden" name="tabel_id[]" class="tabel_id" id="tabel_id'+tb_no+'" value="'+tb_no+'">'+
	// 						'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+tb_no+'" value="'+respon[i].id_barang+'">'+
	// 						'<input type="hidden" name="tabel_idgudang[]" id="tabel_idgudang'+tb_no+'">'+
	// 						'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+tb_no+'">'+
	// 						'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+tb_no+'" value="">'+
	// 						'<input type="hidden" name="tabel_idmotif[]" id="tabel_idmotif'+tb_no+'" value="'+respon[i].id_motif+'">'+
	// 						'<input type="hidden" name="tabel_idkualitas[]" id="tabel_idkualitas'+tb_no+'">'
	// 						]).draw(false);
	// 				}
	// 				tb_no++;
	// 			}
	// 		}
	// 	})
	// }

	$("[name=popup_barang]").change(function(){
		var total = parseFloat(0);
		var ambil = parseFloat(0);
		var id_barang = $("[name=popup_barang]").val();
		var nilai = parseFloat(0);

		$('input[id^="td_jumlah"]').each(function() {
				              
			var val = ($(this).attr('id'));
			val = val.replace('td_jumlah', '');
			if($("#td_idbarang"+val).val() == id_barang){
			   	var jum = $("#td_jumlah"+val).val();
			    total += parseFloat(jum);
			}
			              
	    });

	    $('input[id^="tabel_jumlah"]').each(function() {
				              
			var val = ($(this).attr('id'));
			val = val.replace('tabel_jumlah', '');
			if($("#tabel_idbarang"+val).val() == id_barang){
			   	var jum = $("#tabel_jumlah"+val).val();
			    ambil += parseFloat(jum);
			}
			              
	    });

	    if($("[name=popup_id_table]").val() == ''){
	    	nilai = total-ambil;
	    }else{
	    	nilai = (total-ambil)+parseFloat($("[name=popup_jumlah]").val());
	    }
	    $("[name=popup_stok]").text(nilai);
	    $("[name=popup_jumlah]").attr('min', 0);
		$("[name=popup_jumlah]").attr('max', nilai);

	    console.log(id_barang, total, ambil, nilai);
	})
	
	function get_proses_jml(jml,upah,id){
		var p_jml = parseFloat(jml);
		if(isNaN(p_jml) || p_jml == 0 || p_jml == ""){
			p_jml = parseFloat(0);
		}

		var p_upah = parseFloat(upah);
		if(isNaN(p_upah) || p_upah == 0 || p_upah == ""){
			p_upah = parseFloat(0);
		}
		var tot = p_jml*p_upah;
		$('#list_total'+id).text(curr(tot));
		$('#tabel_total'+id).val(tot);
		// console.log(tot);
	}


	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id_table]").val();
		var id_barang = $("[name=popup_barang]").val();
		var id_gudang = $("[name=popup_gudang]").val();
		var id_upah = $("[name=popup_idupah]").val();
		var kode_barang = $("[name=popup_barang] :selected").attr('kode');
		var nama_barang = $("[name=popup_barang] :selected").attr('nama');
		var nama_gudang = $("[name=popup_gudang] :selected").attr('nama');
		var upah = $("[name=popup_upah]").val();
		var jumlah = $("[name=popup_jumlah]").val();
		
		var satuan = $("[name=popup_satuan]").val();
		var nama_satuan = $("[name=popup_satuan] :selected").attr('nama');

		var id_motif = $("[name=popup_motif]").val();
		var motif = $("[name=popup_motif] :selected").attr('nama');

		var id_kualitas = $("[name=popup_kualitas]").val();
		var kualitas = $("[name=popup_kualitas] :selected").attr('nama');

		var panjang = $("[name=popup_panjang]").val();
		var lebar = $("[name=popup_lebar]").val();
		var pick = $("[name=popup_pick]").val();

		var id_shift = $("[name=popup_shift]").val();


		if(id_barang != '' && jumlah != '' && jumlah != 0 && id_gudang != '' && satuan != '' && motif != '' && panjang != '' && lebar != '' && pick != ''){
			if(id_data == ''){
				table_pembagi.row.add(['<div><center></center></div>',
							'<div id="list_kode'+tb_no+'">'+kode_barang+'</div>',
							'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
							'<div id="list_motif'+tb_no+'">'+motif+'</div>',
							'<div id="list_jumlah'+tb_no+'">'+jumlah+'</div>',
							'<div id="list_namasatuan'+tb_no+'">'+nama_satuan+'</div>',
							'<div id="list_gudang'+tb_no+'">'+nama_gudang+'</div>',
							'<div id="list_panjang'+tb_no+'">'+panjang+'</div>',
							'<div id="list_lebar'+tb_no+'">'+lebar+'</div>',
							'<div id="list_pick'+tb_no+'">'+pick+'</div>',
							'<div id="list_kualitas'+tb_no+'">'+kualitas+'</div>',
							'<div><button type="button" id="btnedit'+tb_no+'" 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_idtable'+tb_no+'" name="tabel_idtable[]" value="">'+
							'<input type="hidden" name="tabel_id[]" class="tabel_id" id="tabel_id'+tb_no+'" value="'+tb_no+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+tb_no+'" value="'+id_barang+'">'+
							'<input type="hidden" name="tabel_idgudang[]" id="tabel_idgudang'+tb_no+'" value="'+id_gudang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+tb_no+'" value="'+jumlah+'">'+
							'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+tb_no+'" value="'+satuan+'">'+
							'<input type="hidden" name="tabel_idmotif[]" id="tabel_idmotif'+tb_no+'" value="'+id_motif+'">'+
							'<input type="hidden" name="tabel_idkualitas[]" id="tabel_idkualitas'+tb_no+'" value="'+id_kualitas+'">'+
							'<input type="hidden" name="tabel_panjang[]" id="tabel_panjang'+tb_no+'" value="'+panjang+'">'+
							'<input type="hidden" name="tabel_lebar[]" id="tabel_lebar'+tb_no+'" value="'+lebar+'">'+
							'<input type="hidden" name="tabel_pick[]" id="tabel_pick'+tb_no+'" value="'+pick+'">'+
							'<input type="hidden" name="tabel_idshift[]" id="tabel_idshift'+tb_no+'" value="'+id_shift+'">'
							]).draw(false);
			}else{
				$("#list_kode"+id_data).text(kode_barang);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_motif"+id_data).text(motif);
				$("#list_gudang"+id_data).text(nama_gudang);
				$("#list_jumlah"+id_data).text(jumlah);
				$("#list_namasatuan"+id_data).text(nama_satuan);
				$("#list_kualitas"+id_data).text(kualitas);
				$("#list_panjang"+id_data).text(panjang);
				$("#list_lebar"+id_data).text(lebar);
				$("#list_pick"+id_data).text(pick);
				

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);
				$("#tabel_idgudang"+id_data).val(id_gudang);
				$("#tabel_namabarang"+id_data).val(nama_barang);				
				$("#tabel_idmotif"+id_data).val(id_motif);
				$("#tabel_jumlah"+id_data).val(jumlah);
				$("#tabel_idsatuan"+id_data).val(satuan);
				$("#tabel_idkualitas"+id_data).val(id_kualitas);
				$("#tabel_panjang"+id_data).val(panjang);
				$("#tabel_lebar"+id_data).val(lebar);
				$("#tabel_pick"+id_data).val(pick);
				$("#tabel_idshift"+id_data).val(id_shift);
			}
		
		$("#exampleModal").modal('hide');
		tb_no++;/*
		get_jum();*/
		}
	})

	function edit_table(id){
		var id_table = $("#tabel_idtable"+id).val();
		var id_barang = $("#tabel_idbarang"+id).val();
		var id_gudang = $("#tabel_idgudang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();

		var satuan = $("#tabel_idsatuan"+id).val();
		var id_kualitas = $("#tabel_idkualitas"+id).val();
		var id_motif = $("#tabel_idmotif"+id).val();

		var panjang = $("#tabel_panjang"+id).val();
		var lebar = $("#tabel_lebar"+id).val();
		var pick = $("#tabel_pick"+id).val();

		var id_shift = $("#tabel_idshift"+id).val();

		$("[name=popup_id_table]").val(id);
		
		$("[name=popup_gudang]").val(id_gudang).trigger('change');
		$("[name=popup_jumlah]").val(jumlah);


		$("[name=popup_satuan]").val(satuan).trigger('change');
		$("[name=popup_kualitas]").val(id_kualitas).trigger('change');
		$("[name=popup_motif]").val(id_motif).trigger('change');

		$("[name=popup_panjang]").val(panjang);
		$("[name=popup_lebar]").val(lebar);		
		$("[name=popup_pick]").val(pick);		

		$("[name=popup_barang]").val(id_barang).trigger('change');

		$("[name=popup_shift]").val(id_shift).trigger('change');

		//console.log(panjang, lebar, pick);

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

	function hapus(d){
		table_pembagi.row(d.parents('tr')).remove().draw();
	}
</script>
@endsection