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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Bon Bahan (ATM)</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_atm/simpan')}}" method="post" autocomplete="off" enctype="multipart/form-data">
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_barangkeluar" value="{{ $data['data']['id_barangkeluar']}}">
        	<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 class="form-control datepicker" type="text" name="tanggal" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):date('d-m-Y', strtotime($data['data']['tanggal'])) }}" placeholder="Tanggal" required/>
        			</div>
        		</div>
        		
        		<div class="form-group">
        			<label class="control-label col-sm-4">No.Order</label>
        			<div class="col-sm-8">
        				@php if($data['data']['id_barangkeluar']!=""){@endphp
        				<input type="hidden" name="workorder" value="{{($data['data']['id_workorder_barang'] != '0') ? $data['data']['id_workorder_barang']:''}}">
        				<select class="form-control select-search" disabled="">
        					<option value="">-- Pilih Order --</option>
        					@php foreach($data['workorder'] as $row){ 
        					$sel = $data['data']['id_workorder_barang'];
        					@endphp
        					<option value="{{ $row->id_workorder_barang }}" order="{{ $row->id_workorder }}" {{ ($row->id_workorder_barang == $sel) ? "selected":"" }}  > {{ $row->no_workorder }}
        					</option>
        					@php } @endphp
        				</select>
        				@php }else{ @endphp
        				<select class="form-control select-search" name="workorder">
        					<option value="">-- Pilih Order --</option>
        					@php foreach($data['workorder'] as $row){ 
        					$sel = $data['data']['id_workorder_barang'];
        					@endphp
        					<option value="{{ $row->id_workorder_barang }}" order="{{ $row->id_workorder }}" {{ ($row->id_workorder_barang == $sel) ? "selected":"" }}  > {{ $row->no_workorder }}
        					</option>
        					@php } @endphp
        				</select>
        				@php } @endphp
        			</div>
        		</div>
        		
        		<div class="form-group">
        			<label class="control-label col-sm-4">Proses Produksi</label>
        			<div class="col-sm-8">
        				@php if($data['data']['id_barangkeluar']!=""){ @endphp
        				<input type="hidden" name="proses" value="{{$data['data']['id_proses']}}">
        				<select class="form-control select-search" disabled="">
        					<option value="">-- Pilih Produksi --</option>
        					@php foreach($data['proses'] as $row){ 
        					$sel = $data['data']['id_proses'];
        					$old = old('proses');
        					if($row->nama_proses2 == ''){

        					echo '<optgroup label="'.$row->nama_proses.'">';
        					} else {
        					@endphp
        					<option value="{{ $row->id_proses }}" {{ ($row->id_proses == $sel) ? "selected":"" }}  > {{ $row->nama_proses2.' - '.$row->nama_proses }}
        					</option>
        					@php
        					}
        					if($row->nama_proses == ''){

        					echo '</optgroup>';
        					}

        					} @endphp
        				</select>
        				@php }else{ @endphp
        				<select class="form-control select-search" name="proses" required>
        					<option value="">-- Pilih Produksi --</option>
        					@php foreach($data['proses'] as $row){ 
        					$sel = $data['data']['id_proses'];
        					$old = old('proses');
        					
        					if($row->nama_proses2 == ''){

        					echo '<optgroup label="'.$row->nama_proses.'">';
        					} else {
        					@endphp
        					<option value="{{ $row->id_proses }}" {{ ($row->id_proses == $sel) ? "selected":"" }}  > {{ $row->nama_proses2.' - '.$row->nama_proses }}
        					</option>
        					@php
        					}
        					if($row->nama_proses == ''){

        					echo '</optgroup>';
        					}

        					} @endphp
        				</select>
        				@php } @endphp
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Perkiraan Selesai</label>
        			<div class="col-sm-8">
        				<input class="form-control datepicker" type="text" name="tanggal_perkiraan" value="{{ ($data['data']['tanggal_perkiraan'] == '') ? old('tanggal_perkiraan') : date('d-m-Y',strtotime($data['data']['tanggal_perkiraan'])) }}" placeholder="Tanggal" required/>
        			</div>
        		</div>
                <div class="form-group">
                    <label class="control-label col-sm-4">Lokasi Produksi </label>
                    <div class="col-sm-8">
                        <select class="form-control select-search" name="id_jenis">
                            @php $sel = $data['data']['jenis'];
                            foreach($data['produksi'] as $d){@endphp
                            <option value="{{$d->id_lokasi}}" {{ ($d->id_lokasi == $sel) ? "selected":"" }} >{{ $d->nama_lokasi }}</option>
                            @php } @endphp
                        </select>
                    </div>
                </div>  
        	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Rekanan</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="rekanan">
        					<option value="">-- Pilih Rekanan --</option>
        					@php foreach($data['rekanan'] as $row){
        					$sel = $data['data']['id_rekanan']; @endphp
        					<option value="@php echo $row->id_rekanan @endphp" {{ ($row->id_rekanan == $sel) ? "selected":"" }}>
        						{{$row->nama.' - '.$row->nama_kelompok}}
        					</option>
        					@php } @endphp
        				</select>
        			</div>
        		</div>
                <div class="form-group">
                    <label class="control-label col-sm-4">Mesin</label>
                    <div class="col-sm-8">
                        <select name="mesin" class="form-control select-search" id="id_mesin">
                            <option value="">--- Pilih Mesin ---</option>
                            @php $no=1; foreach($data['mesin'] as $row){
                            $sel = $data['data']['id_mesin'];
                            @endphp
                                <option value="{{ $row->id_mesin }}" {{ ($row->id_mesin == $sel) ? "selected":""}}>{{ $row->nama }}</option>
                            @php } @endphp
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-4">Nomor</label>
                    <div class="col-sm-3">
                        <input type="text" name="nomor" class="form-control">
                    </div>
                    <div class="col-sm-5">
                        <input type="text" name="nomor_ext" class="form-control">
                    </div>
                </div>
        		
        		<div class="form-group" id="btnworkorder">
        			@if (old('proses'))
				    <div class="alert alert-warning">
				    	<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
				        <i><b>*Rekanan atau Pegawai belum di isi</b></i>
				    </div>
				@endif
				  
        		</div>
        	</div>
        	

        	
        	<div class="col-sm-12 form-group">
        	<h5 class="panel-title">Detail Barang</h5>
        	 <br><a href="#" class="btn btn-success" style="margin-bottom: 1%;" id="btn_tambah_item">Tambah Barang</a></div> 
        	<div class="col-ms-12">
        	<table class="table table-stripped" id="table_barang">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Tanggal</th>
					<th>Kode Barang</th>
					<th>Nama Barang</th>
					<th>Warna</th>
					<th>Gudang</th>
					<th>Jumlah</th>
					<th>Keterangan</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
        	</div>
        	
        	
             <div class="col-md-6"></div>
             <br>
             <div class="col-md-12">
             <a href="{{url('barangkeluar_atm')}}" class="btn bg-warning-400">Kembali</a>
             <div class="form-group pull-right">
             	<button class="btn btn-primary" type="submit">Simpan</button> 
             	<!-- <button class="btn btn-primary" type="submit">Simpan</button> -->
             </div>
         </div>
	    </div>


		<input type="hidden" name="td_total" class="form-control">
		<input type="hidden" name="td_netto" class="form-control">
	    </form>
	</div>
</div>

<!---Modal--->
<div class="modal fade" id="exampleModal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" autocomplete="off">
                    <input type="hidden" name="popup_iddatabase">
	      			<input type="hidden" name="popup_id_table">
	      			<div class="form-group">
                        <label class="col-sm-4 control-label">Tanggal</label>
                        <div class="col-sm-8">
                            <input type="text" name="popup_tanggal" class="form-control datepicker" value="{{ date('d-m-Y') }}" readonly>
                        </div>
                    </div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Produksi sebelumnya</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses">
	      						<option value="">-- Pilih Produksi --</option>
        					@php foreach($data['proses_popup'] as $row){ 
        					$sel = $data['data']['id_proses'];
        					$old = old('proses');
        					if($row->nama_proses2 == ''){

        					echo '<optgroup label="'.$row->nama_proses.'">';
        					} else {
        					@endphp
        					<option value="{{ $row->id_proses }}" {{ ($row->id_proses == $sel) ? "selected":"" }}  > {{ $row->nama_proses2.' - '.$row->nama_proses }}
        					</option>
        					@php
        					}
        					if($row->nama_proses == ''){

        					echo '</optgroup>';
        					}

        					} @endphp		
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Warna</label>
	      				<div class="col-sm-8">
	      					<!-- <input type="text" class="form-control" id="input_warna" readonly> -->
	      					<select class="form-control select-search" name="input_warna" id="input_warna">
	      						<option value="">-- Pilih --</option>
	      						@php foreach($data['warna'] as $d){ @endphp
	      							<option value="{{ $d->id_komposisi}}" nama="{{$d->nama}}">{{$d->nama.' ('.$d->inisial.')'}}</option>
	      						@php } @endphp
	      					</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">
	      						|<option value="">-- Pilih Gudang --</option>
	      						@php foreach($data['gudang'] as $row){
	      						
	      						@endphp
	      						<option value="{{$row->id_gudang}}" id="{{$row->id_gudang}}" nama="{{$row->nama_gudang}}">{{$row->nama_gudang}}</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_barang">
	      						
	      					</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 <!-- <b id="popup_satuanawal"></b> --></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlah" class="form-control number-only" onkeyup="maksimal($(this))">
                            <p class="help-block pull-right" id="popup_helpjumlah"></p>
	      				</div>
	      			</div>
	      			<div class="form-group" id="div_konversi2">
	      				<label class="control-label col-sm-4">Satuan Jumlah </label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_satuanawal" >
	      						<option value="">-- Pilih --</option>
	      						@php foreach($data['satuan'] as $d){@endphp
	      						<option value="{{$d->id_satuan}}" nama="{{$d->nama_satuan}}">{{$d->nama_satuan}}</option>
	      						@php } @endphp
	      					</select>		
	      				</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" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4">Perkiraan Laporan Hasil</label>
                        <div class="col-sm-8">
                            <input type="text" name="popup_jumlahhasil" class="form-control number-only">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4">Satuan Perkiraan Laporan Hasil</label>
                        <div class="col-sm-8">
                            <select class="form-control select-search" name="popup_satuanhasil" >
                                <option value="">-- Pilih --</option>
                                @php foreach($data['satuan'] as $d){@endphp
                                <option value="{{$d->id_satuan}}" nama="{{$d->nama_satuan}}">{{$d->nama_satuan}}</option>
                                @php } @endphp
                            </select>       
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">Ketarangan</label>
                        <div class="col-sm-8">
                            <textarea class="form-control" name="popup_ket"></textarea>
                        </div>
                    </div>
	      		</form>
	      	</div>
	      	<h5 id="alert_workorder"></h5>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<input type="hidden" name="store_konversi">
<input type="hidden" name="store_status" value="{{ $data['data']['status']}}">
<input type="hidden" name="store_rubahsatuan">

<!----Modal---->
<script type="text/javascript">
	var table_barang;
    var store_idbarangkeluar;
	var tb_no = parseInt(10000);
    var load_gudang = 0;
    var FunctionTwo;
	$(document).ready(function(){
		table_barang = $("#table_barang").DataTable({
            "paging": false
        });
		table_barang.on( 'order.dt search.dt', function () {
            table_barang.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();


	//	get_barang();
		get_nomor();

		store_idbarangkeluar = $("[name=id_barangkeluar]").val();
		if(store_idbarangkeluar != ''){
			get_edit(store_idbarangkeluar);
			readonly('[name=tanggal]', true);
			readonly('[name=tanggal_perkiraan]', true);
		}
		var mesin = $("[name=mesin]").val();
		if(mesin > 0){
			disabled('[name=rekanan]', true);
			$('[name=rekanan]').attr('required', false);	
		}else{
			disabled('[name=rekanan]', false);
			$('[name=rekanan]').attr('required', true);
		}

		var rekanan = $("[name=rekanan]").val();
		if(rekanan > 0){
			disabled('[name=mesin]',true);
			$('[name=mesin]').attr('required', false);
		}else{
			disabled('[name=mesin]',false);
			$('[name=mesin]').attr('required', true);
		}

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

        perkiraan_hasil();
		
	})


	$("#btn_tambah_item").click(function(){
        $("[name=popup_iddatabase]").val('');
		$("[name=popup_id_table]").val('');
		$("[name=popup_proses]").val(null).trigger('change');
		$("[name=popup_gudang]").val(null).trigger('change');
		$("[name=input_warna]").val(null).trigger('change');
		$("[name=popup_barang]").html('');
		$("[name=popup_stok]").text('');
		$("[name=popup_jumlah]").val('');
        $("[name=popup_satuanawal]").val(null).trigger('change');
        $("[name=popup_menghasilkan]").val('');
        $("[name=popup_ket]").val('');
		$("#div_konversi").addClass('hide');

		/*$("[name=popup_jumlah_konv]").val('');*/
		$("#popup_satuanawal").text('');
		$("#popup_satuanakhir").text('');
        $("#popup_helpjumlah").text('');

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

        $("[name=popup_jumlahhasil]").val('');
        $("[name=popup_satuanhasil]").val(null).trigger('change');
        
		$("#exampleModal").modal('show');
	})

		

	$("[name=mesin]").change(function(){
		var mesin = $("[name=mesin]").val();

		if(mesin > 0){
			disabled('[name=rekanan]', true);
			
		}else{
			disabled('[name=rekanan]', false);
		}
	});

	$("[name=rekanan]").change(function(){
		var rekanan = $("[name=rekanan]").val();

		if(rekanan > 0){
			disabled('[name=mesin]',true);
			
		}else{
			disabled('[name=mesin]',false);
		}
	});

	$("[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_jumlah]").keyup(function(){
        $("#popup_helpjumlah").text(format_angka($("[name=popup_jumlah]").val()));
    })

    //-------------------------- Popup kosong Tambah -----------------------------------//
    $("[name=input_warna]").change(function(){
        $("[name=popup_gudang]").val(null).trigger('change');
        $("[name=popup_barang]").val(null).trigger('change');
        $("#popup_stok").text('');
    })

    $("[name=popup_proses]").change(function(){
        $("[name=input_war]").val(null).trigger('change');
        $("[name=popup_gudang]").val(null).trigger('change');
        $("[name=popup_barang]").val(null).trigger('change');
        $("#popup_stok").text('');
    })
	
	/* -------------------------- Gudang popup tambah ---------------------------------------*/

	$("[name=popup_gudang]").change(function(){
		var id_gudang = $(this).val();
		var id_proses = $("[name=proses]").val();
		var id_proses2 = $("[name=popup_proses]").val();
		var id_komposisi = $("[name=input_warna]").val();
		
		var id_workorder = $("[name=workorder]").val();
		var id_spk = $("[name=spk]").val();
		var spk = id_spk
		if(id_spk == 0){
			spk = "";
		}
		var workorder = id_workorder;
		if(id_workorder == 0){
			workorder = "";
		}
        if(id_gudang != 0 || id_gudang != ''){
		$.ajax({
			url: '{{ url('barangkeluar_atm/get_barang') }}',
			type: 'post',
			dataType: 'json',
			data: {id : id_gudang, id_barang : '', id_proses : id_proses,id_komposisi:id_komposisi,id_proses2:id_proses2, spk:spk, workorder:workorder},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				
				$("[name=popup_barang]").html('');
				$("[name=popup_stok]").text('');
				if(respon.length > 0){
					$("[name=popup_barang]").append('<option value="">-- Pilih Barang --</option>');
					for(i in respon){
						if(respon[i].stok > 0){
								var id_corak = ''; 
								var nama_corak = '';
								var id_motif = '';
								var nama_motif = '';
                                var id_kual = '';
                                var nama_kual = '';
                                var id_dudul = '';
                                var nama_dudul = '';
                                var id_label = '';
                                var nama_label = '';
                                var id_kemasan = '';
                                var nama_kemasan = '';
                                var id_kemasansatuan = '';
                                var nama_kemasansatuan = '';
                                var id_kemasansepuluh = '';
                                var nama_kemasansepuluh = '';
                                var id_kemasanseratus = '';
                                var nama_kemasanseratus = '';
                                var id_cap = '', nama_cap = '';
								var id_kualitas_proses = '', nama_kualitas_proses = '';
                                var id_kualitas_periksa = '', nama_kualitas_periksa = '';
							if(respon[i].id_corak != ''){
								var id_corak = respon[i].id_corak; 
								var nama_corak = ' - '+respon[i].nama_corak; 
							}
							if(respon[i].id_motif != ''){
								var id_motif = respon[i].id_motif; 
								var nama_motif = ' - '+respon[i].nama_motif; 
							}
                            if(respon[i].id_kualitas != ''){
                                var id_kual = respon[i].id_kualitas; 
                                var nama_kual = ' - '+respon[i].nama_kualitas; 
                            }
                            if(respon[i].id_kualitas_dudul != ''){
                                var id_dudul = respon[i].id_kualitas_dudul;
                                var nama_dudul = ' - Dudulan '+respon[i].kualitas_dudul;
                            }

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

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

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

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

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

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

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

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

                            var id_rencana = '', nama_rencana = '', jumlah_rencana = '' ;
                            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_barang]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].id_satuan+'" corak="'+id_corak+'" motif="'+id_motif+'" kualitas="'+id_kual+'" dudulan="'+id_dudul+'" label="'+id_label+'" kemasan="'+id_kemasan+'" kemasan_satuan="'+id_kemasansatuan+'" kemasan_sepuluh="'+id_kemasansepuluh+'" kemasan_seratus="'+id_kemasanseratus+'" cap="'+id_cap+'" kualitas_proses="'+id_kualitas_proses+'" kualitas_periksa="'+id_kualitas_periksa+'" rencana="'+id_rencana+'" jumlah_rencana="'+jumlah_rencana+'" komposisi="'+respon[i].id_komposisi+'">'+respon[i].concat_nama+'</option>');
						}
					}
				}else{
					$("[name=popup_barang]").html('');
					$("[name=popup_barang]").append('<option value=""> Stok Kosong </option>');
				}
			},
            complete: function(respon){
                load_gudang = 1;
                // console.log(load_gudang);
            }
		})
        }else{
            $("[name=popup_barang]").html('');
            $("[name=popup_stok]").text('');
        }
	})

	$("[name=popup_barang]").change(function(){
        var id_tabel = $("[name=popup_iddatabase]").val();
		var id_gudang = $("[name=popup_gudang]").val();
		var id_barang = $(this).val();
		var id_corak = $("[name=popup_barang] :selected").attr('corak');
		var nilai = parseInt(0);
		var id_proses = $("[name=proses]").val();
		var id_komposisi = $("[name=input_warna]").val();
		var id_proses2 = $("[name=popup_proses]").val();
		var workorder = $("[name=workorder]").val();
        var id_kualitas = $("[name=popup_barang] :selected").attr('kualitas');
        var id_dudul = $("[name=popup_barang] :selected").attr('dudulan');
        var id_motif = $("[name=popup_barang] :selected").attr('motif');
        var id_label = $("[name=popup_barang] :selected").attr('label');
        var id_kemasan = $("[name=popup_barang] :selected").attr('kemasan');
        var id_kemasansatuan = $("[name=popup_barang] :selected").attr('kemasan_satuan');
        var id_kemasansepuluh = $("[name=popup_barang] :selected").attr('kemasan_sepuluh');
        var id_kemasanseratus = $("[name=popup_barang] :selected").attr('kemasan_seratus');
        var id_cap = $("[name=popup_barang] :selected").attr('cap');
        var id_kualitas_proses = $("[name=popup_barang] :selected").attr('kualitas_proses');
        var id_kualitas_periksa = $("[name=popup_barang] :selected").attr('kualitas_periksa');
        var id_rencana = $("[name=popup_barang] :selected").attr('rencana');
        var jumlah_rencana = $("[name=popup_barang] :selected").attr('jumlah_rencana');
        var id_satuan = $("[name=popup_barang] :selected").attr('satuan');
		
        if(id_gudang != ''){
        $.ajax({
			url: '{{ url('barangkeluar_atm/get_barang_hasil') }}',
			type: 'post',
			dataType: 'json',
			data: {id : id_gudang, id_barang : id_barang,id_proses : id_proses,id_komposisi:id_komposisi,id_proses2:id_proses2,id_corak:id_corak,workorder:workorder, 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_cap : id_cap,  id_kualitas_proses : id_kualitas_proses, id_kualitas_periksa : id_kualitas_periksa, id_rencana : id_rencana, jumlah_rencana : jumlah_rencana, id_satuan : id_satuan},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				var total = parseInt(0);
                var total_ = parseInt(0);
                        $('input[id^="tabel_jumlah"]').each(function() {
                              
                        var val = ($(this).attr('id'));
                        val = val.replace('tabel_jumlah', '');

                        if(id_proses == '18'){
                            if($("#tabel_idbarang"+val).val() == id_barang){
                            var jum = $("#tabel_jumlah"+val).val();
                            total += parseFloat(jum);
                        }   
                        }else{
                            if($("#tabel_idbarang"+val).val() == id_barang && $("#tabel_warna"+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_idcap"+val).val() == id_cap && $("#tabel_idkualitas_proses"+val).val() == id_kualitas_proses && $("#tabel_idkualitas_periksa"+val).val() == id_kualitas_periksa && $("#tabel_idrencana"+val).val() == id_rencana && $("#tabel_rencanajumlah"+val).val() == jumlah_rencana && $("#tabel_satuan"+val).val() == id_satuan){
                                
                                // var jum = $("#tabel_jumlah"+val).val();
                                // total += parseFloat(jum);

                                if($("#tabel_status"+val).val() == '0'){
                                    var jum = $("#tabel_jumlah"+val).val();
                                    total += parseFloat(jum);
                                }
                            }
                        }
                    // console.log('------------------'+val+'------------------');
                    // console.log($("#tabel_idbarang"+val).val()+' - '+$("#tabel_warna"+val).val()+' - '+$("#tabel_idcorak"+val).val()+' - '+$("#tabel_idkualitas"+val).val()+' - '+$("#tabel_idkualitas_dudul"+val).val()+' - '+$("#tabel_idmotif"+val).val()+' - '+$("#tabel_idlabel"+val).val()+' - '+$("#tabel_idkemasan"+val).val()+' - '+$("#tabel_idkemasan_satuan"+val).val()+' - '+$("#tabel_idkemasan_sepuluh"+val).val()+' - '+$("#tabel_idkemasan_seratus"+val).val()+' - '+$("#tabel_idcap"+val).val()+' - '+$("#tabel_idkualitas_proses"+val).val()+' - '+$("#tabel_idkualitas_periksa"+val).val()+' - '+$("#tabel_idrencana"+val).val()+' - '+$("#tabel_rencanajumlah"+val).val());
                    // console.log(id_barang+' - '+id_komposisi+' - '+id_corak+' - '+id_kualitas+' - '+id_dudul+' - '+id_motif+' - '+id_label+' - '+id_kemasan+' - '+id_kemasansatuan+' - '+id_kemasansepuluh+' - '+id_kemasanseratus+' - '+id_cap+' - '+id_kualitas_proses+' - '+id_kualitas_periksa+' - '+id_rencana+' - '+jumlah_rencana);
                    /*console.log($("#tabel_warna"+val).val() +' 1 '+ id_komposisi + "\n" + $("#tabel_idcorak"+val).val() +' 2 '+ id_corak + "\n" + $("#tabel_idkualitas"+val).val() +' 3 '+ id_kualitas + "\n" + $("#tabel_idkualitas_dudul"+val).val() +' 4 '+ id_dudul + "\n" + $("#tabel_idmotif"+val).val() +' 5 '+ id_motif);*/
                            
                        });
                        if(respon.length > 0){
                            if($("[name=popup_id_table]").val() == ''){
                                nilai = respon[0].stok-total;
                            }else{
                                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{
                                    var stat = $("[name=store_status]").val();
                                    if(stat == 0){
                                        nilai = parseFloat(respon[0].stok-total)+parseFloat(jum);
                                    }else{
                                        nilai = parseFloat(respon[0].stok-total)+parseFloat(jum);

                                    //    nilai = parseFloat(respon[0].stok)+(parseFloat(jum)+total);
                                    }
                                       
                                }

                                
                            }

                            console.log("stok "+respon[0].stok+"\nTotal "+total+"\nJumlah "+$("[name=popup_jumlah]").val());
                            $("[name=popup_stok]").text(nilai+' '+respon[0].nama_satuan);

                            $("[name=popup_jumlah]").attr('min', 0);
                            $("[name=popup_jumlah]").attr('max', nilai);

                            $("[name=popup_satuanawal]").val(respon[0].id_satuan).trigger('change');

                            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_id_table]").val() != ''){
                                    popup_jumlah($("[name=popup_id_table]").val());
                                }
                                
                            }else{
                                $("[name=popup_menghasilkan]").val('');
                                $("#satuan_menghasilkan").text('');
                                $("#div_menghasilkan").hide();
                            }
                            /*$("[name=popup_jumlah_konv]").attr('min', 0);
                            $("[name=popup_jumlah_konv]").attr('max', nilai*$("[name=store_konversi]").val());*/
                        }
			}
		})
        }
	})

	function get_barang(){
		var id_proses = $("[name=proses]").val();
		var id_gudang = $("[name=gudang]").val();
		var id_proses2 = $("[name=popup_proses]").val();
		var workorder = $("[name=workorder]").val();
		var spk = $("[name=spk]").val();
		var id_corak = $("[name=popup_barang] :selected").attr('corak');
		var id_komposisi = $("#input_warna").val();
        var id_motif = $("[name=popup_barang] :selected").attr('motif');
        var id_kualitas = $("[name=popup_barang] :selected").attr('kualitas');
        var id_dudul = $("[name=popup_barang] :selected").attr('dudulan');
        var id_label = $("[name=popup_barang] :selected").attr('label');
        var id_kemasan = $("[name=popup_barang] :selected").attr('kemasan');
        var id_kemasansatuan = $("[name=popup_barang] :selected").attr('kemasan_satuan');
        var id_kemasansepuluh = $("[name=popup_barang] :selected").attr('kemasan_sepuluh'); 
        var id_kemasanseratus = $("[name=popup_barang] :selected").attr('kemasan_seratus');
        var id_cap = $("[name=popup_barang] :selected").attr('id_cap');
        var id_kualitas_proses = $("[name=popup_barang] :selected").attr('id_kualitas_proses');
        var id_kualitas_periksa = $("[name=popup_barang] :selected").attr('id_kualitas_periksa');
		$.ajax({
			url: '{{ url('barangkeluar/get_barang_proses') }}',
			type: 'post',
			dataType: 'json',
			data:  {id_proses:id_proses,id_gudang:id_gudang,id_proses2:id_proses2, workorder:workorder,spk:spk,id_komposisi:id_komposisi,spk:spk},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				$("[name=popup2_barang]").html('');
				$("[name=popup2_stok]").text('');
				$("[name=popup3_barang]").html('');
				$("[name=popup3_stok]").text('');
				if(respon.length > 0){
					$("[name=popup2_barang]").append('<option value=""> -- Pilih Barang --</option>');
					$("[name=popup3_barang]").append('<option value=""> -- Pilih Barang --</option>');
					for(i in respon){
								var id_corak = ''; 
								var nama_corak = '';
								var id_motif = '';
								var nama_motif = '';
                                var id_kual = '';
                                var nama_kual = '';
                                var id_dudul = '';
                                var nama_dudul = '';
                                var id_label = '';
                                var nama_label = '';
                                var id_kemasan = '';
                                var nama_kemasan = '';
                                var id_kemasansatuan = '';
                                var nama_kemasansatuan = '';
                                var id_kemasansepuluh = '';
                                var nama_kemasansepuluh = '';
                                var id_kemasanseratus = '';
                                var nama_kemasanseratus = '';
                                var id_cap = '', nama_cap = '';
                                var id_kualitas_proses = '', nama_kualitas_proses = '';
                                var id_kualitas_periksa = '', nama_kualitas_periksa = '';
                                var id_rencana = '';
                                var nama_satuanrencana = '';
                                var nama_rencana = '';
                                var jumlah_rencana = '';
							if(respon[i].id_corak != ''){
								var id_corak = respon[i].id_corak; 
								var nama_corak = ' - '+respon[i].nama_corak; 
							}
							if(respon[i].id_motif != ''){
								var id_motif = respon[i].id_motif; 
								var nama_motif = ' - '+respon[i].nama_motif; 
							}
                            if(respon[i].id_kualitas != ''){
                                var id_kual = respon[i].id_kualitas; 
                                var nama_kual = ' - '+respon[i].nama_kualitas; 
                            }

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

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

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

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

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

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

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

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

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

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

							$("[name=popup2_barang]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].satuan+'" corak="'+id_corak+'" motif="'+id_motif+'" kualitas="'+id_kual+'" dudulan="'+id_dudul+'" label="'+id_label+'" kemasan="'+id_kemasan+'" kemasan_satuan="'+id_kemasansatuan+'" kemasan_sepuluh="'+id_kemasansepuluh+'" kemasan_seratus="'+id_kemasanseratus+'" cap="'+id_cap+'"  kualitas_proses="'+id_kualitas_proses+'" kualitas_periksa="'+id_kualitas_periksa+'" rencana="'+id_rencana+'" jumlah_rencana="'+jumlah_rencana+'" nama_satuanrencana="'+nama_satuanrencana+'">'+respon[i].kategori+' - '+respon[i].sub_kategori+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+nama_corak+nama_motif+nama_kual+nama_kualitas_proses+nama_kualitas_periksa+nama_dudul+nama_label+nama_cap+nama_kemasan+nama_kemasansatuan+nama_kemasansepuluh+nama_kemasanseratus+nama_rencana+'</option>');
					}
					for(i in respon){
						$("[name=popup3_barang]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].satuan+'">'+respon[i].kategori+' - '+respon[i].nama_sub+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+'</option>');
					}
				}else{
					$("[name=popup2_barang]").html('');
					$("[name=popup2_barang]").append('<option value=""> Stok Kosong </option>');
					$("[name=popup3_barang]").html('');
					$("[name=popup3_barang]").append('<option value=""> Stok Kosong </option>');
				}


			}
		})
	}


	$("[name=popup_warna_tipe]").change(function(){
		var tipe = $(this).val();
		var id_order = $("[name=workorder]").val()
		if(id_order == ""){
			$("#div_warna_cek").text('No.order belum di isikan');
		}else{
			$("#div_warna_cek").text('');
		}
		$.ajax({
			url: '{{ url('barangkeluar/get_warna') }}',
			type: 'post',
			dataType: 'json',
			data: {tipe : tipe, id_order : id_order},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					if(tipe == '1'){
						var id = respon[0].dasar;
						var warna = respon[0].warna_dasar+' ('+respon[0].inisial_dasar+')';
					}else{
						var id = respon[0].streep;
						var warna = respon[0].warna_streep+' ('+respon[0].inisial_streep+')';
					}
						$("#div_warna").removeClass('hide');
						$("[name=popup_warna]").val(id);
						$("#div_warna_stok").val(warna);

				}else{
						$("#div_warna").addClass('hide');
						$("[name=popup_warna]").val('');
						$("#div_warna_stok").val('');
				}
			}
		}) 
	})

	$("#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 kode_barang = $("[name=popup_barang] :selected").attr('kode');
		var nama_barang = $("[name=popup_barang] :selected").attr('barang');
		var nama_gudang = $("[name=popup_gudang] :selected").attr('nama');
		var jumlah = $("[name=popup_jumlah]").val();
		var id_satuan_jumlah = $("[name=popup_satuanawal]").val();
		var satuan_jumlah = $("[name=popup_satuanawal] :selected").attr('nama');		
		var id_warna = $("[name=input_warna]").val();
		var nama_warna = '';
		if(id_warna != '' || id_warna != 0){
			var nama_warna = $("[name=input_warna] :selected").attr("nama");
		}		
		var id_proses2 = $("[name=popup_proses]").val();
		var id_corak = $("[name=popup_barang] :selected").attr('corak');
		var id_motif = $("[name=popup_barang] :selected").attr('motif');
        var id_kualitas = $("[name=popup_barang] :selected").attr('kualitas');
        var id_kualitas_dudul = $("[name=popup_barang] :selected").attr('dudulan');
        var id_label = $('[name=popup_barang] :selected').attr('label');
        var id_kemasan = $('[name=popup_barang] :selected').attr('kemasan');
        var id_kemasansatuan = $('[name=popup_barang] :selected').attr('kemasan_satuan');
        var id_kemasansepuluh = $('[name=popup_barang] :selected').attr('kemasan_sepuluh');
        var id_kemasanseratus = $('[name=popup_barang] :selected').attr('kemasan_seratus');
        var id_cap = $('[name=popup_barang] :selected').attr('cap');
		var id_kualitas_proses = $('[name=popup_barang] :selected').attr('kualitas_proses');
        var id_kualitas_periksa = $('[name=popup_barang] :selected').attr('kualitas_periksa');
        var id_perubahan = $("[name=popup_barang] :selected").attr('rencana');
        var jumlah_rencana = $("[name=popup_barang] :selected").attr('jumlah_rencana');
        var tanggal = $("[name=popup_tanggal]").val();
        var keterangan = $("[name=popup_ket]").val();
		var total = parseFloat(jumlah);
        var jum_hasil = $("[name=popup_jumlahhasil]").val();
        var satuan_hasil = $("[name=popup_satuanhasil]").val();
		
		if(id_barang != '' && jumlah != '0'){
			if(id_data == ''){
				table_barang.row.add(['<div><center></center></div>',
					'<div id="list_tanggal'+tb_no+'">'+tanggal+'</div>',
					'<div id="list_kode'+tb_no+'">'+kode_barang+'</div>',
					'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
					'<div id="list_warna'+tb_no+'">'+nama_warna+'</div>',
					'<div id="list_gudang'+tb_no+'">'+nama_gudang+'</div>',
					'<div id="list_jumlah'+tb_no+'">'+jumlah+' '+satuan_jumlah+'</div>',
                    '<div id="list_keterangan'+tb_no+'">'+keterangan+'</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_idbarang'+tb_no+'" name="tabel_idbarang[]" value="'+id_barang+'">'+
					'<input type="hidden" id="tabel_idgudang'+tb_no+'" name="tabel_idgudang[]" value="'+id_gudang+'">'+
					'<input type="hidden" id="tabel_jumlah'+tb_no+'" name="tabel_jumlah[]" value="'+jumlah+'">'+
					'<input type="hidden" id="tabel_satuan'+tb_no+'" name="tabel_satuan[]" value="'+id_satuan_jumlah+'">'+
					'<input type="hidden" id="tabel_total'+tb_no+'" name="tabel_total[]" value="'+total+'">'+
					'<input type="hidden" id="tabel_warna'+tb_no+'" name="tabel_warna[]" value="'+id_warna+'">'+
					'<input type="hidden" id="tabel_proses2'+tb_no+'" name="tabel_proses2[]" value="'+id_proses2+'">'+
					'<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_kualitas_dudul+'">'+
                    '<input type="hidden" id="tabel_idlabel'+tb_no+'" name="tabel_idlabel[]" value="'+id_label+'">'+
                    '<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_idcap'+tb_no+'" name="tabel_idcap[]" value="'+id_cap+'">'+
                    '<input type="hidden" id="tabel_idkualitas_proses'+tb_no+'" name="tabel_idkualitas_proses[]" value="'+id_kualitas_proses+'">'+
                    '<input type="hidden" id="tabel_idkualitas_periksa'+tb_no+'" name="tabel_idkualitas_periksa[]" value="'+id_kualitas_periksa+'">'+
                    '<input type="hidden" id="tabel_idrencana'+tb_no+'" name="tabel_idrencana[]" value="'+id_perubahan+'">'+
                    '<input type="hidden" id="tabel_rencanajumlah'+tb_no+'" name="tabel_rencanajumlah[]" value="'+jumlah_rencana+'">'+
                    '<input type="hidden" id="tabel_tanggal'+tb_no+'" name="tabel_tanggal[]" value="'+tanggal+'">'+
                    '<input type="hidden" id="tabel_keterangan'+tb_no+'" name="tabel_keterangan[]" value="'+keterangan+'">'+
                    '<input type="hidden" id="tabel_status'+tb_no+'" name="tabel_status[]" value="0">'+
                    '<input type="hidden" id="tabel_hasilid'+tb_no+'" name="tabel_hasilid[]" value="">'+
                    '<input type="hidden" id="tabel_hasiljumlah'+tb_no+'" name="tabel_hasiljumlah[]" value="'+jum_hasil+'">'+
                    '<input type="hidden" id="tabel_hasilsatuan'+tb_no+'" name="tabel_hasilsatuan[]" value="'+satuan_hasil+'">'
					]).draw(false);
			}else{
				$("#list_tanggal"+id_data).text(tanggal);
				$("#list_kode"+id_data).text(kode_barang);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_warna"+id_data).text(nama_warna);
				$("#list_gudang"+id_data).text(nama_gudang);
				$("#list_jumlah"+id_data).text(jumlah+' '+satuan_jumlah);
				$("#list_keterangan"+id_data).text(keterangan);

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);
				$("#tabel_namabarang"+id_data).val(nama_barang);				
				$("#tabel_jumlah"+id_data).val(jumlah);
				$("#tabel_satuan"+id_data).val(id_satuan_jumlah);
				$("#tabel_total"+id_data).val(total);
				$("#tabel_proses2"+id_data).val(id_proses2);
				$("#tabel_warna"+id_data).val(id_warna);
				$("#tabel_idcorak"+id_data).val(id_corak);
				$("#tabel_idmotif"+id_data).val(id_motif);
                $("#tabel_idkualitas"+id_data).val(id_kualitas);
                $("#tabel_idkualitas_dudul"+id_data).val(id_kualitas_dudul);
                $("#tabel_idlabel"+id_data).val(id_label);
                $("#tabel_idkemasan"+id_data).val(id_kemasan);
                $("#tabel_idkemasan_satuan"+id_data).val(id_kemasansatuan);
                $("#tabel_idkemasan_sepuluh"+id_data).val(id_kemasansepuluh);
                $("#tabel_idkemasan_seratus"+id_data).val(id_kemasanseratus);
                $("#tabel_idcap"+id_data).val(id_cap);
                $("#tabel_idkualitas_proses"+id_data).val(id_kualitas_proses);
                $("#tabel_idkualitas_periksa"+id_data).val(id_kualitas_periksa);
                $("#tabel_idrencana"+id_data).val(id_perubahan);
                $("#tabel_rencanajumlah"+id_data).val(jumlah_rencana);
                $("#tabel_tanggal"+id_data).val(tanggal);
                $("#tabel_keterangan"+id_data).val(keterangan);
                $("#tabel_hasiljumlah"+id_data).val(jum_hasil);
                $("#tabel_hasilsatuan"+id_data).val(satuan_hasil);
			}
		
		$("#exampleModal").modal('hide');
		tb_no++;
		}
	})

	

	function edit_table(id){
        load_gudang = 0;
        var id_proses = $("[name=proses]").val();
        // var id_proses2 = $("[name=popup_proses]").val();
        //proses2 => proses sebelumnya
        var id_proses2 = $("#tabel_proses2"+id).val();
        var warna = $("#tabel_warna"+id).val();

        var id_gudang = $("#tabel_idgudang"+id).val();
		var id_barang = $("#tabel_idbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();		
        var id_corak = $("#tabel_idcorak"+id).val()
        var id_komposisi = $("#tabel_warna"+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 = $("[name=workorder]").val();
    //    var id_proses = $("#tabel_proses2"+id).val();
        var id_satuan = $("#tabel_satuan"+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();
		var tanggal = $("#tabel_tanggal"+id).val();
        var keterangan = $("#tabel_keterangan"+id).val();
        var jum_hasil = $("#tabel_hasiljumlah"+id).val();
        var satuan_hasil = $("#tabel_hasilsatuan"+id).val();

		$("[name=popup_id_table]").val(id);
        

        var tabel_id = $("#tabel_id"+id).val();
        $("[name=popup_iddatabase]").val(tabel_id);

        if(id_proses2 == ''){
            id_komposisi = '';
        }

        
        
        var Function_first = function () {
          var r = $.Deferred();
            
            $("[name=popup_proses]").val(id_proses2).trigger('change');
            $("[name=input_warna]").val(warna).trigger('change');
            $("[name=popup_gudang]").val(id_gudang).trigger('change'); 

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

		var FunctionOne = function () {
		var r = $.Deferred();     
        
        console.log(store_idbarangkeluar+" | "+tabel_id);
        if(store_idbarangkeluar != '' && tabel_id != ''){
            $.ajax({
                url: '{{ url('barangkeluar_atm/get_barang') }}',
                type: 'post',
                dataType: 'json',
                data: {id : id_gudang, id_barang : '', id_proses : id_proses,id_komposisi:id_komposisi,id_proses2:id_proses2, workorder:workorder},
                headers : {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(respon){
                    var jumlah = $("[name=popup_barang]").find("option[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 + "'][value='"+ id_barang +"'][satuan='"+id_satuan+"'][cap='"+id_cap+"'][kualitas_proses='"+id_kualitas_proses+"'][kualitas_periksa='"+id_kualitas_periksa+"'][rencana='"+id_rencana+"'][jumlah_rencana='"+jumlah_rencana+"'][komposisi='"+id_komposisi+"'] ");                    

                    console.log(jumlah.length);
                        if(jumlah.length == '0'){
                            for(i in respon){
                                var id_corak_ = '', nama_corak_ = '', id_motif_ = '', nama_motif_ = '', id_kual_ = '', nama_kual_ = '', id_dudul_ = '', nama_dudul_ = '', id_label_ = '', nama_label_ = '', id_kemasan_ = '', nama_kemasan_ = '', id_kemasansatuan_ = '', nama_kemasansatuan_ = '', id_kemasansepuluh_ = '', nama_kemasansepuluh_ = '', id_kemasanseratus_ = '', nama_kemasanseratus_ = '', id_cap_ = '', nama_cap_ = '', id_kualitas_proses_ = '', nama_kualitas_proses_ = '', id_kualitas_periksa_ = '', nama_kualitas_periksa_ = '';
                            if(respon[i].id_corak != ''){
                                var id_corak_ = respon[i].id_corak; 
                                var nama_corak_ = ' - '+respon[i].nama_corak; 
                            }
                            if(respon[i].id_motif != ''){
                                var id_motif_ = respon[i].id_motif; 
                                var nama_motif_ = ' - '+respon[i].nama_motif; 
                            }
                            if(respon[i].id_kualitas != ''){
                                var id_kual_ = respon[i].id_kualitas; 
                                var nama_kual_ = ' - '+respon[i].nama_kualitas; 
                            }
                            if(respon[i].id_kualitas_dudul != ''){
                                var id_dudul_ = respon[i].id_kualitas_dudul;
                                var nama_dudul_ = ' - Dudulan '+respon[i].kualitas_dudul;
                            }

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

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

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

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

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

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

                            if(respon[i].id_kualitas_proses != ''){
                                var id_kualitas_proses_ = respon[i].id_kualitas_proses; 
                                var nama_kualitas_proses_ = ' - '+respon[i].nama_kualitas_proses; 
                            }

                            if(respon[i].id_kualitas_periksa != ''){
                                var id_kualitas_periksa_ = respon[i].id_kualitas_periksa; 
                                var nama_kualitas_periksa_ = ' - '+respon[i].nama_kualitas_periksa; 
                            }

                            var id_rencana_ = '', nama_rencana_ = '', jumlah_rencana_ = '' ;
                            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_barang]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].id_satuan+'" corak="'+id_corak_+'" motif="'+id_motif_+'" kualitas="'+id_kual_+'" dudulan="'+id_dudul_+'" label="'+id_label_+'" kemasan="'+id_kemasan_+'" kemasan_satuan="'+id_kemasansatuan_+'" kemasan_sepuluh="'+id_kemasansepuluh_+'" kemasan_seratus="'+id_kemasanseratus_+'" cap="'+id_cap_+'" kualitas_proses="'+id_kualitas_proses_+'" kualitas_periksa="'+id_kualitas_periksa_+'" rencana="'+id_rencana_+'" jumlah_rencana="'+jumlah_rencana_+'" komposisi="'+respon[i].id_komposisi+'">'+respon[i].concat_nama+'</option>');
                            }
                        }
                }, complete: function(){
                    // FunctionTwo();
                    repeat();
                }
            })
        }else{
            // FunctionTwo();
            repeat();
        }

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

        function repeat_first(){
            if(load_gudang == 1){
                FunctionOne();
            }else{
                setTimeout(repeat_first, 2000);
            }
        }

        function repeat(){
            if(load_gudang == 1){
                FunctionTwo();
            }else{
                setTimeout(repeat, 2000);
            }
        }

		var FunctionTwo = function () {

            $("[name=popup_barang]").find("option[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 + "'][value='"+ id_barang +"'][satuan='"+id_satuan+"'][cap='"+id_cap+"'][kualitas_proses='"+id_kualitas_proses+"'][kualitas_periksa='"+id_kualitas_periksa+"'][rencana='"+id_rencana+"'][jumlah_rencana='"+jumlah_rencana+"'][komposisi='"+id_komposisi+"'] ").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+"\ncap "+id_cap+"\nid kualitas proses "+id_kualitas_proses+"\nid kualitas periksa "+id_kualitas_periksa); 

			// $("[name=popup_barang]").val(id_barang).trigger('change');
			$("[name=popup_jumlah]").val(jumlah);
			$("[name=popup_satuanawal]").val(id_satuan).trigger('change');
			$("[name=popup_jumlahhasil]").val(jum_hasil);
            $("[name=popup_satuanhasil]").val(satuan_hasil).trigger('change');
			/*$("[name=popup2_jumlah_konv]").val(jumlah_konv);*/
			
			$("[name=popup_stok]").text(jumlah);
			$("[name=popup_jumlah]").attr('min', 0);
			$("[name=popup_jumlah]").attr('max', jumlah);

			popup_jumlah(id);
            $("[name=popup_ket]").val(keterangan);
			$("#exampleModal").modal('show');
		};

		Function_first().done(repeat_first());
	}

	/* -------------------------- Gudang popup edit selesai ---------------------------------------*/


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

	function get_edit(id){
		$.ajax({
			url: "{{ url('barangkeluar_atm/get_edit')}} ",
			type: 'post',
			data: {id : id,},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					for(i in respon){
						table_barang.row.add(['<div><center></center></div>',
							'<div id="list_tanggal'+respon[i].id+'">'+respon[i].tanggal_proses+'</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_warna'+respon[i].id+'">'+respon[i].nama_komposisi+'</div>',
							'<div id="list_gudang'+respon[i].id+'">'+respon[i].nama_gudang+'</div>',
							'<div id="list_jumlah'+respon[i].id+'">'+respon[i].jumlah+' '+respon[i].satuan_jumlah+'</div>',
                            '<div id="list_keterangan'+respon[i].id+'">'+respon[i].keterangan+'</div>',
							/*'<div id="list_jumlah_konv'+respon[i].id+'">'+respon[i].jumlah_konv+' '+respon[i].satuan_jumlah_konv+'</div>',*/
							'<div><button type="button" 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_id'+respon[i].id+'" name="tabel_id[]" value="'+respon[i].id+'">'+
                            '<input type="hidden" id="tabel_idbarang'+respon[i].id+'" name="tabel_idbarang[]" value="'+respon[i].id_barang+'">'+
                            '<input type="hidden" id="tabel_idgudang'+respon[i].id+'" name="tabel_idgudang[]" value="'+respon[i].id_gudang+'">'+
                            '<input type="hidden" id="tabel_jumlah'+respon[i].id+'" name="tabel_jumlah[]" value="'+respon[i].jumlah+'">'+
                            '<input type="hidden" id="tabel_satuan'+respon[i].id+'" name="tabel_satuan[]" value="'+respon[i].id_satuan_jumlah+'">'+
                            '<input type="hidden" id="tabel_total'+respon[i].id+'" name="tabel_total[]" value="'+respon[i].jumlah+'">'+
                            '<input type="hidden" id="tabel_warna'+respon[i].id+'" name="tabel_warna[]" value="'+respon[i].id_komposisi+'">'+
                            '<input type="hidden" id="tabel_proses2'+respon[i].id+'" name="tabel_proses2[]" value="'+respon[i].id_proses_sebelumnya+'">'+
                            '<input type="hidden" id="tabel_idcorak'+respon[i].id+'" name="tabel_idcorak[]" value="'+respon[i].id_corak+'">'+
                            '<input type="hidden" id="tabel_idmotif'+respon[i].id+'" name="tabel_idmotif[]" value="'+respon[i].id_motif+'">'+
                            '<input type="hidden" id="tabel_idkualitas'+respon[i].id+'" name="tabel_idkualitas[]" value="'+respon[i].id_kualitas+'">'+
                            '<input type="hidden" id="tabel_idkualitas_dudul'+respon[i].id+'" name="tabel_idkualitas_dudul[]" value="'+respon[i].id_kualitas_dudul+'">'+
                            '<input type="hidden" id="tabel_idlabel'+respon[i].id+'" name="tabel_idlabel[]" value="'+respon[i].id_label+'">'+
                            '<input type="hidden" id="tabel_idkemasan'+respon[i].id+'" name="tabel_idkemasan[]" value="'+respon[i].id_kemasan+'">'+
                            '<input type="hidden" id="tabel_idkemasan_satuan'+respon[i].id+'" name="tabel_idkemasan_satuan[]" value="'+respon[i].id_kemasansatuan+'">'+
                            '<input type="hidden" id="tabel_idkemasan_sepuluh'+respon[i].id+'" name="tabel_idkemasan_sepuluh[]" value="'+respon[i].id_kemasansepuluh+'">'+
                            '<input type="hidden" id="tabel_idkemasan_seratus'+respon[i].id+'" name="tabel_idkemasan_seratus[]" value="'+respon[i].id_kemasanseratus+'">'+
                            '<input type="hidden" id="tabel_idcap'+respon[i].id+'" name="tabel_idcap[]" value="'+respon[i].id_cap+'">'+
                            '<input type="hidden" id="tabel_idkualitas_proses'+respon[i].id+'" name="tabel_idkualitas_proses[]" value="'+respon[i].id_kualitas_proses+'">'+
                            '<input type="hidden" id="tabel_idkualitas_periksa'+respon[i].id+'" name="tabel_idkualitas_periksa[]" value="'+respon[i].id_kualitas_periksa+'">'+
                            '<input type="hidden" id="tabel_idrencana'+respon[i].id+'" name="tabel_idrencana[]" value="'+respon[i].id_perubahansatuan+'">'+
                            '<input type="hidden" id="tabel_rencanajumlah'+respon[i].id+'" name="tabel_rencanajumlah[]" value="'+respon[i].jumlah_rencana+'">'+
                            '<input type="hidden" id="tabel_tanggal'+respon[i].id+'" name="tabel_tanggal[]" value="'+respon[i].tanggal_proses+'">'+
                            '<input type="hidden" id="tabel_status'+respon[i].id+'" name="tabel_status[]" value="'+respon[i].status_barang+'">'+
                            '<input type="hidden" id="tabel_keterangan'+respon[i].id+'" name="tabel_keterangan[]" value="'+respon[i].keterangan+'">'+
                            '<input type="hidden" id="tabel_hasilid'+respon[i].id+'" name="tabel_hasilid[]" value="'+respon[i].id_keluarhasil+'">'+
                            '<input type="hidden" id="tabel_hasiljumlah'+respon[i].id+'" name="tabel_hasiljumlah[]" value="'+respon[i].hasil+'">'+
                            '<input type="hidden" id="tabel_hasilsatuan'+respon[i].id+'" name="tabel_hasilsatuan[]" value="'+respon[i].id_satuanhasil+'">'
							]).draw(false);
					}

					// get_jum();
				}
				
			}
		})
	}

	function get_nomor(this_){
        //var tgl = this_.val();
        var id_lokasi = $("[name=id_jenis]").val();
        $.ajax({
            url: "{{ url('barangkeluar_atm/get_nomor') }}",
            type: 'get',
            dataType: 'json',
            data: {id : $("[name=id_barangkeluar]").val() , tgl : '', id_lokasi : id_lokasi},
            success: function(respon){
                $("[name=nomor").val(respon.nomor);
                $("[name=nomor_ext]").val(respon.faktur);
            }
        })


    }

    $("[name=id_jenis]").change(function(){
        get_nomor();
    })

    $("[name=proses]").change(function(){
        var val = parseInt($(this).val());
        var id_proses = [{{ implode(",", setup_hasilatm()) }}];

        if(isInArray(val, id_proses) == true){
            $("[name=popup_jumlahhasil]").parent().parent().show();
            $("[name=popup_satuanhasil]").parent().parent().show();
            $("[name=popup2_jumlahhasil]").parent().parent().show();
            $("[name=popup2_satuanhasil]").parent().parent().show();
        }else{
            $("[name=popup_jumlahhasil]").parent().parent().hide();
            $("[name=popup_satuanhasil]").parent().parent().hide();
            $("[name=popup2_jumlahhasil]").parent().parent().hide();
            $("[name=popup2_satuanhasil]").parent().parent().hide();
        }
    })

    function perkiraan_hasil(){
        var val = parseInt($("[name=proses]").val());
        var id_proses = [{{ implode(",", setup_hasilatm()) }}];

        if(isInArray(val, id_proses) == true){
            $("[name=popup_jumlahhasil]").parent().parent().show();
            $("[name=popup_satuanhasil]").parent().parent().show();
            $("[name=popup2_jumlahhasil]").parent().parent().show();
            $("[name=popup2_satuanhasil]").parent().parent().show();
        }else{
            $("[name=popup_jumlahhasil]").parent().parent().hide();
            $("[name=popup_satuanhasil]").parent().parent().hide();
            $("[name=popup2_jumlahhasil]").parent().parent().hide();
            $("[name=popup2_satuanhasil]").parent().parent().hide();
        }

        // console.log();  
    }

    function isInArray(value, array) {
        return array.indexOf(value) > -1;
    }

</script>

@endsection