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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Konversi 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>
	    <div class="table-responsive">
	    <table class="table datatable-basic table-stripped table_konversi">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Nama Konversi</th>
					<th>Mata Uang Awal</th>
					<th>Mata Uang Akhir</th>{{-- 
                    <th>Nilai Awal</th>
                    <th>Nilai Aakhir</th> --}}
					<th class="text-center">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" 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="popup_id">
                <div class="form-group">
	      				<label class="control-label col-sm-4">Nama Konversi</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">Mata Uang Awal</label>
	      				<div class="col-sm-8">
	      					<!-- <input type="text" name="popup_matauang_awal" class="form-control"> -->
	      					<select class="form-control select-search" name="popup_matauang_awal">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nilai Awal</label>
	      				<div class="col-sm-8">
	      					<input type="number" name="popup_nilai_awal" class="form-control">
	      				</div>
	      			</div>
                    <div class="form-group">
	      				<label class="control-label col-sm-4">Mata Uang Akhir</label>
	      				<div class="col-sm-8">
	      					<!-- <input type="text" name="popup_matauang_akhir" class="form-control"> -->
	      					<select class="form-control select-search" name="popup_matauang_akhir">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nilai Akhir</label>
	      				<div class="col-sm-8">
	      					<input type="number" name="popup_nilai_akhir" 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;

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

		
		get_matauang();
	})

	$("#btn_tambah_satuan").click(function(){
		$("[name=popup_id]").val('');
		$("[name=popup_nama]").val('');
		$("[name=popup_matauang_awal]").val(null).trigger('change');
		$("[name=popup_matauang_akhir]").val(null).trigger('change');
		$("[name=popup_nilai_awal]").val('');
		$("[name=popup_nilai_akhir]").val('');

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






	function get_matauang(){
		$.ajax({
			url: "{{ url('konversimatauang/get_matauang') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
					$("[name=popup_matauang_awal]").html('');
					$("[name=popup_matauang_akhir]").html('');
					$("[name=popup_matauang_awal]").append('<option value="">--- Pilih Mata Uang ---</option>');
					$("[name=popup_matauang_akhir]").append('<option value="">--- Pilih Mata Uang ---</option>');
					for(i in result){
						$("[name=popup_matauang_awal]").append('<option value="'+result[i].id_matauang+'">'+result[i].nama_matauang+'</option>');
						$("[name=popup_matauang_akhir]").append('<option value="'+result[i].id_matauang+'">'+result[i].nama_matauang+'</option>');
					}
			}
		})
	}
	

	function edit(id){
		var id = $("#table_id"+id).val();
		var nama = $("#table_nama"+id).val();
		var satuan_awal = $("#table_matauang_awal"+id).val();
		var satuan_akhir = $("#table_matauang_akhir"+id).val();
		var nilai_awal = $("#table_nilai_awal"+id).val();
		var nilai_akhir = $("#table_nilai_akhir"+id).val();
		

		$("[name=popup_id]").val(id);
		$("[name=popup_nama]").val(nama);
		$("[name=popup_matauang_awal]").val(satuan_awal).trigger('change');
		$("[name=popup_matauang_akhir]").val(satuan_akhir).trigger('change');
		$("[name=popup_nilai_awal]").val(nilai_awal);
		$("[name=popup_nilai_akhir]").val(nilai_akhir);

		$("#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('konversimatauang/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 nama = $("[name=popup_nama]").val();
		var matauang_awal = $("[name=popup_matauang_awal]").val();
		var matauang_akhir = $("[name=popup_matauang_akhir]").val();
		var nilai_awal = $("[name=popup_nilai_awal]").val();
		var nilai_akhir = $("[name=popup_nilai_akhir]").val();

		if(nama != '' && matauang_awal != '' && matauang_akhir != '' && nilai_awal != '' && nilai_akhir != ''){
			disabled('#btn_popup_simpan', true);
			$.ajax({
				url: "{{ url('konversimatauang/simpan')}} ",
				type: 'post',
				data: {id : id, nama : nama, matauang_awal:matauang_awal, matauang_akhir:matauang_akhir, nilai_awal:nilai_awal, nilai_akhir:nilai_akhir},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					tb.ajax.reload();
					disabled('#btn_popup_simpan', false);
					$("#exampleModal").modal('hide');
				}
			})
		}
	})
</script>

@endsection