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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Jenis Transaksi</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_transaksi">Tambah</button>
	    </div>
	    <table class="table table-striped table_transaksi">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Jenis Transaksi</th>
					<th>Perkiraan</th>
					<th>Tipe</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="popup_id">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jenis Transaksi</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jenis" class="form-control">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Perkiraan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_kode">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Tipe</label>
	      				<div class="col-sm-8">
	      					<select class="form-control" name="popup_tipe">
	      						<option value="">-- Pilih Tipe --</option>
	      						<option value="D">Debet</option>
	      						<option value="K">Kredit</option>
	      					</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_transaksi").click(function(){
		$("#exampleModal").modal("show");
	})

	function edit_satuan(){
		$("#exampleModal").modal("show");
	}
</script>
<script type="text/javascript">
	var tb;

	$(document).ready(function(){
		tb = $(".table_transaksi").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('transaksi/tampil') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'nama_transaksi', name: 'nama_transaksi'},
	            {data: 'tipe', name: 'tipe'},
	            {data: 'kategori', name: 'kategori'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});

		
		get_perkiraan();
	})

	$("#btn_tambah_transaksi").click(function(){
		$("[name=popup_id]").val('');
		$("[name=popup_jenis]").val('');
		$("[name=popup_kode]").val('');
		$("[name=popup_tipe]").val('');
		$("[name=popup_kode]").select2();
		
		$("#exampleModal").modal("show");
	})

	

	function get_transaksi(){
		$.ajax({
			url: "{{ url('transaksi/tampil')}}  ",
			type: 'get',
			dataType: 'json',
			success: function(respon){
				tb.clear().draw();
				for(i in respon){
					tb.row.add(['<div></div>',
						'<div>'+respon[i].jenis+'</div>',
						'<div>'+respon[i].kode+'</div>',
						'<div><center>'+respon[i].tipe+'</center></div>',
						'<div><center>'+respon[i].aksi+'</center></div>'+
						'<input type="hidden" id="table_id'+respon[i].id_transaksi+'" value="'+respon[i].id_transaksi+'">'+
						'<input type="hidden" id="table_jenis'+respon[i].id_transaksi+'" value="'+respon[i].jenis+'">'+
						'<input type="hidden" id="table_kode'+respon[i].id_transaksi+'" value="'+respon[i].perkiraan+'">'+
						'<input type="hidden" id="table_tipe'+respon[i].id_transaksi+'" value="'+respon[i].tipe+'">'
						]).draw(false);
				}
			}
		})
	}

	function get_perkiraan(){
		$.ajax({
			url: "{{ url('transaksi/get_perkiraan') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_kode]").html('');
					$("[name=popup_kode]").append('<option value="">--- Pilih Kategori ---</option>');
					for(i in result.data){
						$("[name=popup_kode]").append('<option value="'+result.data[i].id+'">'+result.data[i].kode_perkiraan+' - '+result.data[i].nama_perkiraan+'</option>');
					}
					
				}
			}
		})
	}

	

	function edit_transaksi(id){
		var id = $("#table_id"+id).val();
		var transaksi = $("#table_jenis"+id).val();
		var kode = $("#table_kode"+id).val();
		var tipe = $("#table_tipe"+id).val();

		$("[name=popup_id]").val(id);
		$("[name=popup_jenis]").val(transaksi);
		$("[name=popup_kode]").val(kode);
		$("[name=popup_tipe]").val(tipe);
		$("[name=popup_kode]").select2();

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

	}


	function hapus_transaksi(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('transaksi/hapus')}} ",
					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 = $("[name=popup_id]").val();
		var jenis = $("[name=popup_jenis]").val();
		var kode = $("[name=popup_kode]").val();
		var tipe = $("[name=popup_tipe]").val();

		if(jenis != ''){
			$.ajax({
				url: "{{ url('transaksi/simpan')}} ",
				type: 'post',
				data: {id : id, jenis : jenis, kode:kode, tipe:tipe},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					tb.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})
</script>

@endsection