@extends('layout')


@section('content')

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="#"><i class="icon-home2 position-left"></i> Master</a></li>
			<li class="">Upah</li>
			<li class="active">Karyawan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Daftar 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" id="btn_tambah"><i class=""></i>Tambah</button>
	    </div>

	    <table class="table table-stripped table_upah" id="">
	    	<thead>
				<tr>
					<th class="text-center">No</th>
					<th class="text-center">Nama</th>
					<th class="text-center">Bagian</th>
					<th class="text-center">Lokasi</th>
					<th class="text-center">Tanggal Berlaku</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" 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_kb">
	      			<input type="hidden" name="popup_idupah">
	      			<input type="hidden" name="popup_list">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Tanggal Berlaku</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_tanggal" class="form-control datepicker">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Karyawan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_karyawan">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Lokasi</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_lokasi">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Upah (Hari)</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_harian" class="form-control number-only text-right">
	      					<p class="help-block text-right" id="popup_help_hari"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Upah Lembur (Jam)</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_lembur" class="form-control number-only text-right">
	      					<p class="help-block text-right" id="popup_help_jam"></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 tb;

	$(document).ready(function(){
		tb = $(".table_upah").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('upah_karyawan/get_upahkaryawan') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'nama_pegawai', name: 'nama_pegawai'},
	            {data: 'nama_departement', name: 'nama_departement'},
	            {data: 'nama_lokasi', name: 'nama_lokasi'},
	            {data: 'tanggal', name: 'tanggal', className:'text-center'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});

		get_karyawan();
		get_lokasi();
	})

	$("#btn_tambah").click(function(){
		$("[name=popup_idupah]").val('');
		$("[name=popup_list]").val('');
		$("[name=popup_tanggal]").val('');
		$("[name=popup_karyawan]").val(null).trigger('change');
		$("[name=popup_lokasi]").val(null).trigger('change');
		$("[name=popup_harian]").val('');
		$("[name=popup_lembur]").val('');
		$("#popup_help_hari").text('');
		$("#popup_help_jam").text('');

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



	function get_karyawan(){
		$.ajax({
			url: "{{ url('upah_karyawan/get_karyawan')}}  ",
			type: 'get',
			dataType: 'json',
			success: function(respon){
				if(respon.length > 0){
					$("[name=popup_karyawan]").html('');
					for(i in respon){
						$("[name=popup_karyawan]").append('<option value="'+respon[i].id_pegawai+'">'+respon[i].nama_pegawai+' - '+respon[i].nama_departement+'</option>');
					}
				}				
			}
		})
	}

	function get_lokasi(){
		$.ajax({
			url: "{{ url('upah_karyawan/get_lokasi') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_lokasi]").html('');
					$("[name=popup_lokasi]").append('<option value="" kode="">--- Pilih Lokasi ---</option>');
					for(i in result.data){
						$("[name=popup_lokasi]").append('<option value="'+result.data[i].id_lokasi+'" nama="'+result.data[i].nama_lokasi+'">'+result.data[i].nama_lokasi+'</option>');
					}
				}
			}
		})
	}

	$("#btn_popup_simpan").click(function(){
		var id_upah = $("[name=popup_idupah]").val();
		var tanggal = $("[name=popup_tanggal]").val();
		var karyawan = $("[name=popup_karyawan]").val();
		var harian = $("[name=popup_harian]").val();
		var lembur = $("[name=popup_lembur]").val();
		var lokasi = $("[name=popup_lokasi]").val();

		if(tanggal != '' && karyawan != '' && harian != '' && lembur != '' && lokasi != ''){
			$.ajax({
				url: "{{ url('upah_karyawan/simpan_upahkaryawan')}} ",
				type: 'post',
				data: {id_upah : id_upah, tanggal : tanggal, karyawan : karyawan, harian : harian, lembur : lembur, lokasi : lokasi},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					tb.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})


	function edit(id){
		var id_upah = $("#table_id"+id).val();
		var id_pegawai = $("#table_idpegawai"+id).val();
		var tanggal = $("#table_tanggal"+id).val();
		var harian = $("#table_upahharian"+id).val();
		var lembur = $("#table_upahlembur"+id).val();
		var lokasi = $("#table_lokasi"+id).val();


		$("[name=popup_idupah]").val(id_upah);
		$("[name=popup_karyawan]").val(id_pegawai);
		$("[name=popup_tanggal]").val(tanggal);
		$("[name=popup_harian]").val(harian);
		$("[name=popup_lembur]").val(lembur);
		$("[name=popup_lokasi]").val(lokasi).trigger('change');
		rp();
		
		$("#exampleModal").modal('show');

	}

	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('upah_karyawan/hapus_upahkaryawan')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						tb.ajax.reload();
					}
				})
		 	}
		})
	}


	$("[name=popup_harian]").keyup(function(){
		var rp = $("[name=popup_harian]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#popup_help_hari").text(curr(rp));
		
	})

	$("[name=popup_lembur]").keyup(function(){
		var rp = $("[name=popup_lembur]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#popup_help_jam").text(curr(rp));
		
	})

	function rp(){
		var rp = $("[name=popup_harian]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#popup_help_hari").text(curr(rp));
		
		var rp = $("[name=popup_lembur]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#popup_help_jam").text(curr(rp));
	}
	
</script>

@endsection