@extends('layout')


@section('content')

<style type="text/css">
.popover-content{
	padding: 5px !important;
	text-align: left !important;
}

.table-form>tbody>tr>th, .table-form>tfoot>tr>th, .table-form>thead>tr>td, .table-form>tbody>tr>td, .table-form>tfoot>tr>td{
    padding: 5px;
   -- vertical-align: middle;
}

table.table-form tbody>tr.selected, table.table-form tbody>tr.selected td, table.table-form tbody>tr>.selected{
    color: white !important;
    background-color: #49b6d6 !important;
}

table, th, td, input, textarea, select {
    color:  black !important; 
}

.help-block { 
    margin-top: 0px!important;
    margin-bottom: 0px!important;
}
</style>

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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Input Upah</h5>
			<div class="heading-elements">
				<ul class="icons-list">
					<li><a href="{{ $data['url_referrer'] }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
				</ul>
			</div>
		</div>
		<div class="panel-body">
			<form class="form-horizontal" id="form_submit" method="post" action="{{ url('upahproduksi/rekanan_tenun/simpan') }}" autocomplete="off">
				<input type="hidden" name="_token" value="{{ csrf_token() }}">

				<input type="hidden" name="id_upahproduksi" value="{{ $data['data']['id_upahproduksi'] }}">
				<input type="hidden" name="spk" value="{{ $data['data']['id_workorder']}}">
				<input type="hidden" name="workorder" value="{{ $data['data']['id_workorder_barang']}}">
				<input type="hidden" name="id_barangkeluar" value="{{ $data['data']['id_barangkeluar']}}">
				<input type="hidden" name="id_proses" value="{{ $data['barang_masuk']['id_proses']}}">

				<div class="col-sm-6">
					<div class="form-group">
						<label class="control-label col-sm-4">Upah</label>
						<div class="col-sm-8">       				
							<select class="form-control select-search" name="jenis" required>
								@php $sel = $data['data']['id_jenis']; @endphp
								<option value="">--- Pilih Upah ---</option>
								<option value="1" selected="" {{ ( "1" == $sel) ? "selected":"selected"}}>Borongan</option>
								<!-- <option value="2" {{ ( "2" == $sel) ? "selected":""}}>Harian</option> -->
							</select>
						</div>
					</div>

					<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" id="rekanan">
								<option value="">--- Pilih Rekanan ---</option>
								@php $sel = $data['data']['id_rekanan'];
								foreach($data['rekanan'] as $d){@endphp
								<option value="{{$d->id_rekanan}}" {{ ($d->id_rekanan == $sel) ? "selected":""}}>{{$d->nama}}</option>
								@php } @endphp
							</select>
						</div>
					</div>
					<div class="form-group" style="display: none;">
						<label class="control-label col-sm-4">Pegawai</label>
						<div class="col-sm-8">        				
							<select class="form-control select-search" name="pegawai" id="pegawai">
								<option value="">--- Pilih Pegawai ---</option>
								@php $sel = $data['data']['id_pegawai'];
								foreach($data['pegawai'] as $d){@endphp
								<option value="{{$d->id_pegawai}}" {{ ($d->id_pegawai == $sel) ? "selected":""}}>{{$d->nama_pegawai}}</option>
								@php } @endphp
							</select>
						</div>
					</div>        		
					<div class="form-group">
						<label class="control-label col-sm-4">Proses Produksi</label>
						<div class="col-sm-8">
							<select class="form-control select-search" name="proses" required="" id="proses">
							</select>
						</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" id="lokasi_pro">
								<option value="">--- Pilih Jenis ---</option>
								@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 hidden">
	        			<label class="control-label col-sm-4">Akun</label>
	        			<div class="col-sm-8">
	        				{{ Form::select("id_akun_upah", $data['akun_upah'], $data['data']['id_akun_upah'], ['class'=>'form-control set_seleted', 'readonly'=>'']) }}
	        			</div>
	        		</div>
					<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" placeholder="Tanggal" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):date('d-m-Y', strtotime($data['data']['tanggal'])) }}" required/>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-4">Nomor</label>
						<div class="col-sm-8">
							<input class="form-control" type="text" name="nomor" placeholder="" value="{{ $data['data']['nomor'] }}" id="nomor" required="" />
						</div>
					</div>
					<div class="form-group" id="div_laphasil">
						<label class="control-label col-sm-4">Laporan Hasil</label>
						<div class="col-sm-8">
							<select class="form-control select-search" data-placeholder="Pilih laporan hasil" multiple="multiple" name="lap_hasil" id="lap_hasil">

							</select>
						</div>
					</div>
				</div>

				<div class="col-sm-12 form-group">
					<h5>Hasil</h5>
					<div class="tabel_hasil_1">
					<table class="table table-striped" id="table_hasil">
	        			<thead>
	        				<tr>
	        					<th>No</th>
								<th>Kode Barang</th>
								<th>Nama Barang</th>
								<th>Motif</th>
								<th>Corak</th>
								<th>Jml</th>
								<th width="10%">Petenun</th>
								<th width="10%">Makelar</th>
								<th width="10%">Pick</th>
								<th width="10%">Total</th>
	        				</tr>
	        			</thead>
	        			<tbody>
	        				
	        			</tbody>
	        			<tfoot>
	        				<tr>
	        					<th colspan="9">Sub Total</th>
	        					<th>
	        						<input type="hidden" name="sub_total1">
	        						<p id="sub_total1">Rp0</p>
	        					</th>
	        				</tr>
	        			</tfoot>
	        		</table>
	        		</div>
	        		
				</div>

				<div class="col-sm-12 form-group"> 
					<h5>Lain-Lain</h5>        	
					<a class="btn btn-success" id="btn_tambah_item_lain_lain">Tambah</a>  
					<table class="table table-striped" id="table_lain2">
						<thead>
							<tr>
								<th>No.</th>
								<th>Keterangan</th>
								<th>Jumlah</th>
								<th></th>							
							</tr>
						</thead>
						<tbody>

						</tbody>
						<tfoot>
							<tr>
								<th></th>
								<th>Sub Total</th>
								<th><p id="sub_total2">0</p></th>
								<th></th>
							</tr>
						</tfoot>
					</table>
				</div>

				<div class="col-sm-12 form-group">
					<div class=" col-md-6 col-sm-12 m-l-5">
						<div class="table-responsive">

							<table class="table table-bordered table-form">
								<tbody>
									<tr class="active" style="display: none;">
										<td>Total Pembelian</td>
										<td class="text-right">
											<label class="checkbox-inline checkbox-right">
												<input type="checkbox" value="1" name="termasuk_pajak" id="termasuk_pajak" class="" onclick="termasuk_pajaks($(this))" {{ $data['data']['termasuk_pajak']==1 ? "checked='checked'" : "" }}> Sudah Termasuk PPN
											</label>
										</td>
									</tr>
									<tr>
										<td width="30%">Sub Total</td>
										<td><input type="hidden" name="td_total" value="0" readonly="">
											<p class="help-block pull-right" id="td_total">Rp 0</p>
										</td>
									</tr>

									<tr style="display: none;">
										<td>Potongan</td>
										<td><input type="text" name="td_potongan" class="form-control number-only text-right" value="{{ $data['data']['potongan'] }}">
											<p class="help-block pull-right" id="help_popup_potongan">Rp 0</p>
										</td>
									</tr>
									
									<tr>
										<td>PPN</td>
										<td>
											<select class="form-control select-search" name="pajak">
												<option value="0" @if($data['data']['pajak'] == 0) selected @endif>Tidak</option>
												<option value="1" @if($data['data']['pajak'] == 1) selected @endif>Iya</option>
											</select>
											<input type="hidden" name="td_pajak" class="form-control number-only text-right" value="0">
											<p class="help-block pull-right" id="help_popup_pajak">Rp 0</p>
										</td>
									</tr>
									<tr>
										<td>Tagihan</td>
										<td><p class="help-block pull-right" id="td_netto">Rp 0</p></td>
									</tr>
								</tbody>
							</table>

						</div>
					</div>
				</div>

				<div class="col-sm-12">
					<a href="{{ $data['url_referrer'] }}" class="btn bg-warning-400">Kembali</a>
					<div class="form-group pull-right">
						<button class="btn btn-primary" type="submit" id="btn_simpan">Simpan</button>
					</div>
				</div>


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

	<!---Modal--->


	<div class="modal fade" id="exampleModal2" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">Data Lain-Lain</h5>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" autocomplete="off">
						<input type="hidden" name="popup_id_table_lain_lain">
						<div class="form-group">
							<label class="control-label col-sm-4">Keterangan</label>
							<div class="col-sm-8">
								<input type="text" name="jenis_lain_lain" class="form-control" id="jenis_lain_lain">
	      					<!-- <select class="form-control select-search" name="popup_keterangan">	      						
	      					</select> -->
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah</label>
	      				<div class="col-sm-8">
	      					<input type="number" id="upah_lain_lain" name="upah_lain_lain" class="form-control text-right" step="0.01">
	      					<p class="pull-right" id="help_popup_upah_lain_lain"></p>
	      				</div>
	      			</div>	      			
	      		</form>
	      	</div>
	      	<h5 id="alert_workorder"></h5>
	      	<div class="modal-footer">
	      		<button type="button" class="btn btn-primary" id="btn_popup_simpan_lain_lain">Simpan</button>
	      	</div>
	      </div>
	  </div>
	</div>


	<!-- EndModal -->


<script type="text/javascript">

	var f = "{{ $data['f'] }}"; // jenis function controller
	var table_hasil;
	var table_lain2;
	var tb_no_ll= parseInt(10000);

	if(f=="add"){
		var id = "{{$data['data']['id_upahproduksi']}}";
		var c_rekanan 	= "{{$data['barang_masuk']->dataBarangkeluar->id_rekanan}}";
		var c_pegawai 	= "{{$data['barang_masuk']->dataBarangkeluar->id_pegawai}}";
		var c_proses  	= "{{$data['barang_masuk']->dataBarangkeluar->id_proses}}";
		var c_lokasi  	= "{{$data['barang_masuk']->dataBarangkeluar->id_lokasi}}";
		var c_barangmasuk  	= "{{$data['barang_masuk']->id_barangmasuk}}";
		var c_jenis 	= "{{$data['data']['id_jenis']}}";
		var c_laphasil 	= "{{$data['barang_masuk']->id_barangmasuk}}";

	}else if(f=="edit"){
		var id = "{{$data['data']['id_upahproduksi']}}";
		var c_rekanan 	= "{{$data['data']['id_rekanan']}}";
		var c_pegawai 	= "{{$data['data']['id_pegawai']}}";
		var c_proses  	= "{{$data['data']['id_proses']}}";
		var c_lokasi  	= "{{$data['data']['jenis']}}";
		var c_barangmasuk  	= "{{$data['data']['id_barangmasuk']}}";
		var c_jenis 	= "{{$data['data']['id_jenis']}}"; 
		var c_laphasil 	= "{{$data['data']['lap_hasil']}}"; 

	}

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

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

		$("[name=pegawai]").change(function(){
			var pegawai = $("[name=pegawai]").val();
			if(pegawai > 0){
				disabled('[name=rekanan]', true);
				$("#div_laphasil").hide();
			}else{
				disabled('[name=rekanan]', false);
				$("[name=proses]").val(null).trigger('change');
				$("#div_laphasil").show();
			}
			
			get_proses();
		});

		$("[name=rekanan]").change(function(){
			var rekanan = $("[name=rekanan]").val();
			if(rekanan > 0){
				disabled('[name=pegawai]',true);
				$("#div_laphasil").show();
			}else{
				disabled('[name=pegawai]',false);
				$("[name=proses]").val(null).trigger('change');
				$("#div_laphasil").hide();
			}
			
			get_proses();
		});

		$("[name=proses]").change(function(){
			var id_barangmasuk 		= $(this).val();
			var id_barangkeluar 	= $("[name=proses] :selected").attr('barangkeluar');
			var id_workorder 		= $("[name=proses] :selected").attr('spk');
			var id_workorder_barang = $("[name=proses] :selected").attr('workorder');
			var id_proses 			= $("[name=proses] :selected").attr('proses');
			var id_rekanan 			= $("[name=rekanan]").val();
			var id_pegawai 			= $("[name=pegawai]").val();
			var id_jenis 			= $("[name=jenis]").val();
			var lap_hasil 			= $("#lap_hasil").val();

			$("[name=id_barangkeluar").val(id_barangkeluar);
			$("[name=spk").val(id_workorder);
			$("[name=workorder").val(id_workorder_barang);
			$("[name=id_proses").val(id_proses);

			if(id_rekanan !="" && id_rekanan!=null ){
				getLapHasil(id_rekanan, "rekanan", id_proses);

			}else{
				getLapHasil(id_pegawai, "pegawai");

			}
		});

		$("#lap_hasil").change(function(evt){
			evt.preventDefault();

			var lap_hasil 		= $(this).val(),
			id_barangmasuk 		= $("[name=proses]").val(),
			id_barangkeluar		= $("[name=proses] :selected").attr('barangkeluar'),
			id_workorder 		= $("[name=proses] :selected").attr('spk'),
			id_workorder_barang = $("[name=proses] :selected").attr('workorder'),
			id_proses 			= $("[name=proses] :selected").attr('proses'),
			id_rekanan			= $("[name=rekanan]").val(),
			id_pegawai 			= $("[name=pegawai]").val(),
			id_jenis			= $("[name=jenis]").val(),
			id_lain2			= "{{ $data['id_trans'] }}";

			$("[name=id_barangkeluar").val(id_barangkeluar);
			$("[name=spk").val(id_workorder);
			$("[name=workorder").val(id_workorder_barang);
			$("[name=id_proses").val(id_proses);

			$.ajax({
				url: "{{ url('upahproduksi/get_rekanan2')}}",
				type: 'post',
				data: {f : f, lap_hasil : lap_hasil, id_barangmasuk : id_barangmasuk, id_barangkeluar : id_barangkeluar, id_workorder : id_workorder, id_workorder_barang : id_workorder_barang, id_proses : id_proses, id_rekanan : id_rekanan, id_pegawai : '', id_jenis : id_jenis, id_upah_borongan : id_lain2},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					table_hasil.clear().draw();

					var sub_total = 0;
					$("[name=sub_total1]").val(sub_total);
					$("#sub_total1").text(accounting.formatMoney(sub_total));

					load_tabel_hasils(respon);

					netto();
					set_sub_total_lain2();
				}
			});

		});

		$("#upah_lain_lain").keyup(function(){
			var rp=accounting.formatMoney($("#upah_lain_lain").val());
			$("#help_popup_upah_lain_lain").text(rp);
		});

		$("#btn_tambah_item_lain_lain").click(function(){
			$("[name=popup_id_table_lain_lain]").val('');
			$("#jenis_lain_lain").val('');
			$("#upah_lain_lain").val('');
			$("#help_popup_upah_lain_lain").text('');
			$("#exampleModal2").modal('show');
		})

		var no=parseInt(0);
		$("#btn_popup_simpan_lain_lain").click(function(){
			// alert("berhasil");
			var id_data = $("[name=popup_id_table_lain_lain]").val();
			var jenis= $("#jenis_lain_lain").val();
			var upah=$("#upah_lain_lain").val();
			if(jenis != '' && upah !=''){
				if(id_data==''){
					table_lain2.row.add(['<div><center>'+no+'</center></div>',
						'<div id="list_jenis'+tb_no_ll+'">'+jenis+'</div>',
						'<div id="list_upah'+tb_no_ll+'">'+accounting.formatMoney(upah)+'</div>',
						'<div><button type="button" class="btn bg-warning-400" onclick="edit_tablell('+tb_no_ll+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button"  onclick="hapusll(d=$(this))"><i class="icon-eraser"></i></button></div>'+
						'<input type="hidden" id="tabel_id2'+tb_no_ll+'" name="tabel_id2[]" value="">'+
						'<input type="hidden" id="tabel_jenis'+tb_no_ll+'" name="tabel_jenis[]" value="'+jenis+'">'+
						'<input type="hidden" id="tabel_upah'+tb_no_ll+'" name="tabel_upahll[]" value="'+upah+'">',
						]).draw(false);
					set_sub_total_lain2();
					tb_no_ll++;
					no=tb_no_ll;
				}else{
					$("#list_jenis"+id_data).text(jenis);
					$("#list_upah"+id_data).text(accounting.formatMoney(upah));

					$("#tabel_id2"+id_data).val(id_data);
					$("#tabel_jenis"+id_data).val(jenis);
					$("#tabel_upah"+id_data).val(upah);
					set_sub_total_lain2();
				}
			}
			//total_lain2 += parseFloat(upah);
			// $("#sub_total2").text(accounting.formatMoney(total_lain2));
			$("#exampleModal2").modal('hide');
			
		})

		$("[name=rekanan]").val(c_rekanan).change();
		$("#lokasi_pro").val(c_lokasi).change();

		$(document).on("change","select[name=pajak]",function(e){
	    	if($(this).val()==1){
	    		$("#termasuk_pajak").attr("checked", true);
	    		$("input[name=td_pajak]").val(0);    	
	    		$("input[name=td_pajak]").removeAttr('readonly');
	    	}else{
	    		$("#termasuk_pajak").attr("checked", false);
	    		$("input[name=td_pajak]").val(0);
	    		$("input[name=td_pajak]").attr('readonly','readonly');
	    	}
	    	netto();
	    });
	});
	
	function load_tabel_hasils(respon) {
		if(respon.status_hasil == 1){
			var sub_total1=0;
			for(i in respon.hasil){

                var upah= parseFloat(respon.hasil[i].upah);
                var idPbb = respon.hasil[i].id;
                var idBarang = respon.hasil[i].id_barang || 0;
                var idMotif = respon.hasil[i].id_motif || 0;
                var idMotifBarang = respon.hasil[i].id_corak || 0;
                var idProses = respon.hasil[i].id_proses || 0;
                /* === */
                var idGudang = respon.hasil[i].id_gudang || 0;
                var idSatuan = respon.hasil[i].id_satuan || 0;
                var idKomposisi = respon.hasil[i].id_komposisi || 0;
                var idKualitas = respon.hasil[i].id_kualitas || 0;
                var idKualitasDudul = respon.hasil[i].id_kualitas_dudul || 0;
                var idKualitasProses = respon.hasil[i].id_kualitas_proses || 0;
                var idKualitasPeriksa = respon.hasil[i].id_kualitas_periksa || 0;
                var idLabel = respon.hasil[i].id_label || 0;
                var idCap = respon.hasil[i].id_cap || 0;
                var idKemasan = respon.hasil[i].id_kemasan || 0;
                var idKemasansatuan = respon.hasil[i].id_kemasansatuan || 0;
                var idKemasansepuluh = respon.hasil[i].id_kemasansepuluh || 0;
                var idKemasanseratus = respon.hasil[i].id_kemasanseratus || 0;
                var idPlat = respon.hasil[i].id_plat || 0;
                /* === */

                if (respon.hasil[i].nama_kualitas == "Baik") {
                	var upah_petenun = respon.hasil[i].upah_satuan;
                } else {
                	var upah_petenun = respon.hasil[i].potongan_upah_kualitas;
                }

                var upah_makelar = respon.hasil[i].upah_satuan_2 || 0;
                var upah_pick = respon.hasil[i].upah_pick || 0;
                if (respon.hasil[i].jenis_pick == 3) {
                	upah_pick = -1*Math.abs(upah_pick);
                }
                var upah_total = parseFloat(upah_petenun) + parseFloat(upah_makelar) + parseFloat(upah_pick);

                if (isNaN(upah_total)) {upah_total=0;}

				//alert(isNaN(upah));
				if (isNaN(upah)){
					upah=0;
				}

				// edit data upah
				// if(respon.upah_bayar!=""){
				// 	upah = respon.upah_bayar[i].upah_satuan;
				// 	idPbb = respon.upah_bayar[i].id;
				// }
				// end : edit data upah

				var popover_content = "<table class='table table-bordered table-striped'>"+
						"<tbody>"+
							"<tr>"+
								"<th style='padding:1px !important;'>Warna</th>"+
								"<td style='padding:1px !important;'>"+respon.hasil[i].nama_komposisi+"</td>"+
							"</tr>"+
							"<tr>"+
								"<th style='padding:1px !important;'>K. Tenun</th>"+
								"<td style='padding:1px !important;'>"+respon.hasil[i].nama_kualitas+"</td>"+
							"</tr>"+
							"<tr>"+
								"<th style='padding:1px !important;'>Gudang</th>"+
								"<td style='padding:1px !important;'>"+respon.hasil[i].nama_gudang+"</td>"+
							"</tr>"+
							"<tr>"+
								"<th style='padding:1px !important;'>Satuan</th>"+
								"<td style='padding:1px !important;'>"+respon.hasil[i].nama_satuan+"</td>"+
							"</tr>"+
						"</tbody>"+
					"</table>";
				// var total_upah = 0;
				table_hasil.row.add(['<div><center></center></div>',									
                    '<input type="hidden" name="id_pbb_[]" value="'+idPbb+'">'+
                    '<input type="hidden" name="id_motif_[]" value="'+idMotif+'">'+
                    '<input type="hidden" name="id_motif_barang_[]" value="'+idMotifBarang+'">'+
                    '<input type="hidden" name="id_barang_[]" value="'+idBarang+'">'+
                    '<input type="hidden" name="id_proses_[]" value="'+idProses+'">'+
                    '<input type="hidden" name="jumlah_barang_[]" value="'+respon.hasil[i].jumlah+'">'+
                    // '<input type="hidden" name="upah_satuan_[]" value="'+upah+'">'+
                    /* === */
                    '<input type="hidden" name="id_gudang_[]" value="'+idGudang+'">'+
                    '<input type="hidden" name="id_satuan_[]" value="'+idSatuan+'">'+
                    '<input type="hidden" name="id_komposisi_[]" value="'+idKomposisi+'">'+
                    '<input type="hidden" name="id_kualitas_[]" value="'+idKualitas+'">'+
                    '<input type="hidden" name="id_kualitas_dudul_[]" value="'+idKualitasDudul+'">'+
                    '<input type="hidden" name="id_kualitas_proses_[]" value="'+idKualitasProses+'">'+
                    '<input type="hidden" name="id_kualitas_periksa_[]" value="'+idKualitasPeriksa+'">'+
                    '<input type="hidden" name="id_label_[]" value="'+idLabel+'">'+
                    '<input type="hidden" name="id_cap_[]" value="'+idCap+'">'+
                    '<input type="hidden" name="id_kemasan_[]" value="'+idKemasan+'">'+
                    '<input type="hidden" name="id_kemasansatuan_[]" value="'+idKemasansatuan+'">'+
                    '<input type="hidden" name="id_kemasansepuluh_[]" value="'+idKemasansepuluh+'">'+
                    '<input type="hidden" name="id_kemasanseratus_[]" value="'+idKemasanseratus+'">'+
                    '<input type="hidden" name="id_plat_[]" value="'+idPlat+'">'+
                    /* === */
                    '<input type="hidden" name="upah_total_[]" value="'+upah_total+'">'+

                    '<div id="list_kode'+respon.hasil[i].id+'">'+
                    '<button type="button" class="btn btn-link" data-popup="popover" title="" data-html="true" data-content="'+popover_content+'" data-trigger="focus"><i class="fa fa-info-circle"></i> '+respon.hasil[i].kode_barang+'</button>'+
                    '</div>',

					'<div id="list_nama'+respon.hasil[i].id+'">'+respon.hasil[i].nama_barang+'</div>',
					'<div id="list_motif'+respon.hasil[i].id+'">'+respon.hasil[i].nama_motif+'</div>',
					'<div id="list_motif_barang'+respon.hasil[i].id+'">'+respon.hasil[i].nama_corak+'</div>',
					'<div id="list_jumlah'+i+'">'+respon.hasil[i].jumlah+'</div>',
                    '<div>'+
                    '<input type="number" min="0" step="1" class="form-control text-right" name="upah_satuan_[]" value="'+upah_petenun+'" onkeyup="get_total_upah(num='+i+', $(this))">'+
                    '</div>',
                    '<div>'+
                    '<input type="number" min="0" step="1" class="form-control text-right" name="upah_satuan_2_[]" value="'+upah_makelar+'" onkeyup="get_total_upah(num='+i+', $(this))">'+
                    '</div>',
                    '<div>'+
                    '<input type="number" step="1" class="form-control text-right" name="upah_pick_[]" value="'+upah_pick+'" onkeyup="get_total_upah(num='+i+', $(this))">'+
                    '</div>',

					// '<div><p type="text" id="list_upah'+i+'" disabled="true"'+
					// 'onkeyup="get_total_upah(num='+i+',jumlah='+respon.hasil[i].jumlah+',banyak='+respon.hasil.length+')"'+
					// '">'+accounting.formatMoney(upah)+'</p></div>',

					'<div id="list_upah_total'+respon.hasil[i].id+'" class="text-right"><p type="text" id="list_upah_total'+i+'" disabled="true"">'+accounting.formatMoney(upah_total)+'</p></div>',
					]).draw(false);

			}
			$("input[name='upah_satuan_[]']").trigger("keyup");
			$("input[name='upah_satuan_2_[]]'").trigger("keyup");
			$("input[name='upah_pick_[]']").trigger("keyup");
		}else{
			table_hasil.clear().draw();	
		}

		if(respon.status_lain2==1){
			table_lain2.clear().draw();
			for(i in respon.lain2){
				table_lain2.row.add(['<div><center></center></div>',
					'<div id="list_jenis'+respon.lain2[i].id_upah+'">'+respon.lain2[i].jenis+'</div>',
					'<div id="list_upah'+respon.lain2[i].id_upah+'">'+accounting.formatMoney(respon.lain2[i].jumlah)+'</div>',
					'<div><button type="button" class="btn bg-warning-400" onclick="edit_tablell('+respon.lain2[i].id_upah+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button"  onclick="hapusll(d=$(this))"><i class="icon-eraser"></i></button></div>'+
					'<input type="hidden" id="tabel_id2'+respon.lain2[i].id_upah+'" name="tabel_id2[]" value="'+respon.lain2[i].id_upah+'">'+
					'<input type="hidden" id="tabel_jenis'+respon.lain2[i].id_upah+'" name="tabel_jenis[]" value="'+respon.lain2[i].jenis+'">'+
					'<input type="hidden" id="tabel_upah'+respon.lain2[i].id_upah+'" name="tabel_upahll[]" value="'+respon.lain2[i].jumlah+'">'
					]).draw(false);
				tb_no_ll++;
			}
		}else{
			table_lain2.clear().draw();
		}
	}

	function getLapHasil(param, owner, proses) {

		$.ajax({
			url:"{{ url('upahproduksi/getlaporanhasil') }}"+"/"+param+"/"+owner+"/"+proses,
			type:'get',
			dataType:'json',
			success:function(result){
				var select = '';
				$("#lap_hasil").html("");
				$.each( result, function( key, value ) {
					select += '<option value="'+value.id_barangmasuk+'">'+value.noAuto+value.noAuto_ext+'</option>';
				});
				$("#lap_hasil").append(select);
				$("#lap_hasil").val(c_laphasil).trigger("change");
			}
		})
	}

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

		var id_jenis = $("[name=jenis]").val(); // lokasi produksi
		
		$.ajax({
			url: "{{ url('upahproduksi/get_proses') }}",
			type: 'post',
			data: {rekanan : rekanan, pegawai : pegawai, id_jenis : id_jenis},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=proses]").html('');
					$("[name=proses]").append('<option value="" kode="">--- Pilih Proses ---</option>');
					for(i in result.data){
						var ket_nomor = "";
						if(result.data[i].id_workorder != '' || result.data[i].id_workorder != 0){
							if(result.data[i].no_workorder==null || result.data[i].no_workorder==""){
								$nomor = "(Tanpa SPK)";

							}else{
								$nomor = result.data[i].no_workorder+' '+'(Nomor SPK)';

							}
						}else{
							if(result.data[i].no_workorder_barang==null || result.data[i].no_workorder_barang==""){
								$nomor = "(Tanpa Nomor Order)";

							}else{
								$nomor = result.data[i].no_workorder_barang+' '+'(Nomor Order)';

							}
						}

						
						$("[name=proses]").append('<option value="'+result.data[i].id_barangmasuk+'" barangkeluar="'+result.data[i].id_barangkeluar+'" spk="'+result.data[i].id_workorder+'" workorder="'+result.data[i].id_workorder_barang+'" proses="'+result.data[i].id_proses+'" rekanan="'+result.data[i].id_rekanan+'" pegawai="'+result.data[i].id_pegawai+'">'+$nomor+result.data[i].nama_proses+'</option>');
					}
				}else{
					$("[name=proses]").html('');
				}
			},
		}).done(function(){
			$("[name=proses]").val(c_barangmasuk).change();
		})
	}

	function edit_tablell(id){
		$("[name=popup_id_table_lain_lain]").val(id);
		$("#jenis_lain_lain").val($("#tabel_jenis"+id).val());
		$("#upah_lain_lain").val($("#tabel_upah"+id).val());		
		$("#help_popup_upah_lain_lain").text(accounting.formatMoney($("#tabel_upah"+id).val()));
		$("#exampleModal2").modal('show');
	}

	function hapusll(d){
		table_lain2.row(d.parents('tr')).remove().draw();
		set_sub_total_lain2();
	}

	function get_total_upah(id, this_){
		var row = this_.parents("tr");
		var sub_total=0;
		
		get_row_total(row);
		$.each($("input[name='upah_total_[]']"), function(id, item){
			sub_total += parseFloat(item.value);
		});
		
		$("[name=sub_total1]").val(sub_total);
		$("#sub_total1").text(accounting.formatMoney(sub_total));
		$("#td_total").text(accounting.formatMoney(sub_total));
		$("input[name='td_total']").val(sub_total);
		// $("#td_netto").text(accounting.formatMoney(sub_total));
		set_sub_total_lain2();
		// $.when().then(function(){
		// });
		
	}

	function get_row_total(row_) {
		var jumlah_barang = row_.find("input[name='jumlah_barang_[]']").val();
		var upah_satuan = row_.find("input[name='upah_satuan_[]']").val();
		var upah_satuan_2 = row_.find("input[name='upah_satuan_2_[]']").val();
		var upah_pick = row_.find("input[name='upah_pick_[]']").val();
		var total = parseFloat(jumlah_barang)*(parseFloat(upah_satuan)+parseFloat(upah_satuan_2)+parseFloat(upah_pick));
		
		var input_upah_total = row_.find("input[name='upah_total_[]']");
		input_upah_total.val(total);

		row_.find("p").html(accounting.formatMoney(total));
	}

	function set_sub_total_lain2(){
		var sub_total2=0;
		// for(var i=10000;i<=tb_no_ll;i++){
		// 	var data=parseFloat($("#tabel_upah"+i).val());
		// 	if(!isNaN(data)){
		// 		sub_total2 += data;
		// 		// console.log(id_terakhir);
		// 	}
		// }

		$.each($("[name='tabel_upahll[]']"), function(id, item){
			if(!isNaN(item.value)){
				sub_total2 += parseFloat(item.value);
				// console.log(id_terakhir);
			}	
		});
		// console.log(sub_total2);
		$("#sub_total2").text(accounting.formatMoney(sub_total2));
		var td_total = parseFloat($("[name=sub_total1]").val()) + sub_total2;
		$("[name=td_total]").val(td_total);
		$("#td_total").text(accounting.formatMoney(td_total));
		netto();
	}

	function netto(){
		var total = parseFloat($("[name=td_total]").val()) || 0;
		// var potongan = parseFloat($("[name=td_potongan]").val()) || 0;
		// var ongkir = parseFloat($("[name=td_ongkir]").val()) || 0;
		var potongan = parseFloat(0);
		var ongkir = parseFloat(0);
		var netto = parseFloat(0);
		var ppn = parseFloat(0);

		if($('select[name=pajak]').val()==1){
			ppn = parseFloat((total-potongan)*(10/100));
		}

		netto = total-potongan+ppn+ongkir; //+bunga
		// $("[name=td_potongan]").val(potongan);
		// $("#help_popup_potongan").text(accounting.formatMoney(potongan));

		$("[name=td_pajak]").val(ppn);
		$("#help_popup_pajak").text(accounting.formatMoney(ppn));

		// $("[name=td_ongkir]").val(ongkir);
		// $("#help_popup_ongkir").text(accounting.formatMoney(ongkir));

		$("#td_netto").text(accounting.formatMoney(netto));
		$("[name=td_netto]").val(netto);
	}
	
</script>
@endsection