$(function() {
	$(document).on("click",".fn_divisa",function(event){
	   //$this.button('loading');
	    $.ajaxblock();
	    let action = 'view', datos='', method = 'PUT';
	    if ($(this).attr('data-action')) {
	    	datos = {'action':'reload_divisa'};
	    	method = 'POST';
	    	action = 'reload_divisa';
	    }else{
	    	datos = {'id':$(this).attr('data-id'),'action':'view'};
	    }
	   	$.ajaxSetup({
	        headers: {
	            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	        }
	    });
		$.ajax({
		  data: datos,
		  type: method,
		  dataType: 'json',
		  url: $(this).attr('data-url'),
		}).done(function( data, textStatus, jqXHR ){
			switch(action) {
			  	case 'view':
			  		if(!jQuery.isEmptyObject(data.success)){
			  			$('.datatable').DataTable().ajax.reload();
			  		}else{
			  			swal({title:data.error,icon:"warning"});
			  		}
			  	break;
			  	case 'reload_divisa':
			  		if(!jQuery.isEmptyObject(data.success)){
			  			if (!jQuery.isEmptyObject(data.divisa.error)) {
			  				swal({title:data.divisa.error,icon:"warning"});
			  			}else{
				  			$('input[name*="venta"]').val(data.divisa.venta);
				  			$('input[name*="compra"]').val(data.divisa.compra);
			  			}
			  		}else{

			  		}
		    	break;
			}
			$.ajaxunblock();
		}).fail(function( jqXHR, textStatus, errorThrown ){
		  alert( "Solicitud fallida:" + textStatus );
		  $.ajaxunblock();
		});   
		event.preventDefault();
	});
});