@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> Master</a></li>
			<li class="active">Upah</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Upah</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<button class="btn btn-primary" type="button" id="btn_tambah_gudang">Tambah</button>
	    </div>
	    <table class="table table-striped tabel_upah">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Barang</th>
					<th>Proses</th>
					<th>Nominal (Rupiah)</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade" id="exampleModal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog" 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" id="form_gudang">
	      			<input type="hidden" name="id_upah">
	      			<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">Proses</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nominal</label>
	      				<div class="col-sm-8">
	      					<input type="text" class="form-control" name="popup_nominal">
	      					<p class="help-block pull-right" id="help_popup_harga"></p>
	      				</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">
	var tabel_upah;

	$(document).ready(function(){
		tabel_upah = $(".tabel_upah").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('upahpegawai/get_upah') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'nama_barang', name: 'nama_barang'},
	            {data: 'nama_proses', name: 'nama_proses'},
	            {data: 'nominal', name: 'nominal'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});

		
        get_barang();
        get_proses();
        get_nominal();

	})

	$("#btn_tambah_gudang").click(function(){
		$("[name=id_upah]").val('');
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_proses]").val(null).trigger('change');
		$("[name=popup_nominal]").val('');
		$("#help_popup_harga").text('');
		$("#exampleModal").modal('show');
	})

	function get_barang(){
		$.ajax({
			url: "{{ url('upahpegawai/get_barang') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_barang]").html('');
					$("[name=popup_barang]").append('<option value="" kode="">--- Pilih barang ---</option>');
					for(i in result.data){
						$("[name=popup_barang]").append('<option value="'+result.data[i].id+'" nama="'+result.data[i].nama_barang+'">'+result.data[i].nama_kategori+' - '+result.data[i].nama_sub+' - '+result.data[i].kode_barang+' - '+result.data[i].nama_barang+'</option>');
					}
				}
			}
		})
	}

	function get_proses(){
		$.ajax({
			url: "{{ url('upahpegawai/get_proses') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_proses]").html('');
					$("[name=popup_proses]").append('<option value="" kode="">--- Pilih proses ---</option>');
					for(i in result.data){
						$("[name=popup_proses]").append('<option value="'+result.data[i].id_proses+'" nama="'+result.data[i].nama_proses+'">'+result.data[i].nama_proses+'</option>');
					}
				}
			}
		})
	}


	// function get_upah(){
	// 	$.ajax({
	// 		url: "{{ url('upahpegawai/get_upah')}}  ",
	// 		type: 'get',
	// 		dataType: 'json',
	// 		success: function(respon){
	// 			tabel_upah.clear().draw();
	// 			for(i in respon){
	// 				tabel_upah.row.add(['<div><center></center></div>',
	// 					'<div>'+respon[i].nama_barang+'</div>',
	// 					'<div>'+respon[i].nama_proses+'</div>',
	// 					'<div>'+curr(respon[i].nominal)+'</div>',
	// 					'<div>'+respon[i].aksi+'</div>'+
	// 					'<input type="hidden" id="table_idupah'+respon[i].id_upah+'" value="'+respon[i].id_upah+'">'+
	// 					'<input type="hidden" id="table_barang'+respon[i].id_upah+'" value="'+respon[i].id_barang+'">'+
	// 					'<input type="hidden" id="table_proses'+respon[i].id_upah+'" value="'+respon[i].id_proses+'">'+
	// 					'<input type="hidden" id="table_nominal'+respon[i].id_upah+'" value="'+respon[i].nominal+'">'
	// 					]).draw(false);
	// 			}
	// 		}
	// 	})
	// }

	$('[name=popup_nominal]').keyup(function(){
		var rp = curr($('[name=popup_nominal]').val());
		$("#help_popup_harga").text(rp);

	})

	function get_nominal(){
		var rp = curr($('[name=popup_nominal]').val());
		$("#help_popup_harga").text(rp);
	}



	function edit(id){
		$("[name=id_upah]").val($("#table_idupah"+id).val());
		$("[name=popup_barang]").val($("#table_barang"+id).val()).trigger('change');
		$("[name=popup_proses]").val($("#table_proses"+id).val()).trigger('change');
		$("[name=popup_nominal]").val($("#table_nominal"+id).val());
		get_nominal();
		$("#exampleModal").modal('show');
	}

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id_upah]").val();
		var barang = $("[name=popup_barang]").val();
		var proses = $("[name=popup_proses]").val();
		var nominal = $("[name=popup_nominal]").val();

		if(barang != '' && proses != '' && nominal != ''){
			$.ajax({
				url: "{{ url('upahpegawai/simpan') }}",
				type: 'post',
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				data: {id:id, barang:barang, proses:proses, nominal:nominal},
				success: function(respon){
					tabel_upah.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})

	function hapus(id){
		swal({
			title: 'Anda yakin akan menghapus item ini?',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#3085d6',
			cancelButtonColor: '#d33',
			confirmButtonText: 'Ya',
			cancelButtonText: 'Tidak',
		}).then((result) => {
		 	if(result.value){
		 		$.ajax({
					url: "{{ url('upahpegawai/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						tabel_upah.ajax.reload();
					}
				})
		 	}
		})
	}
</script>

@endsection