$(function(){
	navigation_menuitem_search();
})

//CHARGEMENT DE LA PAGE
function init_plugin_navigation(){
	switch($.urlParam('page')){
		default:
		break;
	}
}

//Enregistrement des configurations
function navigation_setting_save(){
	$.action({ 
		action : 'navigation_setting_save', 
		fields :  $('#navigation-setting-form').toJson() 
	},function(){
		$.message('success','Enregistré');
	});
}

/** MENUITEM **/
//Récuperation d'une liste de menuitem dans le tableau #menuitems
function navigation_menuitem_search(callback){
	$.action({
		action : 'navigation_menuitem_search'
	},function(r){
		$('.navigation-template-item:visible').remove();
		for(var k in r.rows){
			navigation_menuitem_add(r.rows[k]);
		}
	});
}

//Ajout ou modification d'élément menuitem
function navigation_menuitem_save(){
	var data = $('#menuitem-form').toJson();
	$.action(data,function(r){
		$('#menuitem-form').attr('data-id','');
		navigation_menuitem_search();
		$('#quickform-modal').modal('hide');
		$.message('success', 'Enregistré');
	});
}

//Récuperation ou edition d'élément menuitem
function navigation_menuitem_edit(element,event){
	/*var line = $(element).closest('tr');
	$.action({action:'navigation_menuitem_edit',id:line.attr('data-id')},function(r){
		$.setForm('#menuitem-form',r);
		$('#menuitem-form').attr('data-id',r.id);
	});*/
	var idMenu = $(element).attr('data-id');
	var modal = $('#quickform-modal');
	modal.find('#quickform-modal-label').text('Édition de menu');
	modal.find('.modal-body').load('plugin/navigation/page.quick.item.php',{
		id: idMenu
	},function(){
		var modalFooter = modal.find('.modal-footer');
		var closeBtn = modalFooter.find('[data-dismiss="modal"]');
		modalFooter.text('').append(closeBtn);
		modalFooter.append('<div onclick="navigation_menuitem_save();" class="btn btn-success save-menu"><i class="fas fa-check"></i> Enregistrer</div>');
		if(idMenu){
			modalFooter.addClass('modal-footer-margin-auto');
			modalFooter.prepend('<div onclick="navigation_menuitem_delete();" class="btn btn-danger delete-menu mr-auto"><i class="fas fa-times"></i> Supprimer</div>');
		} else {
			modalFooter.removeClass('modal-footer-margin-auto');
		}
		init_components(modal);
	});
	modal.modal('show');
	if(!event)return;
	event.preventDefault();
	event.stopPropagation();
}

//Récuperation ou edition d'élément menuitem
function navigation_navigation_edit(element){
	$('.navigation-plus-button,.navigation-validate-button,.navigation-edit-item-button').show();
	$('.navigation-edit-button').hide();
	$('.navigation-menu').sortable({
		items: 'li:not(.pointer)',
		placeholder: "navigation-sortable-placeholder",
		axis: "x",
		cursor: "move",
		stop : function(e, ui){
			var sort = [];
			$('.navigation-template-item:visible').each(function(i,element){
				sort.push($(this).attr('data-id'));
			});
			$.action({
				action : 'navigation_menuitem_move',
				sort : sort
			});
		},
		//handle: ".navigation-template-item"
	});
}

function navigation_menuitem_add(data){

	if(!data) data = {};
	var data = $.extend({
		label : 'Nouveau menu',
		icon : 'user',
	},data);
	var tpl = $('.navigation-template-item.hide').get(0).outerHTML;
	var item = $(Mustache.render(tpl,data));
	
	item.removeClass('hide');
	$('.navigation-template-item.hide').before(item);	
}

function navigation_navigation_save(){
	$('.navigation-plus-button,.navigation-edit-item-button,.navigation-validate-button').hide();
	$('.navigation-edit-button').show();
	$('.navigation-menu').sortable("disable");
}

//Suppression d'élement menuitem
function navigation_menuitem_delete(element){
	if(!confirm('Êtes vous sûr de vouloir supprimer ce menu ?')) return;
	var line = $(element).closest('tr');
	$.action({
		action : 'navigation_menuitem_delete',
		id : $('#menuitem-form').attr('data-id')
	},function(r){
		line.remove();
		$('#menuitem-form').attr('data-id','');
		navigation_menuitem_search();
		$('#quickform-modal').modal('hide');
		$.message('info', 'Menu supprimé');
	});
}
