<div class="container-fluid" id="container-wrapper">
	<div class="d-sm-flex align-items-center justify-content-between mb-4">
		<h1 class="h3 mb-0 text-gray-800"><?= $title;  ?></h1>
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a href="./">Home</a></li>
			<li class="breadcrumb-item active" aria-current="page"><?= $title;  ?></li>
		</ol>
	</div>
	<div class="row">
		<div class="col-lg-12 mb-4">
			
			<div class="card">
				<div class="card-header pb-0">
					<div class="input-group input-group-sm">
						<div class="input-group-prepend">
							<span class="input-group-text" for"limits">LIMIT</span>
						</div>
						<select id="limits" name="limits" class="form-control custom-select w-60px" onchange="search_akun()">
							<option value="10">10</option>
							<option value="20">20</option>
							<option value="50">50</option>
							<option value="100">100</option>
						</select>
						<div class="input-group-prepend">
							<span class="input-group-text" for="sortBy">SORT</span>
						</div>
						<select id="sortBy" class="form-control custom-select w-60px" onchange="search_akun()">
							<option value="ASC" selected>ASC</option>
							<option value="DESC">DESC</option>
						</select>
						
						<input type="text" id="keywords" class="form-control w-10" placeholder="Cari data" onkeyup="search_akun();"/>
						<div class="input-group-append">
							<button class="btn btn-danger btn-sm clear_cari" type="button"><i class="fa fa-times"></i> Clear</button>
							<a href="javascript:void(0)" class="btn btn-sm btn-primary shadow-sm add_akun">
								<i class="fa fa-fw fa-plus fa-sm text-white-50"></i>
								Tambah Akun
							</a>
							<a href="<?= base_url(); ?>masterdata/cetak_akun" class="btn btn-sm btn-secondary" target="_blank">
								<i class="fa fa-fw fa-print fa-sm text-white-50"></i>
								Cetak
							</a>
						</div>
						</div>
					</div>
					
					<div class="card-body" id="data_akun">
						
					</div><!-- /.card-body -->
				</div><!-- /.card -->
				
			</div>
		</div>
	</div>
	<!-- /.container-fluid -->
	
	<div class="modal fade" id="modal_add" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">Add Akun</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 autocomplete="off" class="form-add" id="form-add-akun">
						<div class="load-add"></div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-success simpan_akun">Simpan</button>
					<button type="button" class="btn btn-danger" data-dismiss="modal">Tutup</button>
				</div>
			</div>
		</div>
	</div>
	
	
	<div aria-hidden="true" aria-labelledby="myModalLabel" class="modal fade" id="confirm-delete" role="dialog" tabindex="-1">
		<div class="modal-dialog">
			<div class="modal-content flat">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
					<button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
				</div>
				<div class="modal-body">
					<p>Anda akan menghapus satu url, prosedur ini tidak dapat diubah.</p>
					<p>Apakah Anda ingin melanjutkan?</p>
					<p class="debug-url"></p>
					<input type="hidden" id="data-hapus">
				</div>
				<div class="modal-footer">
					<button class="btn btn-secondary rounded-0" data-dismiss="modal" type="button">Batal</button> 
					<button class="btn btn-danger rounded-0 hapus_akun" type="button">YA</button> 
				</div>
			</div>
		</div>
	</div>
	
	<script>
		search_akun();
		function search_akun(page_num){
			page_num = page_num?page_num:0;
			var keywords = $('#keywords').val();
			var sortBy = $('#sortBy').val();
			var limits = $('#limits').val();
			
			$.ajax({
				type: 'POST',
				url: base_url+"masterdata/ajax_akun/"+page_num,
				data:{page:page_num,keywords:keywords,sortBy:sortBy,limits:limits},
				beforeSend: function(){
					$('body').loading();
				},
				success: function(html){
					$('#halaman').val(page_num);
					$('#data_akun').html(html);
					$('body').loading('stop');
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			});
		}
		
		$(document).on('click','.add_akun',function(e){
			e.preventDefault();
			
			$.ajax({
				'url': base_url + 'masterdata/add_akun',
				'method': 'POST',
				success: function(data) {
					
					$("#modal_add").modal('show');
					$(".load-add").html(data);
					
				},
				error: function (xhr, ajaxOptions, thrownError) {
					sweet('Peringatan!!!',thrownError,'warning','warning');
				}
			})
		});
		$(document).on('click','.simpan_akun',function(e){
			e.preventDefault();
			
			var saldo_kas = angka($('#saldo_kas').val());
			var jumlah_kas = angka($('#jumlah_kas').val());
			
			var formElements = $("#form-add-akun").serialize();
			$.ajax({
				url: base_url + 'masterdata/simpan_akun',
				data: formElements,
				method: 'POST',
				dataType:'json',
				beforeSend: function(){
					$("body").loading({zIndex:1060});
				},
				success: function(data) {
					if(data.status==true){
						search_akun();
						showNotif('bottom-right','Simpan data',data.msg,'success');
						$('#modal_add').modal('hide');
						}else{
						showNotif('bottom-right','Simpan data',data.msg,'warning');
					}
					$('body').loading('stop');
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			});
		});
		
		$(document).on('click','.modal_hapus_akun',function(e){
			e.preventDefault();
			var id = $(this).attr('data-id');
			console.log(id)
			$('#data-hapus').val(id);
			$('#confirm-delete').modal('show');
		})
		$(document).on('click','.hapus_akun',function(e){
			e.preventDefault();
			var id = $("#data-hapus").val();
			// var halaman = $("#halaman").val();
			$.ajax({
				url: base_url + 'masterdata/hapus_akun',
				data: {type:'hapus',id:id},
				method: 'POST',
				dataType:'json',
				beforeSend: function(){
					$('body').loading();
				},
				success: function(data) {
					if(data.status==true){
						showNotif('bottom-right','Hapus data',data.msg,'success');
						$('#confirm-delete').modal('hide');
						search_akun();
						}else{
						sweet('Peringatan!!!',data.msg,'warning','warning');
					}
					
					$('body').loading('stop');
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}	
			});
		});
		// $('#confirm-delete').on('show.bs.modal', function(e) {
		// $('#data-hapus').val($(e.relatedTarget).data('id'));
		// });
		
		$('.clear_cari').on('click', function(){
			$('#keywords').val('');
			search_akun();
		});
		
	</script>			