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

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

	    <table class="table table_satuan table-stripped">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Nama Mata Uang</th>
					<th>Inisial</th>
					<th>Simbol</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" 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">
	      			<input type="hidden" name="id_matauang">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nama Mata Uang</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">Inisial</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_inisial" class="form-control">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Simbol</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_simbol" 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 table_satuan;

	$(document).ready(function(){
		table_satuan = $(".table_satuan").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('matauang/tampil') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'nama_matauang', name: 'nama_matauang'},
	            {data: 'inisial', name: 'inisial', class: 'text-center'},
	            {data: 'simbol', name: 'simbol', class: 'text-center'},
	            {data: 'aksi', name: 'aksi', class: 'text-center', orderable: false, searchable: false}
	        ],
		});
		
	})
	
	$("#btn_tambah_satuan").click(function(){
		$("[name=id_matauang]").val('');
		$("[name=popup_nama]").val('');
		$("[name=popup_inisial]").val('');
		$("[name=popup_simbol]").val('');

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

	function tampil(){
		$.ajax({
			url: "{{ url('matauang/tampil')}}  ",
			type: 'get',
			dataType: 'json',
			success: function(respon){
				table_satuan.clear().draw();
				for(i in respon){
					table_satuan.row.add(['<div><center></center></div>',
						'<div>'+respon[i].nama_matauang+'</div>',
						'<div class="text-center">'+respon[i].inisial+'</div>',
						'<div class="text-center">'+respon[i].simbol+'</div>',
						'<div>'+respon[i].aksi+'</div>'+
						'<input type="hidden" id="table_id'+respon[i].id_matauang+'" value="'+respon[i].id_matauang+'">'+
						'<input type="hidden" id="table_nama'+respon[i].id_matauang+'" value="'+respon[i].nama_matauang+'">'+
						'<input type="hidden" id="table_inisial'+respon[i].id_matauang+'" value="'+respon[i].inisial+'">'+
						'<input type="hidden" id="table_simbol'+respon[i].id_matauang+'" value="'+respon[i].simbol+'">'
						]).draw(false);
				}
			}
		})
	}

	

	function edit(id){
		var id = $("#table_id"+id).val();
		var nama = $("#table_nama"+id).val();
		var inisial = $("#table_inisial"+id).val();
		var simbol = $("#table_simbol"+id).val();

		$("[name=id_matauang]").val(id);
		$("[name=popup_nama]").val(nama);
		$("[name=popup_inisial]").val(inisial);
		$("[name=popup_simbol]").val(simbol);

		$("#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('matauang/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						table_satuan.ajax.reload();
					}
				})
			}
		})
	}

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id_matauang]").val();
		var nama = $("[name=popup_nama]").val();
		var inisial = $("[name=popup_inisial]").val();
		var simbol = $("[name=popup_simbol]").val();
		
		if(nama != '' && inisial != ''){
			$.ajax({
				url: "{{ url('matauang/simpan')}} ",
				type: 'post',
				data: {id_matauang : id,  nama_matauang : nama, inisial : inisial, simbol : simbol},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					table_satuan.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})
</script>

@endsection