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

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

	    <table class="table table-stripped" id="table_perkiraan">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Kode Perkiraan</th>
					<th>Nama Perkiraan</th>
					<th>Saldo Normal</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody id="tabel_perkiraan">
				
			</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">Form Input Perkiraan</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_perkiraan">
	      			<input type="hidden" name="popup_id">
	      			
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Penggunaan</label>
	      				<div class="col-sm-8">
	      					<select name="popup_guna" class="form-control select-search">
	      						<option value=""> --- Pilih --- </option>
	      						<option value="0">Header</option>
	      						<option value="1">Detail</option>
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Parent</label>
	      				<div class="col-sm-8">
	      					<select name="popup_parent" class="form-control select-search">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Kode Perkiraan</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_kode" class="form-control">
	      					<p class="help-block" id="help_kode"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nama Perkiraan</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_nama" class="form-control">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Saldo Normal</label>
	      				<div class="col-sm-8">
	      					<select name="popup_saldo" class="form-control select-search">
	      						<option value="">--Pilih--</option>
	      						<option value="D">Debet</option>
	      						<option value="K">Kredit</option>
	      					</select>
	      				</div>
	      			</div>
	      			
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<input type="hidden" name="store">

<script type="text/javascript"> 
	var tp;
	$(document).ready(function(){
		tp = $("#table_perkiraan").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('perkiraan/tampil_parent') }}',
	        columns: [
	            {data: 'kode_perkiraan', name: 'kode_perkiraan', orderable: false, searchable: false, render : function(data, type, full, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'kode_perkiraan', name: 'kode_perkiraan'},
	            {data: 'nama_perkiraan', name: 'nama_perkiraan'},
	            {data: 'saldo_normal', name: 'saldo_normal'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});

		$('#table_perkiraan tbody').on( 'click', 'tr', function () {
	        var data = tp.row( $(this)).data();
	        alert( data.id_perkiraan );
	    } );

		get_parent();
	})

	function get(){
		
	}

	$("#btn_tambah_perkiraan").click(function(){
		$("[name=popup_id]").val('');
		$("[name=popup_guna]").val('').trigger('change');
		$("[name=popup_parent]").val('').trigger('change');
		$("[name=popup_kode]").val('');
		$("#help_kode").text('');
		$("[name=popup_nama]").val('');
		$("[name=popup_saldo]").val('').trigger('change');
		$("#exampleModal").modal("show");
	})

	

	function get_parent(){
		$.ajax({
			url: '{{ url('perkiraan/get_parent') }}',
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.length > 0){
					$("[name=popup_parent]").append('<option value="" kode="">--- Pilih Parent ---</option>');
					for(i in result){
						$("[name=popup_parent]").append('<option value="'+result[i].id_perkiraan+'" kd_rek="'+result[i].kd_rek_1+'" >'+result[i].kode_perkiraan+' - '+result[i].nama_perkiraan+'</option>');
					}
				}
			}
		})
	}


	

	
	// function edit(id){
	// 	var id = $("#table_id"+id).val();
	// 	var level = $("#table_level"+id).val();
	// 	var kode = $("#table_kode"+id).val();
	// 	var nama = $("#table_nama"+id).val();
	// 	var saldo = $("#table_saldo"+id).val();
	// 	var parent = $("#table_parent"+id).val();
	// 	var saldo_awal = $("#table_saldo_awal"+id).val();
	// 	$("[name=popup_id]").val(id);
	// 	//$("[name=popup_level]").val(level);
	// 	$("[name=popup_level]").html('');
	// 	$("[name=popup_level]").append('<option value="'+level+'"> Level '+level+'</option>');
	// 	$("[name=popup_kode]").val(kode);
	// 	$("[name=popup_nama]").val(nama);
	// 	$("[name=popup_saldo]").val(saldo);
	// 	$("[name=popup_saldo_awal]").val(saldo_awal);
	// 	$("[name=popup_parent]").val(parent);
	// 	$("#exampleModal").modal('show');

	// }

	

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=popup_id]").val();
		var guna = $("[name=popup_guna]").val();
		var parent = $("[name=popup_parent]").val();
		var kode = $("[name=popup_kode]").val();
		var nama = $("[name=popup_nama]").val();
		var saldo = $("[name=popup_saldo]").val();

		if(guna != '' && parent != '' && saldo){
			$.ajax({
				url: "{{ url('perkiraan/get_adaakun')}} ",
				type: 'post',
				data: {kode : kode, parent : parent},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					// $("#exampleModal").modal('hide');
					if(respon.status == 0){
						$("#help_kode").text('');
						$.ajax({
							url: "{{ url('perkiraan/simpan') }}",
							type: 'post',
							dataType: 'json',
							data: $("#form_perkiraan").serialize(),
							headers : {
								'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
							},
							success: function(result){
								$("#exampleModal").modal('hide');
							}
						})
					}else{
						$("#help_kode").text('Kode Akun sudah tersedia');
					}
				}
			})
		}
	})
</script>
@endsection