@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">Pegawai</li>
		</ul>
	</div>
</div>

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

	    <table class="table table-stripped table_barang">
	    	<thead>
				<tr>
					<th class="text-center">No</th>
					<th class="text-center">Departement</th>
					<th class="text-center">ID Pegawai</th>
					<th class="text-center">Nama Pegawai</th>
					<th class="text-center">Lokasi</th>
					<th class="text-center">Status</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_kb">
	      			<input type="hidden" name="popup_id">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Departement</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_departement" required>
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group" id="txt_nomor_pegawai" style="display: none">
	      				<label class="control-label col-sm-4">ID Pegawai</label>
	      				<div class="col-sm-8">
	      					<input type="text" readonly name="popup_nomor_pegawai" class="form-control">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nama Pegawai</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_nama" class="form-control" required>
	      				</div>
	      			</div>
	      			{{-- <div class="form-group">
	      				<label class="control-label col-sm-4">Tahun Awal</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_tahun_awal" class="form-control datepicker">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Tahun Akhir</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_tahun_akhir" class="form-control datepicker">
	      				</div>
	      			</div> --}}
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Status</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_status" required>
	      						<option value="1">Aktif</option>
	      						<option value="0">Tidak Aktif</option>
	      					</select>
	      				</div>
	      			</div>
	      			<hr>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Akun Piutang</label>
	      				<div class="col-sm-8">
	      					{{ Form::select("popup_akun_piutang", $akunPiutang, [null], ['class'=>'form-control select-search','required'=>'true']) }}
	      				</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_barang").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('pegawai/get_pegawai') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'nama_departement', name: 'md.nama'},
	            {data: 'nomor_pegawai', name: 'mp.nomor_pegawai'},
	            {data: 'nama_pegawai', name: 'mp.nama_pegawai'},
	            {data: 'nama_lokasi', name: 'mlp.nama_lokasi'},
	            {data: 'status', name: 'status', orderable: false, searchable: false},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});

		
		get_departement();

		
	})

	$("#btn_tambah_sub").click(function(){
		$("[name=popup_id]").val('');
		// $("[name=popup_nomor_pegawai]").val('-');
		$("[name=popup_nama]").val('');
		$("[name=popup_departement]").val(null).trigger('change');
		$("[name=popup_akun_piutang]").val(null).trigger('change');
		$("[name=popup_tahun_awal]").val('');
		$("[name=popup_tahun_akhir]").val('');
		$("[name=popup_status]").val('0').trigger('change');

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

	function get_departement(){
		$.ajax({
			url: "{{ url('pegawai/get_departement') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_departement]").html('');
					$("[name=popup_departement]").append('<option value="" kode="">--- Pilih Departement ---</option>');
					for(i in result.data){
						$("[name=popup_departement]").append('<option value="'+result.data[i].id_departement+'" nama="'+result.data[i].nama+'">'+result.data[i].nama+'</option>');
					}
				}
			}
		})
	}

	



	function edit_pegawai(id){
		var id_pegawai = $("#table_id"+id).val();
		var departement = $("#table_departement"+id).val();
		var nomor_pegawai = $("#table_nomor_pegawai"+id).val();
		var nama_pegawai = $("#table_nama_pegawai"+id).val();
		var tahun_awal = $("#table_tahun_awal"+id).val();
		var tahun_akhir = $("#table_tahun_akhir"+id).val();
		var akun_piutang = $("#table_akun_piutang"+id).val();
		var status = $("#table_status"+id).val();

		$("#txt_nomor_pegawai").css("display", "block");

		$("[name=popup_id]").val(id_pegawai);
		$("[name=popup_nomor_pegawai]").val(nomor_pegawai);
		$("[name=popup_nama]").val(nama_pegawai);
		$("[name=popup_departement]").val(departement).trigger('change');
		$("[name=popup_akun_piutang]").val(akun_piutang).trigger('change');
		$("[name=popup_tahun_awal]").val(tahun_awal);
		$("[name=popup_tahun_akhir]").val(tahun_akhir);
		$("[name=popup_status]").val(status).trigger('change');
		$("#exampleModal").modal('show');

	}

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

	$("#btn_popup_simpan").click(function(){
		var id_pegawai = $("[name=popup_id]").val();
		var departement = $("[name=popup_departement]").val();
		// var nomor_pegawai = $("[name=popup_nomor_pegawai]").val();
		var nama = $("[name=popup_nama]").val();
		var tahun_awal = $("[name=popup_tahun_awal]").val();
		var tahun_akhir = $("[name=popup_tahun_akhir]").val();
		var akun_piutang = $("[name=popup_akun_piutang]").val();
		var status = $("[name=popup_status]").val();

		if(departement != "" && nama != '' && akun_piutang != null){
			$.ajax({
				url: "{{ url('pegawai/simpan_pegawai')}} ",
				type: 'post',
				data: {id : id_pegawai, id_departement: departement, nama : nama, tahun_awal : tahun_awal, tahun_akhir : tahun_akhir, id_akun_piutang:akun_piutang, status : status},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					if (respon.status == "Success") {
						tb.ajax.reload();
						$("#exampleModal").modal('hide');
					} else {
						swal({
							title: 'Data pegawai gagal disimpan!',
							type: 'warning',
							confirmButtonColor: '#3085d6',
							confirmButtonText: 'OK',
						}).then((result) => {
						 	
						})
					}
				}
			})
		} else {
			swal({
				title: 'Lengkapi data pegawai',
				type: 'warning',
				confirmButtonColor: '#3085d6',
				confirmButtonText: 'OK',
			}).then((result) => {
			 	
			})
		}
	})
	
</script>

@endsection