@extends('layout')


@section('content')

<style type="text/css">
.popover-content{
	padding: 5px !important;
	text-align: left !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="{{ url('barangmasuk') }}" 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_upah" method="post" action="{{ url('upahproduksi/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['data']['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 disabled-select" name="jenis" required="" readonly="">
        					@php $sel = $data['data']['id_jenis']; @endphp
        					<option value="" {{ ($sel=="")? "selected=''":"" }}>--- Pilih Upah ---</option>
        					<option value="1" {{ ($sel=="1")? "selected=''":"" }}>Borongan</option>
        					<option value="2" {{ ($sel=="2")? "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">
        			<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" readonly="">
        				</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" readonly="">
	    					<option value="">--- Pilih Lokasi ---</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="" readonly=""/>
        			</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="Nomor Transaksi" value="{{ $data['data']['nomor'] }}" id="nomor" required="" />
        			</div>
        		</div>
        	</div>
        	
        	<div class="form-group col-sm-12 table-responsive">

        		<div class="tabel-opsi-upah-borongan">

        		<h5>Hasil</h5>
        		<hr>
        		@if($data['jenis_trx']==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>Jumlah</th>
							<th width="20%">Upah</th>
							<th width="20%">Total</th>							
        				</tr>
        			</thead>
        			<tbody>
        				
        			</tbody>
        			<tfoot>
        				<tr>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th>Sub Total</th>
        					<th><p type="text" name="sub_total" id="sub_total1" disabled="true" value="0"></p></th>
        				</tr>
        			</tfoot>
        		</table>
        		@else
        		<table class="table table-striped" id="table_hasil">
        			<thead>
        				<tr>
        					<th>No</th>
							<th>Kode Barang</th>
							<th>Nama Barang</th>
							<th>Warna</th>
							<th>Jumlah</th>
							<th>Satuan</th>
							<th>Gudang</th>
							<th>Corak</th>
							<th>Motif</th>
							<th>Kualitas</th>
							<th>Dudulan</th>
							<th>Label</th>
							<th>Kemasan</th>
							<th>Kemasan Satuan</th>
							<th>Kemasan 10 Potong</th>
							<th>Kemasan Dus Ball</th>
        				</tr>
        			</thead>
        			<tbody>
        				
        			</tbody>
        			<tfoot>
        				<tr>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th></th>
        					<th>Sub Total</th>
        					<th><p type="text" name="sub_total" id="sub_total1" disabled="true" value="0"></p></th>
        				</tr>
        			</tfoot>
        		</table>
        		@endif

        		</div>

        		<div class="tabel-opsi-upah-harian">
    			
		        	<h5>Harian</h5>
		        	<hr>

		    		<a href="#" class="btn btn-success" id="btn_tambah_item">Tambah</a>

		    		<table class="table table-striped" id="table_barang">
		    			<thead>
		    				<tr>
		    					<th>No.</th>
		    					<th>Keterangan</th>
		    					<!-- <th>Perkiraan</th> -->
		    					<th>Satuan</th>
		    					<th>Upah</th>
		    					<th>Total Upah</th>
		    					<th class="text-center"></th>
		    				</tr>
		    			</thead>
		    			<tbody>
		    				
		    			</tbody>
		    			<tfoot>
		    				<tr>
		    					<th></th>
		    				</tr>
		    			</tfoot>
		    		</table>

        		</div>

        		<div class="tabel-opsi-upah-hasil-lainnya">
		    		<h5>Hasil Lainnya</h5>
		    		<table class="table table-striped" id="table_hasil_lainnya">
		    			<thead>
		    				<tr>
		    					<th>No.</th>
		    					<th>Keterangan</th>
		    					<th>Motif</th>
		    					<th>Nominal (Rupiah)</th>
		    				</tr>
		    			</thead>
		    			<tbody>
		    				
		    			</tbody>
		    		</table>
        		</div>

        	</div>

        <!-- <?php if ($data['jenis_trx']==2){ ?> -->

        <!-- <?php } else if($data['jenis_trx']==1){ ?> -->

        <!-- <?php } ?> -->

        <div class="col-sm-12 form-group tabel-opsi-upah-lain2"> 
        	<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>Jenis Transaksi</th>
						<th>Keterangan</th>
						<th>Jumlah</th>
						<th></th>							
    				</tr>
    			</thead>
    			<tbody>
    				
    			</tbody>
    			<tfoot>
    				<tr>
    					<th></th>
    					<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">
        	<a href="{{url('upahproduksi')}}" 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>
</div>

<!---Modal--->

<div class="modal fade" id="exampleModal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" autocomplete="off">
	      			<input type="hidden" name="popup_id_table">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Keterangan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_keterangan">
								@foreach($data['keterangan_upah_harian'] AS $key=>$value)
								<option value="{{ $value->id_upahket }}" nama="{{ $value->nama_upahket }}">{{ $value->nama_upahket }}</option>
								@endforeach
	      					</select>
	      				</div>
	      			</div>
	      			<!-- <div class="form-group">
	      				<label class="control-label col-sm-4">Kode Perkiraan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_perkiraan">
	      						
	      					</select>
	      				</div>
	      			</div> -->
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Satuan</label>
	      				<div class="col-sm-5">
	      					<input type="text" class="form-control col-sm-6" name="popup_satuan">
	      				</div>
	      				<div class="col-sm-3">
	      					<select class="form-control select-search" name="popup_satuan_waktu">
								@foreach($data['satuan'] AS $key=>$value)
								<option value="{{ $value->id_satuan }}" nama="{{ $value->nama_satuan }}">{{ $value->nama_satuan }}</option>
								@endforeach	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Gaji Satuan</label>
	      				<div class="col-sm-8">
	      					<input type="text" class="form-control number-only text-right" name="popup_upah">
	      					<p class="pull-right" id="help_popup_upah"></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">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>


<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">Jenis Transaksi</label>
	      				<div class="col-sm-8">
	      					<select class="form-control" name="jenis_transaksi" id="jenis_transaksi">
	      						<option value="TAMBAHAN">UPAH LAIN</option>
	      						<option value="KASBON">POTONGAN KAS BON</option>
	      					</select>
	      				</div>
	      			</div>
	      			<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">
	      				</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 -->

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


<script type="text/javascript">

	// tombol tambah upah harian
	$("#btn_tambah_item").click(function(){
		$("[name=popup_id_table]").val('');
		$("[name=popup_keterangan]").val(null).trigger('change');
		$("[name=popup_perkiraan]").val(null).trigger('change');
		$("[name=popup_satuan]").val(1);
		$("[name=popup_satuan_waktu]").val(99).trigger('change');
		$("[name=popup_upah]").val(0).addClass("text-right").trigger("keyup");

		// $("#help_popup_upah").text(accounting.formatMoney(0));
		$("#exampleModal").modal('show');
	});

	$("[name=popup_upah]").keyup(function(){
		var rp = accounting.formatMoney($("[name=popup_upah]").val());
		$("#help_popup_upah").text(rp);
	});

	// tombol tambah lain-lain
	$("#btn_tambah_item_lain_lain").click(function(){
		$("[name=popup_id_table_lain_lain]").val('');
		$("#jenis_transaksi").val('TAMBAHAN');
		$("#jenis_lain_lain").val('');
		$("#upah_lain_lain").val(0).addClass("text-right").trigger("keyup");
		
		// $("#help_popup_upah_lain_lain").text(accounting.formatMoney(0));
		$("#exampleModal2").modal('show');
	});

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

	function exchangeField(id)
	{
		$("#lbl_upah"+id).hide();
		$("#txt_upah"+id).attr('type', 'number');
	}

	/*==================================================*/

	var table_barang;
	var table_hasil;
	var table_hasil_lainnya;
	var table_lain2;
	var tb_no = parseInt(10000);
	var tb_no_ll= parseInt(10000);
	var nama_transaksi= <?php echo $data['jenis_trx'] ?>;

	var form_submit_upah = $("#form_submit_upah");

    /*=====*/
    var id = "{{$data['data']['id_upahproduksi']}}";
    var c_rekanan 	= "{{$data['data']['id_rekanan']}}";
    var c_pegawai 	= "{{$data['data']['id_pegawai']}}";
    var c_proses  	= "{{ isset($data['laporan_hasil']) ? $data['laporan_hasil']->id_barangmasuk : $data['data']['id_barangmasuk'] }}";
    var c_barangmasuk  	= "{{$data['data']['id_barangmasuk']}}";
    var c_jenis 	= "{{$data['data']['id_jenis']}}"; 
    var c_lokasi 	= "{{$data['data']['jenis']}}"; 
    // console.log(c_jenis+' - '+c_rekanan+' - '+c_pegawai);
    /*=====*/

	$(document).ready(function(){
		// tabel-opsi-upah-borongan : tabel hasil utk barang dgn corak--motif/corak--kemasan
		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();
        })

		// tabel-opsi-upah-lain2 : tabel lain2 utk data lain-lain
        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();

        // tabel-opsi-upah-hasil-lainnya : tabel upah utk proses dan motif tertentu 
        table_hasil_lainnya = $("#table_hasil_lainnya").DataTable({
        	"paging": false
        });
		table_hasil_lainnya.on( 'order.dt search.dt', function () {
            table_hasil_lainnya.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

        // tabel-opsi-upah-harian : tabel untuk upah harian
		table_barang = $("#table_barang").DataTable({});
		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_satuan();
        // get_ket();
        // get_perkiraan();
        $("[name=id_jenis]").val(c_lokasi).trigger("change");
        $("[name=jenis]").trigger("change");

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

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

        if(id != ''){
        	get_edit(id);
        	if(c_rekanan != '' || c_rekanan != 0){
        		var FunctionOne = function (){
        			var r = $.Deferred();
					$("[name=jenis]").val(c_jenis).trigger('change');
					$("[name=rekanan]").val(c_rekanan).trigger('change');
				
					setTimeout(function () {
						r.resolve();
					}, 3000);
					return r;
        		}
        		var FunctionTwo = function () {
        			$("[name=proses]").val(c_barangmasuk).trigger('change');   
				};

				FunctionOne().done(FunctionTwo);
        	}else{

        		var FunctionOne = function (){        		
        		var r = $.Deferred();
	        		$("[name=jenis]").val(c_jenis).trigger('change');
					$("[name=pegawai]").val(c_pegawai).trigger('change');
				 
					setTimeout(function () {
						r.resolve();
					}, 3000);
					return r;
        		}
        		var FunctionTwo = function () {
        			$("[name=proses]").val(c_proses).trigger('change');   
				};

				FunctionOne().done(FunctionTwo);
        	}

        }else{
        	var id_tabel= <?php echo $data['id_tabel']; ?>;
        	if(id_tabel==1){
        		get_by_id();
	        }else{
	        	get_by_id_barang_masuk();
	        }

        }
        
	})

	get_nomor_upah($("#nomor"));

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

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

		});
	});

	// jenis upah : borongan-harian
	$("[name=jenis]").change(function(){
		var jenis = $(this).val();
		if(jenis != '' || jenis != 0 || !isNaN(jenis)){
			if(jenis == '1'){ // opsi borongan
				$(".tabel-opsi-upah-borongan").removeClass("hidden");
				if(!$(".tabel-opsi-upah-harian").hasClass("hidden"))
				{
					$(".tabel-opsi-upah-harian").addClass("hidden");
				}

				if(!$(".tabel-opsi-upah-hasil-lainnya").hasClass("hidden"))
				{
					$(".tabel-opsi-upah-hasil-lainnya").addClass("hidden");
				}

				// $("#tanggal_harian").addClass('hidden');
			}else if(jenis == '2'){ // opsi harian
				if(!$(".tabel-opsi-upah-borongan").hasClass("hidden")){
					$(".tabel-opsi-upah-borongan").addClass("hidden");
				}
				$(".tabel-opsi-upah-harian").removeClass("hidden");

				if(!$(".tabel-opsi-upah-hasil-lainnya").hasClass("hidden"))
				{
					$(".tabel-opsi-upah-hasil-lainnya").addClass("hidden");
				}

				// $("#tanggal_harian").removeClass('hidden');
			}else{
				if(!$(".tabel-opsi-upah-borongan").hasClass("hidden")){
					$(".tabel-opsi-upah-borongan").addClass("hidden");
				}
				if(!$(".tabel-opsi-upah-harian").hasClass("hidden"))
				{
					$(".tabel-opsi-upah-harian").addClass("hidden");
				}
				if(!$(".tabel-opsi-upah-hasil-lainnya").hasClass("hidden"))
				{
					$(".tabel-opsi-upah-hasil-lainnya").addClass("hidden");
				}

				// $("#tanggal_harian").addClass('hidden');
			}
		}
	})	

	$("[name=proses]").change(function(){
		var set_id_upahproduksi = $("input[type=hidden][name=id_upahproduksi]").val();
		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() || 0;
		var id_pegawai = $("[name=pegawai]").val() || 0;
		var id_jenis = $("[name=jenis]").val();

		$("[name=id_barangkeluar").val(id_barangkeluar);
		$("[name=spk").val(id_workorder);
		$("[name=workorder").val(id_workorder_barang);
		$("[name=id_proses").val(id_proses);
		var id_lain2= <?php echo $data['id_trans']; ?>;

		// console.log(id_jenis+' - '+id_rekanan+' - '+id_pegawai);


		if(id_rekanan != '' || id_rekanan != 0){ // tabel hasil untuk rekanan
			$.ajax({
				url: "{{ url('upahproduksi/get_rekanan')}} ",
				type: 'post',
				data: {id_upahproduksi : set_id_upahproduksi, 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();
					table_hasil_lainnya.clear().draw();
					if(respon.status_hasil == 1){
						if(nama_transaksi==2){ // tabel lama : tabel hasil corak--kemasan (rekanan)
							for(i in respon.hasil){
								table_hasil.row.add(['<div><center></center></div>',
									'<div id="list_kode'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].kode_barang+'</div>',
									'<div id="list_nama'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_barang+'</div>',
									'<div id="list_komposisi'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_komposisi+'</div>',
									'<div id="list_jumlah'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].jumlah+'</div>',
									'<div id="list_namasatuan'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_satuan+'</div>',
									'<div id="list_gudang'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_gudang+'</div>',
									'<div id="list_corak'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_corak+'</div>',
									'<div>'+respon.hasil[i].nama_motif+'</div>',
									'<div>'+respon.hasil[i].nama_kualitas+'</div>',
									'<div>'+respon.hasil[i].kualitas_dudul+'</div>',
									'<div>'+respon.hasil[i].nama_label+'</div>',
									'<div>'+respon.hasil[i].nama_kemasan+'</div>',
									'<div>'+respon.hasil[i].nama_kemasansatuan+'</div>',
									'<div>'+respon.hasil[i].nama_kemasansepuluh+'</div>',
									'<div>'+respon.hasil[i].nama_kemasanseratus+'</div>'
									]).draw(false);
								}
						}else{ // tabel baru : tabel hasil corak--motif (rekanan)
							var sub_total1=0;
							for(i in respon.hasil){

								var upah= parseFloat(respon.hasil[i].upah);
                                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;
                                
                                for(j in respon.upah){

                                    if(respon.upah[j].id_proses==idProses && respon.upah[j].id_barang==idBarang){
                                        upah = parseFloat(respon.upah[j].nominal);

                                    }else if(respon.upah[j].id_proses==idProses && respon.upah[j].id_corak==idMotifBarang){
                                        upah = parseFloat(respon.upah[j].nominal);

                                    }
                                    else if(respon.upah[j].id_proses==idProses && respon.upah[j].id_motif==idMotif){
                                        upah = parseFloat(respon.upah[j].nominal);

                                    }
                                }
								//alert(isNaN(upah));
								if (isNaN(upah)){
									upah=0;
								}
								var jumlah=parseFloat(respon.hasil[i].jumlah);
								var total_upah= upah*jumlah;
								sub_total1 +=total_upah;
								table_hasil.row.add(['<div><center></center></div>',                                    
                                    '<input type="hidden" name="id_pbb_[]" value="'+respon.hasil[i].id+'">'+
									'<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="upah_total_[]" value="'+total_upah+'">'+
                                    '<div id="list_kode'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].kode_barang+'</div>',
									'<div id="list_nama'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_barang+'</div>',
									'<div id="list_jumlah'+i+'">'+respon.hasil[i].jumlah+'</div>',

                                    '<div><a href="javascript:void(0);" class="pull-right" id="lbl_upah'+i+'" onclick="exchangeField('+i+')">'+upah+'</a><input type="hidden" min="0" step="1" class="form-control text-right" id="txt_upah'+i+'" name="upah_satuan_[]" value="'+upah+'" onkeyup="get_total_upah(num='+i+',jumlah='+respon.hasil[i].jumlah+',banyak='+respon.hasil.length+', $(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_masuk+'"><p type="text" id="list_upah_total'+i+'" disabled="true"">'+accounting.formatMoney(total_upah)+'</p></div>',
									]).draw(false);

							}
							$("#sub_total1").text(accounting.formatMoney(sub_total1));
						}

					}else{
							table_hasil.clear().draw();	
					}

					if(respon.status_upah == 1){
						if(respon.s_upah == 1){ // tabel-opsi-upah-hasil-lainnya (rekanan)
						for(i in respon.upah){
							var keterangan = '';
							var motif = '';
							if(respon.upah[i].id_barang != '' || respon.upah[i].id_barang != 0){
								var keterangan = respon.upah[i].kode_barang+' - '+respon.upah[i].nama_barang; 
							}

							if(respon.upah[i].id_corak != '' || respon.upah[i].id_corak != 0){
								var motif_barang = respon.upah[i].motif_keterangan; 
							}

							table_hasil_lainnya.row.add(['<div><center></center></div>',
								'<div>'+keterangan+'</div>',
								'<div>'+respon.upah[i].motif_keterangan+'</div>',
								'<div>'+respon.upah[i].nominal+'</div>'
								]).draw(false);
						}

						}else{ // tabel-opsi-upah-harian (rekanan) [masih belum sesuai]
							for(i in respon.upah){	
							table_hasil_lainnya.row.add(['<div><center></center></div>',
								'<div>Gaji Harian</div>',
								'<div></div>',
								'<div>'+accounting.formatMoney(respon.upah[i].upah_harian)+'/Hari </div>'
								]).draw(false);
							}

							for(i in respon.upah){	
							table_hasil_lainnya.row.add(['<div><center></center></div>',
								'<div>Gaji Lembur</div>',
								'<div></div>',
								'<div>'+accounting.formatMoney(respon.upah[i].upah_lembur)+'/Jam </div>'
								]).draw(false);
							}
						}

					}else{
							table_hasil_lainnya.clear().draw();	
					}

					// 
					if(respon.status_lain2==1){ // tabel-opsi-upah-lain2 (rekanan)
						table_lain2.clear().draw();
						for(i in respon.lain2){
							var jt = respon.lain2[i].jenis_transaksi == "KASBON" ? "BAYAR KAS BON" : "UPAH LAIN";
							table_lain2.row.add(['<div><center>'+no+'</center></div>',
								'<div id="list_jenis_transaksi'+respon.lain2[i].id_upah+'">'+jt+'</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_transaksi'+respon.lain2[i].id_upah+'" name="tabel_jenis_transaksi[]" value="'+respon.lain2[i].jenis_transaksi+'">'+
								'<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+'">'+
								'<input type="hidden" id="tabel_upah'+tb_no_ll+'" value="'+respon.lain2[i].jumlah+'">',
								]).draw(false);
							tb_no_ll++;
							set_sub_total_lain2();
						}
					}else{
						table_lain2.clear().draw();
					}
				}
			})

			}else{ // tabel hasil untuk pegawai

				$.ajax({
				url: "{{ url('upahproduksi/get_pegawai')}} ",
				type: 'post',
				data: {id_upahproduksi : set_id_upahproduksi, 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_pegawai : 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();	
					table_hasil_lainnya.clear().draw();	
					if(respon.status_hasil == 1){
						if(nama_transaksi==2){ // tabel lama : tabel utk hasil corak--kemasan (pegawai)
							for(i in respon.hasil){
								table_hasil.row.add(['<div><center></center></div>',
									'<div id="list_kode'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].kode_barang+'</div>',
									'<div id="list_nama'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_barang+'</div>',
									'<div id="list_komposisi'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_komposisi+'</div>',
									'<div id="list_jumlah'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].jumlah+'</div>',
									'<div id="list_namasatuan'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_satuan+'</div>',
									'<div id="list_gudang'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_gudang+'</div>',
									'<div id="list_corak'+respon.hasil[i].id_masuk+'">'+respon.hasil[i].nama_corak+'</div>',
									'<div>'+respon.hasil[i].nama_motif+'</div>',
									'<div>'+respon.hasil[i].nama_kualitas+'</div>',
									'<div>'+respon.hasil[i].kualitas_dudul+'</div>',
									'<div>'+respon.hasil[i].nama_label+'</div>',
									'<div>'+respon.hasil[i].nama_kemasan+'</div>',
									'<div>'+respon.hasil[i].nama_kemasansatuan+'</div>',
									'<div>'+respon.hasil[i].nama_kemasansepuluh+'</div>',
									'<div>'+respon.hasil[i].nama_kemasanseratus+'</div>'
									]).draw(false);
							}
						}else{ // tabel baru : tabel utk hasil corak--motif--kemasan (pegawai)
							var sub_total1=0;
							// console.log(respon);
							for(i in respon.hasil){
                                var upah= parseFloat(respon.hasil[i].upah);
                                var idPbb = respon.hasil[i].id_detail;
                                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 idProsesjahit = respon.hasil[i].id_jahit || 0;
                                var idLokasi = respon.hasil[i].id_lokasi || 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;
                                /* === */

                                // ganti input upah_satuan dengan data master upah

                                var pro = [];
                                for(j in respon.upah){
                                	// console.log(idProses+" - "+get_prosesjahit()+"\n"+respon.upah[j].id_motif_barang+" - "+idMotifBarang+"\n"+respon.upah[j].id_motif+" - "+idMotif);
                                	if(jQuery.inArray(idProses, get_prosesjahit()) !== -1){
                                		if(respon.upah[j].id_proses==idProsesjahit && respon.upah[j].id_motif_barang==idMotifBarang && respon.upah[j].id_motif==idMotif && respon.upah[j].id_lokasi == idLokasi){
									        upah = parseFloat(respon.upah[j].nominal);
									        

	                                    }else{
											
	                                    }	
                                	}else{
                                		if(respon.upah[j].id_proses==idProses && respon.upah[j].id_barang==idBarang && respon.upah[j].id_motif_barang==idMotifBarang && respon.upah[j].id_motif==idMotif && respon.upah[j].id_lokasi == idLokasi){
									        upah = parseFloat(respon.upah[j].nominal);
									        
	                                    }
                                	}

                //  					if(respon.upah[j].id_proses==idProses && respon.upah[j].id_barang==idBarang && respon.upah[j].id_motif_barang==idMotifBarang && respon.upah[j].id_motif==idMotif){
								        // upah = parseFloat(respon.upah[j].nominal);
                //                     }
                                    
                                }

								//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;'>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>"+
											"<tr>"+
												"<th style='padding:1px !important;'>Jenis</th>"+
												"<td style='padding:1px !important;'>"+respon.hasil[i].nama_proses_+"</td>"+
											"</tr>"+
										"</tbody>"+
									"</table>";

								var jumlah=parseFloat(respon.hasil[i].jumlah);
								var total_upah= upah*jumlah;
								sub_total1 +=total_upah;
								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="'+total_upah+'">'+

                                    '<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>'+'<a href="javascript:void(0);" class="pull-right" id="lbl_upah'+i+'" onclick="exchangeField('+i+')">'+upah+'</a><input type="hidden" min="0" step="1" class="form-control text-right" id="txt_upah'+i+'" name="upah_satuan_[]" value="'+upah+'" onkeyup="get_total_upah(num='+i+',jumlah='+respon.hasil[i].jumlah+',banyak='+respon.hasil.length+', $(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(total_upah)+'</p></div>',
									]).draw(false);

							}
							$("#sub_total1").text(accounting.formatMoney(sub_total1));
						}

					}else{
							table_hasil.clear().draw();	
					}

					if(respon.status_upah == 1){
						if(respon.s_upah == 1){ // tabel-opsi-upah-hasil-lainnya (pegawai)
						for(i in respon.upah){
							var barang = '';
							var motif = '';
							if(respon.upah[i].id_barang != '' || respon.upah[i].id_barang != 0){
								var barang = respon.upah[i].kode_barang+' - '+respon.upah[i].nama_barang; 
							}

							if(respon.upah[i].id_motif_barang != '' || respon.upah[i].id_motif_barang != 0){
								var motif_barang = respon.upah[i].motif_keterangan; 
							}

							table_hasil_lainnya.row.add(['<div><center></center></div>',
								'<div>'+barang+'</div>',
								'<div>'+respon.upah[i].motif_keterangan+'</div>',
								'<div>'+respon.upah[i].nominal+'</div>'
								]).draw(false);
						}
						}else{ // tabel-opsi-upah-harian (pegawai) [masih belum sesuai]
							for(i in respon.upah){	
							table_hasil_lainnya.row.add(['<div><center></center></div>',
								'<div>Gaji Harian</div>',
								'<div></div>',
								'<div>'+accounting.formatMoney(respon.upah[i].upah_harian)+'/Hari </div>'
								]).draw(false);
							}

							for(i in respon.upah){	
							table_hasil_lainnya.row.add(['<div><center></center></div>',
								'<div>Gaji Lembur</div>',
								'<div></div>',
								'<div>'+accounting.formatMoney(respon.upah[i].upah_lembur)+'/Jam </div>'
								]).draw(false);
							}
						}

					}else{
							table_hasil_lainnya.clear().draw();	
					}

					if(respon.status_upah_harian==1){
						table_barang.clear().draw();
						for(i in respon.upah_harian){
							total_upah = parseFloat(respon.upah_harian[i].satuan)*parseFloat(respon.upah_harian[i].upah);
							table_barang.row.add([
									'<div><center></center></div>',
									'<div id="list_ket'+respon.upah_harian[i].id+'">'+respon.upah_harian[i].nama_keterangan+'</div>',
									// '<div id="list_perkiraan'+respon.upah_harian[i].id+'">'+kode_perkiraan+' - '+nama_perkiraan+'</div>',
									'<div id="list_satuan'+respon.upah_harian[i].id+'">'+respon.upah_harian[i].satuan+' '+respon.upah_harian[i].nama_satuan+'</div>',
									'<div id="list_upah'+respon.upah_harian[i].id+'">'+accounting.formatMoney(respon.upah_harian[i].upah)+'</div>',
									'<div id="list_total'+respon.upah_harian[i].id+'">'+accounting.formatMoney(total_upah)+'</div>',
									'<div><button type="button" class="btn bg-warning-400" onclick="edit_table('+respon.upah_harian[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.upah_harian[i].id+'" name="tabel_id[]" value="'+respon.upah_harian[i].id+'">'+
									'<input type="hidden" id="tabel_ket'+respon.upah_harian[i].id+'" name="tabel_ket[]" value="'+respon.upah_harian[i].keterangan+'">'+
									// '<input type="hidden" id="tabel_perkiraan'+respon.upah_harian[i].id+'" name="tabel_perkiraan[]" value="'+id_perkiraan+'">'+
									'<input type="hidden" id="tabel_satuan'+respon.upah_harian[i].id+'" name="tabel_satuan[]" value="'+respon.upah_harian[i].satuan+'">'+
									'<input type="hidden" id="tabel_satuanwaktu'+respon.upah_harian[i].id+'" name="tabel_satuanwaktu[]" value="'+respon.upah_harian[i].satuan_waktu+'">'+
									'<input type="hidden" id="tabel_nama_satuanwaktu'+respon.upah_harian[i].id+'" name="tabel_nama_satuanwaktu[]" value="'+respon.upah_harian[i].nama_satuan+'">'+
				                    '<input type="hidden" id="tabel_upah'+respon.upah_harian[i].id+'" name="tabel_upah[]" value="'+respon.upah_harian[i].upah+'">'+
				                    '<input type="hidden" id="tabel_total'+respon.upah_harian[i].id+'" name="tabel_total[]" value="'+total_upah+'">'
								]).draw(false);
						}

					}else{
						table_barang.clear().draw();
					}

					if(respon.status_lain2==1){ // tabel-opsi-upah-lain2 (pegawai)
						table_lain2.clear().draw();
						for(i in respon.lain2){
							var jt = respon.lain2[i].jenis_transaksi == "KASBON" ? "BAYAR KAS BON" : "UPAH LAIN";
							table_lain2.row.add(['<div><center>'+no+'</center></div>',
								'<div id="list_jenis_transaksi'+respon.lain2[i].id_upah+'">'+jt+'</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_transaksi'+respon.lain2[i].id_upah+'" name="tabel_jenis_transaksi[]" value="'+respon.lain2[i].jenis_transaksi+'">'+
								'<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+'">'+
								'<input type="hidden" id="tabel_upah'+tb_no_ll+'" value="'+respon.lain2[i].jumlah+'">',
								]).draw(false);
							tb_no_ll++;
							set_sub_total_lain2();
						}
					}else{
						table_lain2.clear().draw();
					}

					
				}
			})


		}
		

		


	}) 
	// $( "#nomor" ).keyup(function() {
	//   alert($( "#nomor" ).val());
	// });

	function get_nomor_upah(this_) {
		$.ajax({
			type : "get",
			url : "{{ url('upahproduksi/get_nomor_upah') }}",
			data : { id_upahproduksi : '{{ $data["data"]["id_upahproduksi"]}}' },
			dataType : "json",
			success : function(respon){
				$("#nomor").val(respon.nomor);
			}
		});
	}
	function get_total_upah(id,jumlah,banyak, this_){
		var row = this_.parents("tr");

		$.when(get_row_total(row)).then(function(){
			// var upah=$("#list_upah"+num).val();
			// var total=jumlah*upah;
			// $("#list_upah_total"+num).val(total);
			// for(var i=0;i<banyak;i++){
			// 	sub_total += parseFloat($("#list_upah_total"+i).val());
			// }
			var sub_total=0
			$.each($("input[name='upah_total_[]']"), function(id, item){
				sub_total += parseFloat(item.value);
			});

			$("#sub_total1").val(sub_total).text(accounting.formatMoney(sub_total));
		});
		
	}

	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 total = jumlah_barang*upah_satuan;

		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;
		$("[name^='tabel_upahll']").each(function(index, item){
			var data=parseFloat(item.value);
			if(!isNaN(data)){
				if($("input[name^='tabel_jenis_transaksi']:eq("+index+")").val() == "TAMBAHAN") {
					sub_total2 += data;
				} else {
					sub_total2 -= data;
				}
			}
		});
		// $(`.item:eq(${index})`).val()
		// console.log($("input[name^='tabel_jenis_transaksi']").val());
		$("#sub_total2").text(accounting.formatMoney(sub_total2));
	}

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

		var id_jenis = $("[name=jenis]").val();
		
		$.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)';

							}
						}

						var nomor_produksi = "[ "+result.data[i].masukNoAuto+result.data[i].masukNoAuto_ext+" ]";
						$("[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_produksi+" "+$nomor+' - '+result.data[i].nama_proses+'</option>');
					}
				}else{
					$("[name=proses]").html('');
				}
			},
			complete : function(r){
				var result = r.responseJSON;
				if(result.status==1){
					$("[name=proses]").val(c_proses).trigger("change");
				}else{
					$("[name=proses]").val("").trigger("change");
				}
			}
		})
		return r;
	}

	function get_ket(){
		$.ajax({
			url: "{{ url('upahproduksi/get_ket') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_keterangan]").html('');
					$("[name=popup_keterangan]").append('<option value="" kode="">--- Pilih Keterangan ---</option>');
					for(i in result.data){
						$("[name=popup_keterangan]").append('<option value="'+result.data[i].id_upahket+'" nama="'+result.data[i].nama_upahket+'">'+result.data[i].nama_upahket+'</option>');
					}
				}
			}
		})
	}

	function get_perkiraan(){
		$.ajax({
			url: "{{ url('upahproduksi/get_perkiraan') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_perkiraan]").html('');
					$("[name=popup_perkiraan]").append('<option value="">--- Pilih Kategori ---</option>');
					for(i in result.data){
						$("[name=popup_perkiraan]").append('<option value="'+result.data[i].id+'" kode="'+result.data[i].kode_perkiraan+'" nama="'+result.data[i].nama_perkiraan+'">'+result.data[i].kode_perkiraan+' - '+result.data[i].nama_perkiraan+'</option>');
					}
					
				}
			}
		})
	}
	
	function get_satuan(){
		$.ajax({
			url: "{{ url('upahproduksi/get_satuan') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_satuan_waktu]").html('');
					$("[name=popup_satuan_waktu]").append('<option value="" kode="">--- Pilih Waktu ---</option>');
					for(i in result.data){
						$("[name=popup_satuan_waktu]").append('<option value="'+result.data[i].id_satuan+'" nama="'+result.data[i].nama_satuan+'">'+result.data[i].nama_satuan+'</option>');
					}
				}
			}
		})
	}

	var id_trans=<?php Print($data['id_trans']); ?>;
	function get_by_id(){
		$.ajax({
			url:"{{ url('upahproduksi/getNoWorker') }}"+"/"+id_trans+"/1",
			type:'get',
			dataType:'json',
			success:function(result){
			if(result[0]['stat_pekerja'] == "Pegawai"){
					$("[name=pegawai]").val(result[0]['id_pegawai']).trigger('change');
					document.getElementById('rekanan').disabled=true;
				}else if(result[0]['stat_pekerja'] == "Rekanan"){
					$("[name=rekanan]").val(result[0]['id_rekanan']).trigger('change');
					document.getElementById('pegawai').disabled=true;
				}			
				
			},
			complete : function(r){
				var result = r.responseJSON;
				get_proses().done(function(){
					$("#proses").val(result[0]['idPros']).trigger('change');
	                $("#lokasi_pro").val(result[0]['id_lokasi']).trigger('change');
				});
			}
		})
	}
	function get_by_id_barang_masuk(){
		// console.log("get_by_id_barang_masuk()");
		$.ajax({
			url:"{{ url('upahproduksi/getNoWorker') }}"+"/"+id_trans+"/2",
			type:'get',
			dataType:'json',
			success:function(result){
				// console.log(result);
        		 if(result[0]['stat_pekerja'] == "Pegawai"){
					$("[name=pegawai]").val(result[0]['id_pegawai']).trigger('change');
					document.getElementById('rekanan').disabled=true;
				}else if(result[0]['stat_pekerja'] == "Rekanan"){
					$("[name=rekanan]").val(result[0]['id_rekanan']).trigger('change');
					document.getElementById('pegawai').disabled=true;
				}
			},
			complete : function(r){
				var result = r.responseJSON;
				// get_proses().done(function(){
				// 	$("#proses").val(result[0]['idPros']).trigger('change');
	   //              $("#lokasi_pro").val(result[0]['id_lokasi']).trigger('change');
				// });
			}
		})
	}

	var total_lain2=0;
	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_transaksi= $("#jenis_transaksi").val();
		var lbl_jenis_transaksi = jenis_transaksi == "KASBON" ? "BAYAR KAS BON" : "UPAH LAIN";
		var jenis= $("#jenis_lain_lain").val();
		var upah=$("#upah_lain_lain").val();
		var lblupah = 0;
		if (jenis_transaksi == "KASBON") {
			lblupah = upah*-1;
		} else {
			lblupah = upah;
		}
		if(jenis != '' && upah !=''){
			if(id_data==''){
				table_lain2.row.add(['<div><center>'+no+'</center></div>',
					'<div id="list_jenis_transaksi'+tb_no_ll+'">'+lbl_jenis_transaksi+'</div>',
					'<div id="list_jenis'+tb_no_ll+'">'+jenis+'</div>',
					'<div id="list_upah'+tb_no_ll+'">'+accounting.formatMoney(lblupah)+'</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_transaksi'+tb_no_ll+'" name="tabel_jenis_transaksi[]" value="'+jenis_transaksi+'">'+
					'<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');
		
	})


	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id_table]").val();
		var id_ket = $("[name=popup_keterangan]").val();
		var keterangan = $("[name=popup_keterangan] :selected").attr('nama');
		// var id_perkiraan = $("[name=popup_perkiraan]").val();
		// var kode_perkiraan = $("[name=popup_perkiraan] :selected").attr('kode');
		// var nama_perkiraan = $("[name=popup_perkiraan] :selected").attr('nama');
		var satuan = $("[name=popup_satuan]").val();
		var satuan_waktu = $("[name=popup_satuan_waktu]").val();
		var nama_satuanwaktu = $("[name=popup_satuan_waktu] :selected").attr('nama');
		var upah = $("[name=popup_upah]").val();
		var total_upah = parseFloat($("[name=popup_upah]").val())*($("[name=popup_satuan]").val());
		
		if(keterangan != '' && satuan != '' && satuan_waktu != '' && upah != ''){
			if(id_data == ''){
				table_barang.row.add(['<div><center></center></div>',
					'<div id="list_ket'+tb_no+'">'+keterangan+'</div>',
					// '<div id="list_perkiraan'+tb_no+'">'+kode_perkiraan+' - '+nama_perkiraan+'</div>',
					'<div id="list_satuan'+tb_no+'">'+satuan+' '+nama_satuanwaktu+'</div>',
					'<div id="list_upah'+tb_no+'">'+accounting.formatMoney(upah)+'</div>',
					'<div id="list_total'+tb_no+'">'+accounting.formatMoney(total_upah)+'</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_ket'+tb_no+'" name="tabel_ket[]" value="'+id_ket+'">'+
					// '<input type="hidden" id="tabel_perkiraan'+tb_no+'" name="tabel_perkiraan[]" value="'+id_perkiraan+'">'+
					'<input type="hidden" id="tabel_satuan'+tb_no+'" name="tabel_satuan[]" value="'+satuan+'">'+
					'<input type="hidden" id="tabel_satuanwaktu'+tb_no+'" name="tabel_satuanwaktu[]" value="'+satuan_waktu+'">'+
					'<input type="hidden" id="tabel_nama_satuanwaktu'+tb_no+'" name="tabel_nama_satuanwaktu[]" value="'+nama_satuanwaktu+'">'+
                    '<input type="hidden" id="tabel_upah'+tb_no+'" name="tabel_upah[]" value="'+upah+'">'+
                    '<input type="hidden" id="tabel_total'+tb_no+'" name="tabel_total[]" value="'+total_upah+'">'
					]).draw(false);
			}else{
				$("#list_ket"+id_data).text(keterangan);
				// $("#list_perkiraan"+id_data).text(kode_perkiraan+' - '+nama_perkiraan);
				$("#list_satuan"+id_data).text(satuan+' '+nama_satuanwaktu);
				$("#list_upah"+id_data).text(accounting.formatMoney(upah));
				$("#list_total"+id_data).text(accounting.formatMoney(total_upah));
				
				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_ket"+id_data).val(id_ket);
				// $("#tabel_perkiraan"+id_data).val(id_perkiraan);
				$("#tabel_satuan"+id_data).val(satuan);	
				$("#tabel_satuanwaktu"+id_data).val(satuan_waktu);
				$("#tabel_nama_satuanwaktu"+id_data).val(nama_satuanwaktu);				
				$("#tabel_upah"+id_data).val(upah);
				$("#tabel_total"+id_data).val(total_upah);
			}
		
		$("#exampleModal").modal('hide');
		tb_no++;
		}
	})

	function edit_table(id){
		var keterangan = $("#tabel_ket"+id).val();
		// var perkiraan = $("#tabel_perkiraan"+id).val();
		var satuan = $("#tabel_satuan"+id).val();
		var satuan_waktu = $("#tabel_satuanwaktu"+id).val();
		var upah = $("#tabel_upah"+id).val();
		
		$("[name=popup_id_table]").val(id);
		$("[name=popup_keterangan]").val(keterangan).trigger('change');
		// $("[name=popup_perkiraan]").val(perkiraan).trigger('change');
		$("[name=popup_satuan]").val(satuan);
		$("[name=popup_satuan_waktu]").val(satuan_waktu).trigger('change');
		$("[name=popup_upah]").val(upah);
		$("#help_popup_upah").text(accounting.formatMoney(upah));

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

	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()).trigger("keyup");

		$("#exampleModal2").modal('show');

	}

	function get_edit(id){
		$.ajax({
			url: "{{ url('upahproduksi/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){ // tabel-opsi-upah-harian
						var total_upah = parseFloat(respon[i].satuan)*parseFloat(respon[i].upah);
						table_barang.row.add(['<div><center></center></div>',
							'<div id="list_ket'+respon[i].id_upahproduksi_barang+'">'+respon[i].keterangan+'</div>',
							// '<div id="list_ket'+respon[i].id_upahproduksi_barang+'">'+respon[i].perkiraan+'</div>',
							'<div id="list_satuan'+respon[i].id_upahproduksi_barang+'">'+respon[i].satuan+ ' /'+respon[i].nama_satuanwaktu+'</div>',
							'<div id="list_upah'+respon[i].id_upahproduksi_barang+'">'+accounting.formatMoney(respon[i].upah)+'</div>',
							'<div id="list_total'+respon[i].id_upahproduksi_barang+'">'+accounting.formatMoney(total_upah)+'</div>',
							'<div><button type="button" class="btn bg-warning-400" onclick="edit_table('+respon[i].id_upahproduksi_barang+')"><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" name="tabel_id[]" id="tabel_id'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].id_upahproduksi_barang+'">'+
							'<input type="hidden" name="tabel_ket[]" id="tabel_ket'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].id_keterangan+'">'+
							// '<input type="hidden" name="tabel_perkiraan[]" id="tabel_perkiraan'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].id_perkiraan+'">'+
							'<input type="hidden" name="tabel_satuan[]" id="tabel_satuan'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].satuan+'">'+
							'<input type="hidden" name="tabel_satuanwaktu[]" id="tabel_satuanwaktu'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].satuan_waktu+'">'+
							'<input type="hidden" name="tabel_nama_satuanwaktu[]" id="tabel_nama_satuanwaktu'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].nama_satuan_waktu+'">'+
							'<input type="hidden" name="tabel_upah[]" id="tabel_upah'+respon[i].id_upahproduksi_barang+'" value="'+respon[i].upah+'">'+
							'<input type="hidden" name="tabel_total[]" id="tabel_total'+respon[i].id_upahproduksi_barang+'" value="'+total_upah+'">'
							]).draw(false);
					}

				}
				
			}
		})

	}

	function hapus(d){
	table_barang.row(d.parents('tr')).remove().draw();	
	}
	function hapusll(d){
		table_lain2.row(d.parents('tr')).remove().draw();
		//alert(tb_no_ll);
		set_sub_total_lain2();
	}

	
</script>
@endsection