var navigationMenuEdited = false;

//CHARGEMENT DE LA PAGE SETTING
function init_setting_global_navigation(){

	/*switch($.urlParam('page')){
		default:
		break;
	}*/

	window.onbeforeunload = function(e){
	  if(!navigationMenuEdited) return e=null;
	  return 'Vous n\'avez pas sauvgardé certaines modifications sur le menu, êtes vous sûr de vouloir quitter la page?';
	  
	};
		

	navigation_tree_search();
	$( ".navigation-tree-list" ).sortable({
		placeholder: "navigation-item-placeholder",
		handle: ".navigation-item-header",
		cancel: ".not-editable",
		distance : 15,
      	update : function(){
      		navigationMenuEdited = true;
      	},
		grid: [ 50, 1 ],
		sort: function(event,ui){
			var leftLimit = ui.item.hasClass('navigation-line-child') ? -20: 15;
			if(ui.position.left < leftLimit){
				ui.helper.css('left',leftLimit+'px');
			}

			if(ui.position.left >65 ){
				left = 65;
				ui.helper.css('left',left+'px');
			}
			if(ui.item.index()==1){
				ui.helper.css('left',leftLimit+'px');
			}
		},
		stop: function(event,ui){


			if(ui.position.left>15){
				if(!ui.item.hasClass('navigation-line-child')) navigationMenuEdited = true;
				ui.item.addClass('navigation-line-child');
			}else{
				if(ui.item.hasClass('navigation-line-child')) navigationMenuEdited = true;
				ui.item.removeClass('navigation-line-child');
			}

		},
		 helper: "clone",
      opacity: 0.8
    });
    $( ".navigation-tree > u" ).disableSelection();

	
}




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






/** Editeur d'arborescence **/

//Enregistrement des configurations
function navigation_setting_save(){

	var items = [];
	var lastParentIndex = 0;
	$('.navigation-line:visible').each(function(i,element){
		var line = $(element);
		var item = {};
		item.label = line.find('.item-label').val();
		item.visibility = line.find('input.item-visibility').val();
		item.url = line.find('.item-url').val();
		item.icon = line.find('.item-icon').val();
		item.target = line.find('[name="item-target"]:checked').val();

		if(line.hasClass('navigation-line-child')){
			items[lastParentIndex].childs.push(item);
		}else{
			lastParentIndex = items.length;
			item.childs = [];
			items.push(item);
		}

	});
	

	$.action({ 
		action : 'navigation_setting_save', 
		fields :  $('#navigation-setting-form').toJson(),
		items : items,
		menu : $('#navigation-menu').val()
	},function(){
		navigationMenuEdited = false;
		$.message('success','Enregistré');
	});
}


function navigation_tree_search(callback){
	var menu = $('#navigation-menu').val();
	if(!menu || menu=='') return;
	$.action({
		action : 'navigation_tree_search',
		menu : menu
	},function(r){
		$('.navigation-tree > ul > li:visible').remove();
		for(var key in r.rows){
			var line =  r.rows[key];
			navigation_tree_add(line);
			if(line.childs){
				for(var key2 in line.childs){
					navigation_tree_add(line.childs[key2]);
				}
			}
		}

		$('.navigation-line [name="item-target"]').each(function(){
			var input = $(this);
			if(input.attr('data-value')==input.val()) input.prop('checked', true);
		});
		init_components();
	});
}


function navigation_tree_new(element){
	var data = {
		label : 'Sans titre',
		icon : 'far fa-bookmark',
		url : '',
		labelType : 'non définis'
	};


	if(element){
		data = $(element).closest('.list-group-item').data();
		data.target = 'redirect';
		$.message('info','Lien "'+data.label+'" ajouté au menu');
	}

	var line = navigation_tree_add(data);
	$('.navigation-line .navigation-item-form').addClass('hidden');
	line.find('.navigation-item-form').removeClass('hidden');
	init_components();
	line.find('.item-label').select();

}

function navigation_tree_add(data){
	var tpl = $('.navigation-line:hidden').get(0).outerHTML;
	tpl = tpl.replace('data-type="user-tpl"','data-type="user"');
	var line = $(Mustache.render(tpl,data));

	line.removeClass('hidden');
	
	$('.navigation-tree > ul').append(line);


	line.find('[name="item-target"][value="'+data.target+'"]').prop('checked',true);

	line.find('.item-label').keyup(function(){
		navigationMenuEdited = true;
		line.find('.navigation-item-label').text($(this).val());
	});
	line.find('.item-url').keyup(function(){
		navigationMenuEdited = true;
		line.find('.navigation-item-url').text($(this).val());
	});
	line.find('[name="item-target"]').click(function(){
		navigationMenuEdited = true;
		line.find('.navigation-item-type span').text($(this).parent().text());
	});
	line.find('.item-icon').change(function(){
		navigationMenuEdited = true;
		line.find('.navigation-item-icon i').attr('class',$(this).val());
	});
	return line;
}

function navigation_tree_toggle(element){
	var line = $(element).closest('.navigation-line');
	$('.navigation-item-form',line).toggleClass('hidden');
}

function navigation_tree_remove(element){
	if(!confirm('Êtes-vous sûr de vouloir supprimer cet élement?')) return;
	navigationMenuEdited = true;
	$(element).closest('.navigation-line').remove()
}

