@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> Keuangan</a></li>
			<li class="active">Kasbon Pegawai/Rekanan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Daftar Kasbon Pegawai/Rekanan</h5>
			
			<div class="heading-elements">
		    	<div class="btn-group">
	            	<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
	            		<i class="fa fa-plus"></i> Tambah <span class="caret"></span>
	            	</button>
	            	<ul class="dropdown-menu dropdown-menu-right">
						<li><a href="{{ url('piutang_pegawai/create') }}">Kasbon Pegawai</a></li>
						<li><a href="{{ url('piutang_rekanan/create') }}">Kasbon Rekanan</a></li>
					</ul>
				</div>
				&nbsp;
				<a href="{{ url('piutang_pegawai/laporan-piutang-karyawan') }}" class="btn btn-success"><i class="fa fa-list"></i> Laporan</a>
	       	</div>
	       	
	    </div>
	    <div class="panel-body" style="padding-bottom: 0px;">

		    <div class="tabbable">
				<ul class="nav nav-tabs nav-tabs-highlight">
					<li class="active"><a href="#basic-tab1" data-toggle="tab">Pegawai</a></li>
					<li><a href="#basic-tab2" data-toggle="tab">Rekanan</a></li>
				</ul>
			</div>

	    </div>

		<div class="tab-content">
			<div class="tab-pane active" id="basic-tab1">
				<table class="table" id="tabel-kasbon-pegawai">
					<thead>
						<tr>
							<th>No</th>
							<th>Tanggal</th>
							<th>No Transaksi</th>
							<th>Pegawai</th>
							<th>Tagihan</th>
							<th>Pembayaran</th>
							<th>Status</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
					</tbody>
				</table>

			</div>

			<div class="tab-pane" id="basic-tab2">
				<table class="table" id="tabel-kasbon-rekanan">
					<thead>
						<tr>
							<th>No</th>
							<th>Tanggal</th>
							<th>No Transaksi</th>
							<th>Rekanan</th>
							<th>Tagihan</th>
							<th>Pembayaran</th>
							<th>Status</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
					</tbody>
				</table>

			</div>
		</div>

	    <div class="panel-footer">
	    	<div class="col-sm-12 col-md-12">

	    	</div>
	    </div>
	    
	</div>
</div>

<script type="text/javascript">
	var tabelKasbonPegawai;
	var tabelKasbonRekanan;

	$(document).ready(function () {
		// $("#tabel-piutang-pegawai, #tabel-piutang-rekanan").DataTable();
		loadDataKasbonPegawai();
		loadDataKasbonRekanan();
	});

	function loadDataKasbonPegawai() {
		tabelKasbonPegawai = $("#tabel-kasbon-pegawai").DataTable({
			serverSide : true,
			processing : true,
			ajax : "{{ url('piutang_pegawai/data_kasbon_pegawai') }}",
			columns : [
			  {data:'id', name:'id', orderable:false, searchable:false, render : function(data, type, full, meta){
			  	return meta.row+1;
			  }},
			  {data:'tanggal_transaksi', name:'tanggal_transaksi'},
			  {data:'nomor_transaksi', name:'nomor_transaksi'},
			  {data:'pegawai', name:'pegawai'},
			  {data:'nominal', name:'nominal', render:function(nominal) {
			  	return accounting.formatMoney(nominal);
			  }},
			  {data:'pembayaran', name:'pembayaran', render:function(pembayaran) {
			  	return accounting.formatMoney(pembayaran);
			  }},
			  {data:'status', name:'status', searchable:false, orderable : false},
			  {data:'aksi', name:'aksi', searchable:false, orderable : false},
			],
			createdRow : function( row, data, dataIndex ) {
				$(row).attr('data-idkasbon', data['id']);

				$('td', row).eq(4).addClass("text-right");
				$('td', row).eq(5).addClass("text-right");
			},
		});
	}

	function btnDeleteKasbonPegawai(this_) {
		var id_kasbon = this_.parents("tr").data("idkasbon");
		
		swal({
			title: 'Anda yakin akan menghapus data ini?',
			text : 'Menghapus data akan mempengaruhi transaksi Anda.',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#d33',
			cancelButtonColor: '#3085d6',
			confirmButtonText: 'Hapus',
			cancelButtonText: 'Batal',
		}).then((result) => {
			
		 	if(result.value){
		 		$.ajax({
					url: "{{ url('piutang_pegawai')}}/"+id_kasbon,
					type: 'post',
					data: {"_method":"DELETE"},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						if (respon.status == 1 || respon.status == "1") {
							tabelKasbonPegawai.ajax.reload();
							Swal.fire(
							  'Berhasil!',
							  'Data berhasil dihapus.',
							  'success'
							)
						} else {
							Swal.fire(
							  'Gagal!',
							  'Data gagal dihapus.',
							  'error'
							)
						}
					}
				})
		 	}
		})
	}

	function btnCetakKasbonPegawai(this_) {
		var id_kasbon = this_.parents("tr").data("idkasbon");
		window.open("{{ url('piutang_pegawai/kasbon_cetak') }}/"+id_kasbon);
	}

	function loadDataKasbonRekanan() {
		tabelKasbonRekanan = $("#tabel-kasbon-rekanan").DataTable({
			serverSide : true,
			processing : true,
			ajax : "{{ url('piutang_rekanan/data_kasbon_rekanan') }}",
			columns : [
			  {data:'id', name:'id', orderable:false, searchable:false, render : function(data, type, full, meta){
			  	return meta.row+1;
			  }},
			  {data:'tanggal_transaksi', name:'tanggal_transaksi'},
			  {data:'nomor_transaksi', name:'nomor_transaksi'},
			  {data:'nama_rekanan', name:'nama_rekanan', render:function(nama_rekanan, display, data){
			  	return data.nama_rekanan+" ( "+data.keterangan_rekanan+" )";
			  }},
			  {data:'nominal', name:'nominal', render:function(nominal) {
			  	return accounting.formatMoney(nominal);
			  }},
			  {data:'pembayaran', name:'pembayaran', render:function(pembayaran) {
			  	return accounting.formatMoney(pembayaran);
			  }},
			  {data:'status', name:'status', searchable:false, orderable : false},
			  {data:'aksi', name:'aksi', searchable:false, orderable : false},
			],
			createdRow : function( row, data, dataIndex ) {
				$(row).attr('data-idkasbon', data['id']);

				$('td', row).eq(4).addClass("text-right");
				$('td', row).eq(5).addClass("text-right");
			},
		});
	}

	function btnDeleteKasbonRekanan(this_) {
		var id_kasbon = this_.parents("tr").data("idkasbon");
		
		swal({
			title: 'Anda yakin akan menghapus data ini?',
			text : 'Menghapus data akan mempengaruhi transaksi Anda.',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#d33',
			cancelButtonColor: '#3085d6',
			confirmButtonText: 'Hapus',
			cancelButtonText: 'Batal',
		}).then((result) => {
			
		 	if(result.value){
		 		$.ajax({
					url: "{{ url('piutang_rekanan')}}/"+id_kasbon,
					type: 'post',
					data: {"_method":"DELETE"},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						if (respon.status == 1 || respon.status == "1") {
							tabelKasbonRekanan.ajax.reload();
							Swal.fire(
							  'Berhasil!',
							  'Data berhasil dihapus.',
							  'success'
							)
						} else {
							Swal.fire(
							  'Gagal!',
							  'Data gagal dihapus.',
							  'error'
							)
						}
					}
				})
		 	}
		})
	}

	function btnCetakKasbonRekanan(this_) {
		var id_kasbon = this_.parents("tr").data("idkasbon");
		window.open("{{ url('piutang_rekanan/kasbon_cetak') }}/"+id_kasbon);
	}
</script>

@endsection