@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> User</a></li>
			<li class="active">Group</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Group</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">    

	    &nbsp; <button class="btn btn-primary" type="button" id="btn_tambah_group">Tambah</button>
	    <table class="table datatable-basic table-striped table_group">
	    	<thead>
				<tr>
					<th width="4%">No</th>
					<th>Nama Group</th>
					<th class="text-center" width="15%">Aksi</th>
				</tr>	
			</thead>
			<tbody>

			</tbody>
	    </table>
	    </div>
	</div>
</div>

<div class="modal fade" id="exampleModal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data Form Group</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 form-validate-jquery" autocomplete="off" id="form_group">
	      			<input type="hidden" name="popup_id">
	      					<div class="form-group">
			      				<label class="control-label col-sm-4">Nama Group</label>
			      				<div class="col-sm-8">
			      					<input type="text" name="popup_nama" class="form-control">
			      				</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;
	var formBarang = $("#form_group");

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

	})

	$("#btn_tambah_group").click(function(){
		$("[name=popup_id]").val('');
		$("[name=popup_nama]").val('');
		disabled('#btn_popup_simpan', false);
		
		$("#exampleModal").modal("show");
	})	
	

	function edit_table(id){
		var id_group = $("#table_id"+id).val();
		var nama = $("#table_nama"+id).val();

		$("[name=popup_id]").val(id_group);
		$("[name=popup_nama]").val(nama);
		$("#exampleModal").modal('show');
	}


	function hapus(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('group/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 nama = $("[name=popup_nama]").val();
		if(nama != ''){
			disabled('#btn_popup_simpan', true);
			$.ajax({
				url: "{{ url('group/simpan')}} ",
				type: 'post',
				data: $("#form_group").serialize(),
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					if(respon.status == 1){
					tb.ajax.reload();
						$("#exampleModal").modal('hide');
					}else{
						$("#info_barang").text('Kode sudah ada!');
					}
					 disabled('#btn_popup_simpan', false);
				}
			})
		}
	})



</script>


@endsection