@extends('layout')


@section('content')

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="index.html"><i class="icon-home2 position-left"></i> Inventori</a></li>
			<li class="active">Stock Opname</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Stock Opname</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('opname/simpan')}}" method="post" autocomplete="off">
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_opname" value="{{$data['data']['id_opname']}}">
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal</label>
        			<div class="col-sm-8">
        				<input class="form-control datepicker" type="text" name="tanggal" 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">Gudang</label>
        			<div class="col-sm-8">
        				<select name="gudang" class="form-control select-search" required>
        					{{-- <option value="">--- Pilih Gudang ---</option> --}}
        					@php 
        					$sel = $data['data']['id_gudang'];
        					foreach($data['gudang'] as $d) {@endphp
        					<option value="{{ $d->id_gudang }}" {{($d->id_gudang == $sel) ? "selected":""}} nama="{{ $d->nama_gudang }}">{{ $d->nama_gudang }}</option>
        					@php } @endphp
        				</select>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Keterangan</label>
        			<div class="col-sm-8">
        				<textarea class="form-control" rows="2" name="keterangan" required>{{$data['data']['keterangan']}}</textarea>
        			</div>
        		</div>
        		
        	</div>
        	<div class="col-sm-6">
				    		
        	</div>

        	
        	<div class="col-sm-12 form-group">
        	<h5 class="panel-title">Detail Barang</h5>
        	<a class="btn bg-success" id="btn_tambah_item">Tambah</a>
        	<div class="col-ms-12">
        	<table class="table table-stripped dataTable">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Kode Barang</th>
					<th>Nama Barang</th>
					<th>Gudang</th>
					<th>Warna</th>
					<th>Corak</th>
					<th>Motif</th>
					<th>Stok</th>
					<th>Fisik</th>	
					<th>Selisih</th>
					<th>Ket.</th>	
					<th>Aksi</th>			
				</tr>
			</thead>
			<tbody id="table_fisik_detail">
				
			</tbody>
	    </table>
        	</div>
        	
             <div class="col-md-6"></div>
             <br>
             <div class="col-md-12">
             	<a href="{{url('opname')}}" class="btn bg-warning-400">Kembali</a>
             <div class="form-group pull-right">
             	<!-- ($data['data']->status == '0') ?'<button class="btn btn-primary" type="submit">Simpan</button>':'' -->
             	
             	<button class="btn btn-primary" type="submit">Simpan</button>
           
             	<!-- <button class="btn btn-primary" type="submit">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">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Proses</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses">
	      						<option value="">Tanpa Proses</option>
	      						@php
	      						foreach($data['proses'] 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">Kode 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_ddd_stok"></label>
	      					<input type="hidden" name="popup_stok" class="form-control number-only" readonly="">
	      				</div>
	      			</div>  			
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah Fisik</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_fisik" class="form-control number-only">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Selisih</label>
	      				<div class="col-sm-8">
	      					<label class="control-label" name="popup_ddd_selisih"></label>
	      					<input type="hidden" name="popup_selisih" class="form-control number-only">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Keterangan</label>
	      				<div class="col-sm-8">
	      					<textarea class="form-control" rows="2" name="popup_ket"></textarea>
	      				</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>

<script type="text/javascript">
	$("#btn_tambah_item").click(function(){
		$("[name=popup_id]").val('');
		$("[name=popup_kode]").val(null).trigger('change');
		$("[name=popup_ddd_stok]").text('');
		$("[name=popup_stok]").val('');
		$("[name=popup_fisik]").val('');
		$("[name=popup_ddd_selisih]").text('');
		$("[name=popup_selisih]").val('');
		$("[name=popup_ket]").val('');

		$('[name=popup_proses]').val($('[name=popup_proses] option:first-child').val()).trigger('change');

		$("#exampleModal").modal('show');
	})
	var table;
	var tb_no = parseInt(10000);
	$(document).ready(function(){
		table = $(".dataTable").DataTable({
			"paging": false
		});
		table.on( 'order.dt search.dt', function () {
            table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();
        var id_opname = $("[name=gudang]").val();
        // get_barang(id_opname);
        id = '{{ $data['data']['id_opname']}}';
		if(id != ''){
			get_edit(id);
		}
		get_selisih();

		// get_jum();
	})

	$("[name=gudang]").change(function(){
		 var id_gudang = $("[name=gudang]").val();		
		if(id_gudang != ''){
			table.clear().draw();
			// get_barang(id_gudang);
		}else{
			$("[name=popup_kode]").html('');
		}
	})

	$("[name=popup_proses]").change(function(){
		var id_barang = $("[name=gudang]").val();
		get_barang(id_barang);
	})

	$("[name=popup_kode]").change(function(){
		var id_barang = $("[name=popup_kode] :selected").val();
		var id_gudang = $("[name=gudang]").val();
		var stok = $("[name=popup_kode] :selected").attr('stok');
		var satuan = $("[name=popup_kode] :selected").attr('nama_satuan');
		var fisik = $("[name=popup_fisik]").val();

		

		if(isNaN(fisik) || fisik == 0 || fisik == ''){
			fisik = parseFloat(0);
		}
		if(id_barang != "" && id_gudang != ''){
			/*get_jml(id_gudang, id_barang);*/
			$("[name=popup_ddd_stok]").html(stok+' '+satuan);
			$("[name=popup_stok]").val(stok);

			get_selisih(stok, fisik);

		}else{
			$("[name=popup_stok]").html('');
		}
	})

	$("[name=popup_fisik]").keyup(function(){
		var stok = $("[name=popup_stok]").val();
		var fisik = $("[name=popup_fisik]").val();
		if(fisik != ""){
			get_selisih(stok,fisik);
		}else{
			$("[name=popup_selisih]").html("");
		}
	})

	
	function get_selisih(stok,fisik){
		var s_fisik = parseFloat(fisik);
		var s_stok = parseFloat(stok);
		if(isNaN(s_fisik) || s_fisik == 0 || s_fisik == ""){
			s_fisik = parseFloat(0);
		} 
		if(isNaN(s_stok) || s_stok == 0 || s_stok == ""){
			s_stok = parseFloat(0);
		} 

		var selisih = s_fisik-s_stok;

		$("[name=popup_selisih]").val(selisih);
		if(selisih < 0){
			$("[name=popup_ddd_selisih]").text(selisih).css({'color':'red'});
		}else{
			$("[name=popup_ddd_selisih]").text(selisih).css({'color':'#333'});
			
		}

}

	function get_barang(id_gudang){
		var tgl = $("[name=tanggal]").val();
		$.ajax({
			url: "{{ url('opname/get_barang') }}",
			type: 'post',
			dataType: 'json',
			data: {id : id_gudang, id_barang : '', id_proses : $("[name=popup_proses]").val(), tanggal : tgl},
			headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
			success: function(result){
				if(result.length > 0){
					$("[name=popup_kode]").html('');
					$("[name=popup_kode]").append('<option value="" kode="">--- Pilih Barang ---</option>');
					for(i in result){

						var id_nk = '', nk = '', komp = '';
						if(result[i].nama_komposisi != ''){
							id_nk = result[i].id_komposisi;
							nk = ' - '+result[i].nama_komposisi+' ( '+result[i].inisial_komposisi+' )';
							komp = result[i].nama_komposisi+' ( '+result[i].inisial_komposisi+' )';
						}

						var id_np = '', np = '';
						if(result[i].nama_proses != ''){
							id_np = result[i].id_proses;
							np = ' - '+result[i].nama_proses;
						}

						var id_nc = '', nc = '', ncr = '';
						if(result[i].nama_corak != ''){
							id_nc = result[i].id_corak;
							nc = ' - '+result[i].nama_corak;
							ncr = result[i].nama_corak;
						}
						
						var id_nm = '', nm = '', nmt = '';
						if(result[i].nama_motif != ''){
							id_nm = result[i].id_motif;
							nm = ' - '+result[i].nama_motif;
							nmt = ' - '+result[i].nama_motif;
						}

						var id_k = '', nama_kual = '';
						if(result[i].id_kualitas != ''){
                           id_k = result[i].id_kualitas; 
                           nama_kual = ' - '+result[i].nama_kualitas; 
                        }

                        var id_dudul = '', nama_dudul = '';
                        if(result[i].id_kualitas_dudul != ''){
                           	id_dudul = result[i].id_kualitas_dudul;
                           	nama_dudul = ' - Dudulan '+result[i].kualitas_dudul;
                        }

                        var id_label = '', nama_label = '';
                        if(result[i].id_label !=''){
                           	id_label = result[i].id_label;
                           	nama_label = ' - '+result[i].nama_label;
                        }

                        var id_kemas = '', nama_kemas = '';
                        if(result[i].id_kemasan != ''){
                           	id_kemas = result[i].id_kemasan;
                           	nama_kemas = ' - '+result[i].nama_kemasan;
                        }

                        var id_kemassatuan = '', nama_kemassatuan = '';
                        if(result[i].id_kemasansatuan != ''){
                           	id_kemassatuan = result[i].id_kemasansatuan;
                           	nama_kemassatuan = ' - '+result[i].nama_kemasansatuan;
                        }

                        var id_kemassepuluh = '', nama_kemassepuluh = '';
                        if(result[i].id_kemasansepuluh != ''){
                           	id_kemassepuluh = result[i].id_kemasansepuluh;
                           	nama_kemassepuluh = ' - '+result[i].nama_kemasansepuluh;
                        }

                        var id_kemasseratus = '', nama_kemasseratus = '';
                        if(result[i].id_kemasanseratus != ''){
                           	id_kemasseratus = result[i].id_kemasanseratus;
                           	nama_kemasseratus = ' - '+result[i].nama_kemasanseratus;
                        }

                        var id_workorder = '', no_workorder = '';
                        if(result[i].id_workorder != ''){
                            id_workorder = result[i].id_workorder;
                            no_workorder = ' - '+result[i].no_workorder;
                        }

                        var id_workorder_barang = '', no_workorder_barang = '';
                        if(result[i].id_workorder_barang != ''){
                            id_workorder_barang = result[i].id_workorder_barang;
                            no_workorder_barang = ' - '+result[i].no_workorder_barang;
                        }

                        var id_satuan = '', nama_satuan = '';
                        if(result[i].id_satuan != ''){
                            id_satuan = result[i].id_satuan;
                            nama_satuan = ' - '+result[i].nama_satuan;
                        }

                        var id_cap = '', nama_cap = '';
                        if(result[i].id_cap != ''){
                            id_cap = result[i].id_cap;
                            nama_cap = ' - '+result[i].nama_cap;
                        }

                        var id_kual_proses = '', nama_kual_proses = '', id_kual_periksa = '', nama_kual_periksa = '';

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

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

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

							$("[name=popup_kode]").append('<option value="'+result[i].id_barang+'" stok="'+result[i].stok+'"  kode="'+result[i].kode_barang+'" satuan="'+result[i].id_satuan+'" nama_satuan="'+result[i].nama_satuan+'" barang="'+result[i].nama_barang+'" komposisi="'+id_nk+'" proses="'+id_np+'" corak="'+id_nc+'" motif="'+id_nm+'" kualitas="'+id_k+'" dudul="'+id_dudul+'" label="'+id_label+'" kemasan="'+id_kemas+'" kemassatuan="'+id_kemassatuan+'" kemassepuluh="'+id_kemassepuluh+'" kemasseratus="'+id_kemasseratus+'" workorder="'+id_workorder+'" workorder_barang="'+id_workorder_barang+'" cap="'+id_cap+'" id_kual_proses="'+id_kual_proses+'" id_kual_periksa="'+id_kual_periksa+'" rencana="'+id_rencana+'" jumlah_rencana="'+jumlah_rencana+'" nama_komposisi="'+komp+'" nama_corak="'+ncr+'" nama_motif="'+nmt+'" >'+result[i].concat_nama+'</option>');
						
					}
				}else{
					$("[name=popup_kode]").html('');
					$("[name=popup_kode]").append('<option value="" kode="">Data Kosong</option>');
				}
			}
		})
	}


	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id]").val();
		var id_barang = $("[name=popup_kode]").val();
		var nama_barang = $("[name=popup_kode] :selected").attr('barang');
		var kode = $("[name=popup_kode] :selected").attr('kode');
		var id_gudang = $("[name=gudang]").val();
		var nama_gudang = $("[name=gudang] :selected").attr('nama');
		var ket = $("[name=popup_ket]").val();
		var stok = $("[name=popup_stok]").val();
		var fisik = $("[name=popup_fisik]").val();

		var warna = $("[name=popup_kode] :selected").attr('nama_komposisi');
		var corak = $("[name=popup_kode] :selected").attr('nama_corak');
		var motif = $("[name=popup_kode] :selected").attr('nama_motif');

		var id_komposisi = $("[name=popup_kode] :selected").attr('komposisi');
		var id_proses = $("[name=popup_kode] :selected").attr('proses');
		var id_corak = $("[name=popup_kode] :selected").attr('corak');
		var id_motif = $("[name=popup_kode] :selected").attr('motif');
		var id_kualitas = $("[name=popup_kode] :selected").attr('kualitas');
		var id_dudul = $("[name=popup_kode] :selected").attr('dudul');
		var id_label = $("[name=popup_kode] :selected").attr('label');
		var id_kemasan = $("[name=popup_kode] :selected").attr('kemasan');
		var id_kemassatuan = $("[name=popup_kode] :selected").attr('kemassatuan');
		var id_kemassepuluh = $("[name=popup_kode] :selected").attr('kemassepuluh');
		var id_kemasseratus = $("[name=popup_kode] :selected").attr('kemasseratus');
		var id_workorder = $("[name=popup_kode] :selected").attr('workorder');
		var id_workorder_barang = $("[name=popup_kode] :selected").attr('workorder_barang');
		var id_satuan = $("[name=popup_kode] :selected").attr('satuan');
		var id_cap = $("[name=popup_kode] :selected").attr('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');

		var total = parseFloat(fisik)-parseFloat(stok);
		if(id_barang != '' && fisik != ''){
			if(id_data == ''){
				table.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_gudang'+tb_no+'">'+nama_gudang+'</div>',
					'<div id="list_warna'+tb_no+'">'+warna+'</div>',
					'<div id="list_corak'+tb_no+'">'+corak+'</div>',
					'<div id="list_motif'+tb_no+'">'+motif+'</div>',
					'<div id="list_stok'+tb_no+'">'+stok+'</div>',
					'<div id="list_fisik'+tb_no+'">'+fisik+'</div>',
					'<div id="list_total'+tb_no+'">'+total+'</div>',
					'<div id="list_ket'+tb_no+'">'+ket+'</div>',
					'<div class="btn-group"><button type="button" class="btn bg-warning-400 btn-xs" onclick="edit_table('+tb_no+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800 btn-xs" 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_idbarang'+tb_no+'" name="tabel_idbarang[]" value="'+id_barang+'">'+
					'<input type="hidden" id="tabel_namabarang'+tb_no+'" name="tabel_namabarang[]" value="'+nama_barang+'">'+
					'<input type="hidden" id="tabel_idgudang'+tb_no+'" name="tabel_idgudang[]" value="'+id_gudang+'">'+
					'<input type="hidden" id="tabel_namagudang'+tb_no+'" name="tabel_namagudang[]" value="'+nama_gudang+'">'+
					'<input type="hidden" id="tabel_stok'+tb_no+'" name="tabel_stok[]" value="'+stok+'">'+
					'<input type="hidden" id="tabel_fisik'+tb_no+'" name="tabel_fisik[]" value="'+fisik+'">'+
					'<input type="hidden" id="tabel_selisih'+tb_no+'" name="tabel_selisih[]" value="'+total+'">'+
					'<input type="hidden" id="tabel_ket'+tb_no+'" name="tabel_ket[]" value="'+ket+'">'+

					'<input type="hidden" id="tabel_komposisi'+tb_no+'" name="tabel_komposisi[]" value="'+id_komposisi+'">'+
					'<input type="hidden" id="tabel_proses'+tb_no+'" name="tabel_proses[]" value="'+id_proses+'">'+
					'<input type="hidden" id="tabel_corak'+tb_no+'" name="tabel_corak[]" value="'+id_corak+'">'+
					'<input type="hidden" id="tabel_motif'+tb_no+'" name="tabel_motif[]" value="'+id_motif+'">'+
					'<input type="hidden" id="tabel_kualitas'+tb_no+'" name="tabel_kualitas[]" value="'+id_kualitas+'">'+
					'<input type="hidden" id="tabel_dudul'+tb_no+'" name="tabel_dudul[]" value="'+id_dudul+'">'+
					'<input type="hidden" id="tabel_label'+tb_no+'" name="tabel_label[]" value="'+id_label+'">'+
					'<input type="hidden" id="tabel_kemasan'+tb_no+'" name="tabel_kemasan[]" value="'+id_kemasan+'">'+
					'<input type="hidden" id="tabel_kemassatuan'+tb_no+'" name="tabel_kemassatuan[]" value="'+id_kemassatuan+'">'+
					'<input type="hidden" id="tabel_kemassepuluh'+tb_no+'" name="tabel_kemassepuluh[]" value="'+id_kemassepuluh+'">'+
					'<input type="hidden" id="tabel_kemasseratus'+tb_no+'" name="tabel_kemasseratus[]" value="'+id_kemasseratus+'">'+
					'<input type="hidden" id="tabel_workorder'+tb_no+'" name="tabel_workorder[]" value="'+id_workorder+'">'+
					'<input type="hidden" id="tabel_workorder_barang'+tb_no+'" name="tabel_workorder_barang[]" value="'+id_workorder_barang+'">'+
					'<input type="hidden" id="tabel_satuan'+tb_no+'" name="tabel_satuan[]" value="'+id_satuan+'">'+
					'<input type="hidden" id="tabel_cap'+tb_no+'" name="tabel_cap[]" value="'+id_cap+'">'+
					'<input type="hidden" id="tabel_idkualitas_proses'+tb_no+'" name="tabel_idkualitas_proses[]" value="'+id_kual_proses+'">'+
					'<input type="hidden" id="tabel_idkualitas_periksa'+tb_no+'" name="tabel_idkualitas_periksa[]" value="'+id_kual_periksa+'">'+
					'<input type="hidden" id="tabel_idrencana'+tb_no+'" name="tabel_idrencana[]" value="'+id_rencana+'">'+
					'<input type="hidden" id="tabel_jumlahrencana'+tb_no+'" name="tabel_jumlahrencana[]" value="'+jumlah_rencana+'">'
					]).draw(false);
			}else{
				$("#list_kode"+id_data).text(kode);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_gudang"+id_data).text(nama_gudang);
				$("#list_stok"+id_data).text(stok);
				$("#list_fisik"+id_data).text(fisik);
				$("#list_total"+id_data).text(total);
				$("#list_ket"+id_data).text(ket);

				$("#list_warna"+id_data).text(warna);
				$("#list_corak"+id_data).text(corak);
				$("#list_motif"+id_data).text(motif);

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);
				$("#tabel_namabarang"+id_data).val(nama_barang);				
				$("#tabel_idgudang"+id_data).val(id_gudang);
				$("#tabel_namagudang"+id_data).val(nama_gudang);				
				$("#tabel_stok"+id_data).val(stok);
				$("#tabel_fisik"+id_data).val(fisik);
				$("#tabel_selisih"+id_data).val(total);
				$("#tabel_ket"+id_data).val(ket);

				$("#tabel_komposisi"+id_data).val(id_komposisi);
				$("#tabel_proses"+id_data).val(id_proses);
				$("#tabel_corak"+id_data).val(id_corak);
				$("#tabel_motif"+id_data).val(id_motif);
				$("#tabel_kualitas"+id_data).val(id_kualitas);
				$("#tabel_dudul"+id_data).val(id_dudul);
				$("#tabel_label"+id_data).val(id_label);
				$("#tabel_kemasan"+id_data).val(id_kemasan);
				$("#tabel_kemassatuan"+id_data).val(id_kemassatuan);
				$("#tabel_kemassepuluh"+id_data).val(id_kemassepuluh);
				$("#tabel_kemasseratus"+id_data).val(id_kemasseratus);
				$("#tabel_workorder"+id_data).val(id_workorder);
				$("#tabel_workorder_barang"+id_data).val(id_workorder_barang);
				$("#tabel_satuan"+id_data).val(id_satuan);
				$("#tabel_cap"+id_data).val(id_cap);
				$("#tabel_idkualitas_proses"+id_data).val(id_kual_proses);
				$("#tabel_idkualitas_periksa"+id_data).val(id_kual_periksa);
				$("#tabel_idrencana"+id_data).val(id_rencana);
				$("#tabel_jumlahrencana"+id_data).val(jumlah_rencana);

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

	function get_edit(id){
		$.ajax({
			url: "{{ url('opname/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){
						console.log(respon[i].id_opname_barang);
						table.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id_opname_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_opname_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_gudang'+respon[i].id_opname_barang+'">'+respon[i].nama_gudang+'</div>',
							'<div id="list_warna'+respon[i].id_opname_barang+'">'+respon[i].nama_komposisi+'</div>',
							'<div id="list_corak'+respon[i].id_opname_barang+'">'+respon[i].nama_corak+'</div>',
							'<div id="list_motif'+respon[i].id_opname_barang+'">'+respon[i].nama_motif+'</div>',

							'<div id="list_stok'+respon[i].id_opname_barang+'">'+respon[i].jumlah_stok+'</div>',
							'<div id="list_fisik'+respon[i].id_opname_barang+'">'+respon[i].jumlah_opname+'</div>',
							'<div id="list_total'+respon[i].id_opname_barang+'">'+respon[i].jumlah_selisih+'</div>',
							'<div id="list_ket'+respon[i].id_opname_barang+'">'+respon[i].keterangan+'</div>',
							'<div class="btn-group"><button type="button" class="btn bg-warning-400 btn-xs" onclick="edit_table('+respon[i].id_opname_barang+')"><i class="icon-pencil"></i> </button>'+
							'<button class="btn bg-danger-800 btn-xs" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>'+
							'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon[i].id_opname_barang+'" value="'+respon[i].id_opname_barang+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id_opname_barang+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_namabarang[]" id="tabel_namabarang'+respon[i].id_opname_barang+'" value="'+respon[i].nama_barang+'">'+
							'<input type="hidden" name="tabel_idgudang[]" id="tabel_idgudang'+respon[i].id_opname_barang+'" value="'+respon[i].id_gudang+'">'+
							'<input type="hidden" name="tabel_namagudang[]" id="tabel_namagudang'+respon[i].id_opname_barang+'" value="'+respon[i].nama_gudang+'">'+
							'<input type="hidden" name="tabel_stok[]" id="tabel_stok'+respon[i].id_opname_barang+'" value="'+respon[i].jumlah_stok+'">'+
							'<input type="hidden" name="tabel_fisik[]" id="tabel_fisik'+respon[i].id_opname_barang+'" value="'+respon[i].jumlah_opname+'">'+
							'<input type="hidden" name="tabel_selisih[]" id="tabel_selisih'+respon[i].id_opname_barang+'" value="'+respon[i].jumlah_selisih+'">'+
							'<input type="hidden" name="tabel_ket[]" id="tabel_ket'+respon[i].id_opname_barang+'" value="'+respon[i].keterangan+'">'+

							'<input type="hidden" id="tabel_komposisi'+respon[i].id_opname_barang+'" name="tabel_komposisi[]" value="'+respon[i].id_komposisi+'">'+
							'<input type="hidden" id="tabel_proses'+respon[i].id_opname_barang+'" name="tabel_proses[]" value="'+respon[i].id_proses+'">'+
							'<input type="hidden" id="tabel_corak'+respon[i].id_opname_barang+'" name="tabel_corak[]" value="'+respon[i].id_corak+'">'+
							'<input type="hidden" id="tabel_motif'+respon[i].id_opname_barang+'" name="tabel_motif[]" value="'+respon[i].id_motif+'">'+
							'<input type="hidden" id="tabel_kualitas'+respon[i].id_opname_barang+'" name="tabel_kualitas[]" value="'+respon[i].id_kualitas+'">'+
							'<input type="hidden" id="tabel_dudul'+respon[i].id_opname_barang+'" name="tabel_dudul[]" value="'+respon[i].id_kualitas_dudul+'">'+
							'<input type="hidden" id="tabel_label'+respon[i].id_opname_barang+'" name="tabel_label[]" value="'+respon[i].id_label+'">'+
							'<input type="hidden" id="tabel_kemasan'+respon[i].id_opname_barang+'" name="tabel_kemasan[]" value="'+respon[i].id_kemasan+'">'+
							'<input type="hidden" id="tabel_kemassatuan'+respon[i].id_opname_barang+'" name="tabel_kemassatuan[]" value="'+respon[i].id_kemasansatuan+'">'+
							'<input type="hidden" id="tabel_kemassepuluh'+respon[i].id_opname_barang+'" name="tabel_kemassepuluh[]" value="'+respon[i].id_kemasansepuluh+'">'+
							'<input type="hidden" id="tabel_kemasseratus'+respon[i].id_opname_barang+'" name="tabel_kemasseratus[]" value="'+respon[i].id_kemasanseratus+'">'+
							'<input type="hidden" id="tabel_workorder'+respon[i].id_opname_barang+'" name="tabel_workorder[]" value="'+respon[i].id_workorder+'">'+
							'<input type="hidden" id="tabel_workorder_barang'+respon[i].id_opname_barang+'" name="tabel_workorder_barang[]" value="'+respon[i].id_workorder_barang+'">'+
							'<input type="hidden" id="tabel_satuan'+respon[i].id_opname_barang+'" name="tabel_satuan[]" value="'+respon[i].id_satuan+'">'+
							'<input type="hidden" id="tabel_cap'+respon[i].id_opname_barang+'" name="tabel_cap[]" value="'+respon[i].id_cap+'">'+
							'<input type="hidden" id="tabel_idkualitas_proses'+respon[i].id_opname_barang+'" name="tabel_idkualitas_proses[]" value="'+respon[i].id_kualitas_proses+'">'+
							'<input type="hidden" id="tabel_idkualitas_periksa'+respon[i].id_opname_barang+'" name="tabel_idkualitas_periksa[]" value="'+respon[i].id_kualitas_periksa+'">'+

							'<input type="hidden" id="tabel_idrencana'+respon[i].id_opname_barang+'" name="tabel_idrencana[]" value="'+respon[i].id_perubahansatuan+'">'+
							'<input type="hidden" id="tabel_jumlahrencana'+respon[i].id_opname_barang+'" name="tabel_jumlahrencana[]" value="'+respon[i].jumlah_rencana+'">'
							]).draw(false);
					}

					get_selisih();
				}
				
			}
		})
	}

	function edit_table(id){
		var id_data = '{{$data['data']['id_opname']}}';
		var id_table = $("[name=popup_id]").val();
		var id_barang = $("#tabel_idbarang"+id).val();
		var stok = $("#tabel_stok"+id).val();
		var fisik = $("#tabel_fisik"+id).val();
		var selisih = $("#tabel_selisih"+id).val();
		var ket = $("#tabel_ket"+id).val();
		var id_gudang = $("#tabel_idgudang"+id).val();

		var gudang = '{{ $data['data']['id_gudang'] }}';
		
		if(gudang == ''){
			gudang = $("[name=gudang]").val();
		}

		var komposisi = $("#tabel_komposisi"+id).val();
		var proses = $("#tabel_proses"+id).val();
		var corak = $("#tabel_corak"+id).val();
		var motif = $("#tabel_motif"+id).val();
		var kualitas = $("#tabel_kualitas"+id).val();
		var dudul = $("#tabel_dudul"+id).val();
		var label = $("#tabel_label"+id).val();
		var kemasan = $("#tabel_kemasan"+id).val();
		var kemassatuan = $("#tabel_kemassatuan"+id).val();
		var kemassepuluh = $("#tabel_kemassepuluh"+id).val();
		var kemasseratus = $("#tabel_kemasseratus"+id).val();
		var workorder = $("#tabel_workorder"+id).val();
		var workorder_barang = $("#tabel_workorder_barang"+id).val();
		var satuan = $("#tabel_satuan"+id).val();
		var cap = $("#tabel_cap"+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_jumlahrencana"+id).val();
        var id_satuan = $("#tabel_satuan"+id).val();

        

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

		  setTimeout(function () {
		    r.resolve();
		  }, 4000);
		  return r;
		};

		var FunctionSecond = function () {
		  var r = $.Deferred();
			
			func_2();

			setTimeout(function () {
		    r.resolve();
		  }, 3500);
		  return r;
		};	

		function func_2(){

		if(id_data != '' && id != ''){

			var id_ren = id_rencana;
			var jumlah_ren = jumlah_rencana;
		  	
		  	$.ajax({
				url: '{{ url('opname/get_barangdetail') }}',
				type: 'post',
				dataType: 'json',
				data: {id : id_gudang, id_barang : id_barang, id_komposisi:komposisi,id_corak:corak, id_kualitas:kualitas, id_dudul:dudul, id_motif:motif, id_label:label, id_kemasan:kemasan, id_kemasansatuan:kemassatuan, id_kemasansepuluh:kemassepuluh, id_kemasanseratus:kemasseratus, id_proses:proses, spk:workorder, workorder:workorder_barang, id_cap : 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(result){
					//	var jum = $("[name=popup_kode]").find("option[komposisi='" + komposisi + "'][corak='" + corak + "'][motif='" + motif + "'][kualitas='" + kualitas + "'][dudul='" + dudul + "'][label='" + label + "'][kemasan='" + kemasan + "'][kemassatuan='" + kemassatuan + "'][kemassepuluh='" + kemassepuluh + "'][kemasseratus='" + kemasseratus + "'][workorder='" + workorder + "'][workorder_barang='" + workorder_barang + "'][value='"+ id_barang +"'][satuan='"+id_satuan+"'][proses='"+proses+"'][cap='"+cap+"'][id_kual_proses='"+id_kualitas_proses+"'][id_kual_periksa='"+id_kualitas_periksa+"'][rencana='"+id_rencana+"'][jumlah_rencana='"+jumlah_rencana+"'] ");

					var test =	$("[name=popup_kode]").find("option[komposisi='" + komposisi + "'][proses='" + proses + "'][corak='" + corak + "'][motif='" + motif + "'][kualitas='" + kualitas + "'][dudul='" + dudul + "'][label='" + label + "'][kemasan='" + kemasan + "'][kemassatuan='" + kemassatuan + "'][kemassepuluh='" + kemassepuluh + "'][kemasseratus='" + kemasseratus + "'][workorder='" + workorder + "'][workorder_barang='" + workorder_barang + "'][satuan='" + satuan + "'][value='"+ id_barang +"'][cap='"+ cap +"'][id_kual_proses='"+id_kualitas_proses+"'][id_kual_periksa='"+id_kualitas_periksa+"'][rencana='"+id_ren+"'][jumlah_rencana='"+jumlah_ren+"']");

					console.log(komposisi+" - "+proses+" - "+corak+" - "+motif+" - "+kualitas+" - "+dudul+" - "+label+" - "+kemasan+" - "+kemassatuan+" - "+kemassepuluh+" - "+kemasseratus+" - "+workorder+" - "+workorder_barang+" - "+satuan+" - "+id_barang+" - "+id_kualitas_proses+" - "+id_kualitas_periksa+" - "+id_ren+" - "+jumlah_ren);

					console.log(test.length);
						
						if(test.length == '0'){
							for(i in result){
								var id_nk = '', nk = '', komp = '';
								if(result[i].nama_komposisi != ''){
									id_nk = result[i].id_komposisi;
									nk = ' - '+result[i].nama_komposisi+' ( '+result[i].inisial_komposisi+' )';
									komp = result[i].nama_komposisi+' ( '+result[i].inisial_komposisi+' )';
								}

								var id_np = '', np = '';
								if(result[i].nama_proses != ''){
									id_np = result[i].id_proses;
									np = ' - '+result[i].nama_proses;
								}

								var id_nc = '', nc = '', ncr = '';
								if(result[i].nama_corak != ''){
									id_nc = result[i].id_corak;
									nc = ' - '+result[i].nama_corak;
									ncr = result[i].nama_corak;
								}
								
								var id_nm = '', nm = '', nmt = '';
								if(result[i].nama_motif != ''){
									id_nm = result[i].id_motif;
									nm = ' - '+result[i].nama_motif;
									nmt = result[i].nama_motif;
								}

								var id_k = '', nama_kual = '';
								if(result[i].id_kualitas != ''){
		                           id_k = result[i].id_kualitas; 
		                           nama_kual = ' - '+result[i].nama_kualitas; 
		                        }

		                        var id_dudul = '', nama_dudul = '';
		                        if(result[i].id_kualitas_dudul != ''){
		                           	id_dudul = result[i].id_kualitas_dudul;
		                           	nama_dudul = ' - Dudulan '+result[i].kualitas_dudul;
		                        }

		                        var id_label = '', nama_label = '';
		                        if(result[i].id_label !=''){
		                           	id_label = result[i].id_label;
		                           	nama_label = ' - '+result[i].nama_label;
		                        }

		                        var id_kemas = '', nama_kemas = '';
		                        if(result[i].id_kemasan != ''){
		                           	id_kemas = result[i].id_kemasan;
		                           	nama_kemas = ' - '+result[i].nama_kemasan;
		                        }

		                        var id_kemassatuan = '', nama_kemassatuan = '';
		                        if(result[i].id_kemasansatuan != ''){
		                           	id_kemassatuan = result[i].id_kemasansatuan;
		                           	nama_kemassatuan = ' - '+result[i].nama_kemasansatuan;
		                        }

		                        var id_kemassepuluh = '', nama_kemassepuluh = '';
		                        if(result[i].id_kemasansepuluh != ''){
		                           	id_kemassepuluh = result[i].id_kemasansepuluh;
		                           	nama_kemassepuluh = ' - '+result[i].nama_kemasansepuluh;
		                        }

		                        var id_kemasseratus = '', nama_kemasseratus = '';
		                        if(result[i].id_kemasanseratus != ''){
		                           	id_kemasseratus = result[i].id_kemasanseratus;
		                           	nama_kemasseratus = ' - '+result[i].nama_kemasanseratus;
		                        }

		                        var id_workorder = '', no_workorder = '';
		                        if(result[i].id_workorder != ''){
		                            id_workorder = result[i].id_workorder;
		                            no_workorder = ' - '+result[i].no_workorder;
		                        }

		                        var id_workorder_barang = '', no_workorder_barang = '';
		                        if(result[i].id_workorder_barang != ''){
		                            id_workorder_barang = result[i].id_workorder_barang;
		                            no_workorder_barang = ' - '+result[i].no_workorder_barang;
		                        }

		                        var id_satuan = '', nama_satuan = '';
		                        if(result[i].id_satuan != ''){
		                            id_satuan = result[i].id_satuan;
		                            nama_satuan = ' - '+result[i].nama_satuan;
		                        }

		                        var id_cap = '', nama_cap = '';
		                        if(result[i].id_cap != ''){
		                            id_cap = result[i].id_cap;
		                            nama_cap = ' - '+result[i].nama_cap;
		                        }

		                        var id_kual_proses = '', nama_kual_proses = '', id_kual_periksa = '', nama_kual_periksa = '';

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

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

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

								

								$("[name=popup_kode]").append('<option value="'+result[i].id_barang+'" stok="'+result[i].stok+'"  kode="'+result[i].kode_barang+'" satuan="'+result[i].id_satuan+'" nama_satuan="'+result[i].nama_satuan+'" barang="'+result[i].nama_barang+'" komposisi="'+id_nk+'" proses="'+id_np+'" corak="'+id_nc+'" motif="'+id_nm+'" kualitas="'+id_k+'" dudul="'+id_dudul+'" label="'+id_label+'" kemasan="'+id_kemas+'" kemassatuan="'+id_kemassatuan+'" kemassepuluh="'+id_kemassepuluh+'" kemasseratus="'+id_kemasseratus+'" workorder="'+id_workorder+'" workorder_barang="'+id_workorder_barang+'" cap="'+id_cap+'" id_kual_proses="'+id_kual_proses+'" id_kual_periksa="'+id_kual_periksa+'" rencana="'+id_rencana+'" jumlah_rencana="'+jumlah_rencana+'" nama_komposisi="'+komp+'" nama_corak="'+ncr+'" nama_motif="'+nmt+'">'+result[i].concat_nama+'</option>');

								
							}
						}
					},
					complete: function(){
						FunctionTwo();
					}
				})
			}else{
				FunctionTwo();
			}
		}

	//	console.log(komposisi+" - "+proses+" - "+corak+" - "+motif+" - "+kualitas+" - "+dudul+" - "+label+" - "+kemasan+" - "+kemassatuan+" - "+kemassepuluh+" - "+kemasseratus+" - "+workorder+" - "+workorder_barang+" - "+satuan+" - "+id_barang+" - "+id_kualitas_proses+" - "+id_kualitas_periksa+" - "+id_rencana+" - "+jumlah_rencana);

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

			var test =	$("[name=popup_kode]").find("option[komposisi='" + komposisi + "'][proses='" + proses + "'][corak='" + corak + "'][motif='" + motif + "'][kualitas='" + kualitas + "'][dudul='" + dudul + "'][label='" + label + "'][kemasan='" + kemasan + "'][kemassatuan='" + kemassatuan + "'][kemassepuluh='" + kemassepuluh + "'][kemasseratus='" + kemasseratus + "'][workorder='" + workorder + "'][workorder_barang='" + workorder_barang + "'][satuan='" + satuan + "'][value='"+ id_barang +"'][cap='"+ 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(test.length);

			var nama_satuan = $("[name=popup_kode] :selected").attr('nama_satuan');

			$("[name=popup_ddd_stok]").html(stok+' '+nama_satuan);
			$("[name=popup_stok]").val(stok);
			$("[name=popup_fisik]").val(fisik);
			$("[name=popup_selisih]").val(selisih);
			if(selisih < 0){
				$("[name=popup_ddd_selisih]").text(selisih).css({'color':'red'});
				$("[name=popup_selisih]").val(selisih);
			}else{
				$("[name=popup_ddd_selisih]").text(selisih).css({'color':'#333'});
				$("[name=popup_selisih]").val(selisih);
			}
			$("[name=popup_ket]").val(ket);
			$("#exampleModal").modal('show');
		};

		FunctionOne().done(FunctionSecond);
		

	//	Function_().done(FunctionTwo);
		get_selisih();
	}

	function hapus(d){
		table.row(d.parents('tr')).remove().draw();
		
	}



	/*$("[name=gudang]").change(function(){
		var gudang = $("[name=gudang]").val();
		$.ajax({
			url: "{{ url('opname/get_barang')}}",
			type: "post",
			data: {id : gudang},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				table.clear().draw();
				if(respon.length > 0){
				for(i in respon){
					if(respon[i].stok > 0){
						table.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_gudang'+respon[i].id_barang+'">'+respon[i].nama_gudang+'</div>',
							'<div id="list_stok'+respon[i].id_barang+'">'+respon[i].stok+'</div>',
							'<div><input type="text" class="form-control tabel_fisik" name="tabel_fisik[]" id="tabel_fisik'+respon[i].id_barang+'"></div>',
							'<div><input type="text" class="form-control" name="tabel_selisih[]" id="tabel_selisih'+respon[i].id_barang+'" readonly></div>'+
							'<div><input type="hidden" name="tabel_idbarang[]" class="tabel_idbarang" id="tabel_idbarang'+respon[i].id_barang+'" value="'+respon[i].id_barang+'"></div>'+
							'<div><input type="hidden" name="tabel_idgudang[]" class="tabel_idgudang" id="tabel_idgudang'+respon[i].id_barang+'" value="'+respon[i].id_gudang+'"></div>'+
							'<div><input type="hidden" name="tabel_stok[]" class="tabel_stok" id="tabel_stok'+respon[i].id_barang+'" value="'+respon[i].stok+'"></div>'+
							'<div><input type="hidden" name="tabel_id[]" class="tabel_id" id="tabel_id'+respon[i].id_barang+'" value="'+respon[i].id_barang+'"></div>'
							]).draw(false);
					}
						$("#tabel_fisik"+respon[i].id_barang).keyup(function(){
								var fisik = $(this).parents('tr').find('.tabel_fisik').val();
								var stok = $(this).parents('tr').find('.tabel_stok').val();
								var id = $(this).parents('tr').find('.tabel_id').val();
								get_selisih(fisik,stok,id);
								console.log(fisik,stok,id);
						});
				}
			}else{
				
			 }
		  }
		})
	})*/

	/*function get_selisih(fisik,stok,id){
		var s_fisik = parseFloat(fisik);
		var s_stok = parseFloat(stok);
		if(isNaN(s_fisik) || s_fisik == 0 || s_fisik == ""){
			s_fisik = parseFloat(0);
		} 
		if(isNaN(s_stok) || s_stok == 0 || s_stok == ""){
			s_stok = parseFloat(0);
		} 

		var selisih = s_fisik-s_stok;

		$("#tabel_selisih"+id).val(selisih);
	}*/
</script>

@endsection