@extends('layout')


@section('content')

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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Input Laporan Hasil</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<form class="form-horizontal" method="post" action="{{ url('barangmasuk/simpan') }}" autocomplete="off">
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_barangmasuk" value="{{ $data['data']['id_barangmasuk'] }}">
	    	<input type="hidden" name="id_barangkeluar" value="{{ $data['data']['id_barangkeluar'] }}">
	    	<input type="hidden" name="barangkeluar" value="{{ $data['data2']->id_barangkeluar }}">
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Selesai</label>
        			<div class="col-sm-8">
        				<input class="form-control datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ $data['data']['tanggal'] }}" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Pengambilan</label>
        			<div class="col-sm-8">
        				<input class="form-control" type="text" name="tanggal_pengambilan" placeholder="" value="{{ date('d-m-Y', strtotime($data['data2']->tanggal)) }}" readonly/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Perkiraan Selesai</label>
        			<div class="col-sm-8">
        				<input class="form-control" type="text" name="tanggal_perkiraan" placeholder="" value="{{ date('d-m-Y', strtotime($data['data2']->tanggal_perkiraan)) }}" readonly/>
        			</div>
        		</div>
        	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Rekanan</label>
        			<div class="col-sm-8">
        				@php
        				if($data['data2']->id_rekanan == '0'){
        				$rekanan = '0';
        			}else{
        				$rekanan = $data['data2']->nama_rekanan;
        		}
        				@endphp
        				<input class="form-control" type="text" name="rekanan" placeholder="" value="{{ $rekanan }}" readonly/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Pegawai</label>
        			<div class="col-sm-8">
        				@php 
        				if($data['data2']->id_pegawai == '0'){
        				$pegawai = '0';
        			}else{
        				$pegawai = $data['data2']->nama_pegawai;
        		}
        				@endphp
        				<input class="form-control" type="text" name="pegawai" placeholder="" value="{{ $data['data2']->nama_pegawai }}" readonly/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Proses Produksi</label>
        			<div class="col-sm-8">
        				<input class="form-control" type="text" name="proses" placeholder="" value="{{ $data['data2']->nama_proses }}" readonly/>
        			</div>
        		</div>
        	</div>
        	
        	<div class="col-sm-12 form-group">
	        	<h5 class="panel-title">Barang Diambil</h5>
	        	<div class="col-ms-12">
		        	<table class="table table-stripped dataTable">
				    	<thead>
							<tr>
								<th>No</th>
								<th>Kode Barang</th>
								<th>Nama Barang</th>
								<th>Gudang</th>
								<th>Jumlah</th>
								
							</tr>
						</thead>
						<tbody>
							@php $no=1; @endphp
							@foreach($data['barang'] as $d) 
							<tr>
								<td>{{$no++}}</td>
								<td>{{ $d->kode_barang }}</td>
								<td>{{ $d->nama_barang }}</td>
								<td>{{ $d->nama_gudang }}</td>
								<td>{{ $d->jumlah }}</td>
							</tr>
							@endforeach
						</tbody>
				    </table>
	        	</div>
        	</div>
            
            <div class="col-sm-12 form-group">
	        	<h5 class="panel-title">Barang Disetor</h5>
	        	<br><a href="#" class="btn btn-success" style="margin-bottom: 1%;" id="btn_tambah_item">Tambah Barang</a>
		        	<table class="table table-stripped" id="table_hasil">
				    	<thead>
							<tr>
								<th>No</th>
								<th>Kode Barang</th>
								<th>Nama Barang</th>
								<th>Gudang</th>
								<th>Upah</th>
								<th>Jumlah barang</th>
								<th>Kualitas</th>
								<th>Total Upah</th>
								<th></th>
							</tr>
						</thead>
						<tbody id="body_setor">
						</tbody>
			    	</table>
        	</div>
            <div class="form-group pull-right">
             	<button class="btn btn-primary" type="submit">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">Barang</label>
	      				<div class="col-sm-8">
	      			 	<select class="form-control select-search" name="popup_barang">
	      			 		
	      			 	</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Gudang</label>
	      				<div class="col-sm-8">
	      			 	<select class="form-control select-search" name="popup_gudang">
	      			 		
	      			 	</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Upah</label>
	      				<div class="col-sm-8">
	      			 	<input type="text" class="form-control" name="popup_upah" readonly="">
	      			 	<input type="hidden" name="popup_idupah" id="popup_idupah">
	      			 	<p class="help-block pull-right" id="popup_upah_text"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah barang</label>
	      				<div class="col-sm-8">
	      			 	<input type="number" name="popup_jumlah" class="form-control">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Kualitas</label>
	      				<div class="col-sm-8">
	      					<select name="popup_kualitas" class="form-control select-search">

	      					</select>
	      				</div>
	      			</div>	      			
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>
<script type="text/javascript">

	$("#btn_tambah_item").click(function(){
		$("[name=popup_id_table]").val('');
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_gudang]").val(null).trigger('change');
		$("[name=popup_jumlah]").val('');
		$("[name=popup_upah]").val('');
		$("[name=popup_kualitas]").val(null).trigger('change');

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

	var table_barang;
	var table_hasil;
	var tb_no = parseInt(10000);
	$(document).ready(function(){
		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();

        table_hasil = $("#table_hasil").DataTable();
		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();

		get_barang();
        get_gudang();
        get_kualitas()
	})

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

	function get_barang(){
		$.ajax({
			url: '{{ url('barangmasuk/get_barang') }}',
			success: function(respon){
				$("[name=popup_barang]").html('');
				$("[name=popup_barang]").append('<option value="">--- Pilih Barang ---</option>')
				if(respon.length > 0){
					for(i in respon){
						$("[name=popup_barang]").append('<option value="'+respon[i].id_barang+'" nama="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].satuan+'">'+respon[i].kategori+' - '+respon[i].nama_sub+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+'</option>');
					}
				}
			}
		})
	}

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

	$("[name=popup_barang]").change(function(){
		var id_barang = $("[name=popup_barang]").val();
		
		$.ajax({
			url: "{{ url('barangmasuk/get_upah')}} ",
			type: 'post',
			data: {id : id_barang},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					$("#div_konversi").removeClass('hide');
					$("[name=popup_upah]").val(respon[0].nominal);
					$("#popup_idupah").val(respon[0].id_upah);
					$("#popup_upah_text").text(curr(respon[0].nominal));
				}else{
					$("#div_konversi").addClass('hide');
					$("[name=popup_upah]").val('0');
					$("#popup_idupah").val('');			
					$("#popup_upah_text").text(curr(0));	
				}
			}
		})
	})

	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id_table]").val();
		var id_barang = $("[name=popup_barang]").val();
		var id_gudang = $("[name=popup_gudang]").val();
		var id_upah = $("[name=popup_idupah]").val();
		var kode_barang = $("[name=popup_barang] :selected").attr('kode');
		var nama_barang = $("[name=popup_barang] :selected").attr('nama');
		var nama_gudang = $("[name=popup_gudang] :selected").attr('nama');
		var upah = $("[name=popup_upah]").val();
		var jumlah = $("[name=popup_jumlah]").val();
		var id_kualitas = $("[name=popup_kualitas]").val();
		var nama_kualitas = $('[name=popup_kualitas] :selected').attr('nama');
		var total = parseInt(jumlah*upah);
		if(id_barang != '' && jumlah != ''){
			if(id_data == ''){
				table_hasil.row.add(['<div><center></center></div>',
					'<div id="list_kode'+tb_no+'">'+kode_barang+'</div>',
					'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
					'<div id="list_gudang'+tb_no+'">'+nama_gudang+'</div>',
					'<div id="list_upah'+tb_no+'">'+upah+'</div>',
					'<div id="list_jumlah'+tb_no+'">'+jumlah+'</div>',
					'<div id="list_kualitas'+tb_no+'">'+nama_kualitas+'</div>',
					'<div id="list_kualitas'+tb_no+'">'+total+'</div>',
					'<div><button type="button" class="btn bg-warning-400" onclick="edit_table('+tb_no+')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button"  onclick="hapus($(this))""><i class="icon-eraser"></i></button></div>'+
					'<input type="hidden" id="tabel_id'+tb_no+'" name="tabel_id[]" value="">'+
					'<input type="hidden" id="tabel_idbarang'+tb_no+'" name="tabel_idbarang[]" value="'+id_barang+'">'+
					'<input type="hidden" id="tabel_idgudang'+tb_no+'" name="tabel_idgudang[]" value="'+id_gudang+'">'+
					'<input type="hidden" id="tabel_idupah'+tb_no+'" name="tabel_idupah[]" value="'+id_upah+'">'+
					'<input type="hidden" id="tabel_upah'+tb_no+'" name="tabel_upah[]" value="'+upah+'">'+
					// '<input type="hidden" id="tabel_kodebarang'+tb_no+'" name="tabel_kodebarang[]" value="'+kode_barang+'">'+
					'<input type="hidden" id="tabel_jumlah'+tb_no+'" name="tabel_jumlah[]" value="'+jumlah+'">'+
					'<input type="hidden" id="tabel_idkualitas'+tb_no+'" name="tabel_idkualitas[]" value="'+id_kualitas+'">'+
					'<input type="hidden" id="tabel_total'+tb_no+'" name="tabel_total[]" value="'+total+'">'
					]).draw(false);
			}else{
				$("#list_kode"+id_data).text(kode_barang);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_gudang"+id_data).text(nama_gudang);
				$("#list_upah"+id_data).text(upah);
				$("#list_jumlah"+id_data).text(jumlah);
				$("#list_total"+id_data).text(total);
				$("#list_kualitas"+id_data).text(nama_kualitas);

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);
				$("#tabel_namabarang"+id_data).val(nama_barang);				
				$("#tabel_upah"+id_data).val(upah);
				$("#tabel_jumlah"+id_data).val(jumlah);
				$("#tabel_idkualitas").val(id_kualitas);
				$("#tabel_total"+id_data).val(total);
			}
		
		$("#exampleModal").modal('hide');
		tb_no++;/*
		get_jum();*/
		}
	})

	function edit_table(id){
		var id_barang = $("#tabel_idbarang"+id).val();
		var id_gudang = $("#tabel_idgudang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var upah = $("#tabel_upah"+id).val();
		var id_kualitas = $("#tabel_idkualitas"+id).val();

		$("[name=popup_id_table]").val(id);
		$("[name=popup_barang]").val(id_barang).trigger('change');
		$("[name=popup_gudang]").val(id_gudang).trigger('change');
		$("[name=popup_jumlah]").val(jumlah);
		$("[name=popup_upah]").val(upah);
		$("[name=popup_kualitas]").val(id_kualitas).trigger('change');

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

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

	
	/*$("#btn_tambah_hasil").click(function(){

		table_hasil.row.add([
			'<div>1</div>',
			'<div><select class="form-control select-search" >'+
									'</select>'+
								'</div>',
								'<div>Barang 1</div>',
								'<div>'+
									'<select class="form-control select-search">'+
										'<option value="">Gudang 1</option>'+
										'<option value="">Gudang 2</option>'+
									'</select>'+
								'</div>',
								'<div><input type="text" name="jumlah_barang" class="form-control"></div>',
								'<div><input type="text" name="kualitas_barang" class="form-control"></div>',
								'<button class="btn bg-danger-800" type="button" onclick=""><i class="icon-eraser"></i></button>']).draw(false);

	})*/

	
	
	/*$("#btn_tambah_hasil").click(function(){

		table_hasil.row.add([
			'<div>1</div>',
			'<div><select class="form-control select-search" >'+
									'</select>'+
								'</div>',
								'<div>Barang 1</div>',
								'<div>'+
									'<select class="form-control select-search">'+
										'<option value="">Gudang 1</option>'+
										'<option value="">Gudang 2</option>'+
									'</select>'+
								'</div>',
								'<div><input type="text" name="jumlah_barang" class="form-control"></div>',
								'<div><input type="text" name="kualitas_barang" class="form-control"></div>',
								'<button class="btn bg-danger-800" type="button" onclick=""><i class="icon-eraser"></i></button>']).draw(false);

	})*/

	/*function get_hasil(id){
		
		$.ajax({
			url: "{{ url('barangmasuk/get_hasil')}} ",
			type: 'post',
			data: {id : id},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					for(i in respon){
						
						table_hasil.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_nama'+respon[i].id+'">'+respon[i].nama_komposisi+'</div>',
							'<div id="list_nama'+respon[i].id+'">'+respon[i].jumlah+'</div>',
							'<div id="list_nama'+respon[i].id+'">'+respon[i].jumlah_konv+'</div>',
							'<div id="list_gudang'+respon[i].id+'">'+respon[i].nama_gudang+'</div>',
							'<div><button class="btn bg-warning-400" type="button" onclick="edit_hasil('+respon[i].id+')"><i class="icon-eraser"></i></button></div>'+
							'<input type="hidden" name="tabel_id[]" class="tabel_id" id="tabel_id'+respon[i].id+'" value="'+respon[i].id_barangmasuk+'">'+
							'<input type="hidden" name="tabel_idbarang[]" class="tabel_id" id="tabel_idbarang'+respon[i].id+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_idgudang[]" class="tabel_id" id="tabel_idgudang'+respon[i].id+'" value="'+respon[i].id_gudang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" class="tabel_id" id="tabel_jumlah'+respon[i].id+'" value="'+respon[i].jumlah+'">'+
							'<input type="hidden" name="tabel_jumlahkonv[]" class="tabel_id" id="tabel_jumlahkonv'+respon[i].id+'" value="'+respon[i].jumlah_konv+'">'+
							'<input type="hidden" id="tabel_idupah'+respon[i].id+'" name="tabel_idupah[]" value="0">'+
							'<input type="hidden" id="tabel_idkualitas'+respon[i].id+'" name="tabel_idkualitas[]" value="0">'+
							'<input type="hidden" id="tabel_total'+respon[i].id+'" name="tabel_total[]" value="0">'
							]).draw(false);


					}

				}
				
			}
		})
	}
	*/
	
</script>
@endsection