var modal_confirm_delete_options = {
	title: "Etes-vous sur ?",
	text: "Cet enregistrement sera effacé.",
	type: "warning",
	showCancelButton: true,
	confirmButtonClass: "btn-danger",
	confirmButtonText: "Oui !",
	cancelButtonText: "Annuler"
};


function modal_confirm_delete(id,module,route,callback) {
	// console.log(id);
	var $loader = $('#loader');
	Swal.fire(modal_confirm_delete_options).then(function(result) {
		if (result.value) {
			$.ajaxSetup({
  				headers: {
    				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  				}
			});
			// console.log('/' + route +'/'+ id);
			$.ajax({
				url    : '/' + route +'/'+ id,
				method : 'DELETE',
				success : function(resp){
					if (resp.success) {
						Swal.fire("Supprimé !", "L'enregistrement a été supprimé.", "success");
						$('#'+module+'-modal').modal('hide');
						callback();
					} else {
						Swal.fire("Erreur!", "L'enregistrement n'a pu être supprimé pour les raisons suivantes : "+resp.message, "danger");
						if(resp.code == 401){
							location.reload();
						}
					}
					$loader.hide();
				}
			});
		} else if (result.dismiss === Swal.DismissReason.cancel) {

		}
	})
}

function modal_save(id,route,module,callback,$button) {
	var $loader = $('#loader');
	var $data = $('#'+module+'-form-data').serialize();
	var $method = $button.attr('data-method');
	var $url = ($method == 'POST') ? '/' + route : '/' + route +'/'+ id;

	$button.prop('disabled', true);
	$button.html('en cours...');
	$loader.show();

	$.ajax({
		url: $url,
		data: $data,
		method : $method,
		success: function(resp){

			$button.prop('disabled', false);
			$button.html('Enregistrer');
			$loader.hide();

			if (resp.success) {
				$('#'+module+'-form-data').each(function(){
					this.reset();
				});
				$('#btn-'+module+'-delete').hide();
				$('#'+module+'-modal').modal('hide');
				callback();
			} else {
				swal(resp.message);
				if (resp.code == 401) {
					location.reload();
				}
			}
		}
	});
}

function modal_add(module, callback) {
	/*
	$('#'+module+'-form-data').each(function(){
		this.reset();
	});
	*/
	$('#btn-'+module+'-save').attr('data-method', 'POST');
	$('#btn-'+module+'-delete').hide();
	$('#'+module+'-modal').modal('show');
	callback();
}

function modal_edit(id,route,module,callback) {
	var $loader = $('#loader');
	$loader.show();
	$('#btn-'+module+'-delete').show();

	$.get('/'+route+'/'+id, function(resp){
		if (resp.success) {
			/*
			$('#'+module+'-form-data').each(function(){
				this.reset();
			});
			*/
			callback(resp.data);

			$('#btn-'+module+'-save').attr('data-method', 'PUT');
			$('#btn-'+module+'-delete').show();
			$('#'+module+'-modal').modal('show');
		} else {
			if (resp.code == 401) {
				location.reload();
			}
		}
		$loader.hide();
	});
}

function initModalAdmin(route,module,callback,populate,Table) {
	
	var selector = getDatatablesSelector(model); 
/*
	$( selector + ' tbody').on( 'click', 'tr', function (e) {
		e.preventDefault();
		// $(this).toggleClass('selected');
		id = Table.row(this).id();
		modal_edit(id,route,module,populate);
    } );
*/
/*
	$('#btn-'+module+'-add').click(function(e){
		e.preventDefault();
		modal_add(module, populate);
	});
*/
/*
	$('#'+module+'-table').on('click', '.btn-'+module+'-edit', function(e){
		e.preventDefault();
		var id = $(this).attr('data-id');
		modal_edit(id,route,module,populate);
	});
*/
/*	
	$('#'+module+'-table').on('click', '.btn-'+module+'-delete', function(e){
		e.preventDefault();
		var id = $(this).attr('data-id');   
		modal_confirm_delete(id,route,callback);
	});
*/

	$('#btn-'+module+'-delete').click(function(e){
		e.preventDefault();
		var id = $('#id').val();
		modal_confirm_delete(id,module,route,callback);
	});

/*
	$('#btn-'+module+'-save').click(function(e){
		e.preventDefault();
		var id = $('#id').val();
		modal_save(id,route,module,callback,$(this));
	});
*/
}

