# -*- encoding : utf-8 -*-
class Admin::MenuItemsController < ApplicationController
	before_filter :authenticate_admin!

	
	layout "admin"

	
	def index
		
		
		
		if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
			
			
			params[:parent_id] = nil if !params[:parent_id]
			
			@menu_parent = MenuItem.find(params[:parent_id]) if params[:parent_id]
			
			
		
			@order = "position ASC"

			@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order).page(magick_page()).per(magick_per_page())

			if @menu_items.num_pages.to_i < magick_page().to_i
				params[:page] = @menu_items.num_pages
				@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order).page(magick_page()).per(magick_per_page())
			end
		
		
		
		
		else
			redirect_to admin_root_path, :alert => "Un menu doit être séléctionné."
		end
	end

	def new

		
		
		@menu_item = MenuItem.new(:menu_id => params[:menu_id],:parent_id => params[:parent_id])
		
		params[:menu_item_content_type] = "Page" if !params[:menu_item_content_type]
		
		@content = params[:menu_item_content_type].constantize.new()
		@menu_item.menu_content = @content
		
		
		
		
	end

	def create
	
		params_content = params[:menu_item].delete(:menu_content_attributes)
		content_type = params[:menu_item][:menu_content_type]
		
		puts params[:menu_item][:menu_content_type]
		
		@menu_item= MenuItem.new(params[:menu_item])
		content = content_type.constantize.new(params_content)
		
		@menu_item.menu_content = content
	

		
		respond_to do |format|
			if @menu_item.save
				MenuItem.all.each do |mi|
					mi.set_permalink
				end
				
				flash[:notice] = "Le menu à été ajouté avec succès."
				format.html { 
					redirect_to(admin_menu_items_path(:menu_id => @menu_item.menu_id, :parent_id => @menu_item.parent_id)) 
					}
				
			else
				format.html { render :action => "new" }
			end
		end
	
	end

	def edit

		@menu_item= MenuItem.find(params[:id])
		
	end

	def update

		@menu_item= MenuItem.find(params[:id])
		
		@menu = @menu_item.menu
		
		if request.xhr?
			@menu_parent = @menu_item.parent if @menu_item.parent
			@menu_items = MenuItem.where(:parent_id => @menu_item.parent_id, :menu_id => @menu_item.menu_id).order(:position)
		end
	
		if params[:menu_item][:parent_id] and params[:menu_item][:parent_id] == "no-menu-selected"
			@menu_item.parent_id = nil
			@menu_item.save
			params[:menu_item].delete(:parent_id)
			
		end
		
		@reorder = true if params[:reorder]
		
		
		
		
	
		
		
		respond_to do |format|
			if @menu_item.update_attributes(params[:menu_item])
				puts "TEST"
				MenuItem.all.each do |mi|
					mi.set_permalink
				
				end
			
				flash[:notice] = "Le menu à été modifié avec succès."
				format.html { redirect_to(admin_menu_items_path(:parent_id => @menu_item.parent_id, :menu_id => @menu_item.menu_id)) }
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					
						render :action => "show" }
				end
			else
				
				flash[:alert] = "Cet élément de menu n'a pas pu être déplacé. Vérifiez que sont lien permanent n'éxiste pas déjà dans l'élément cible."
				if @reorder
					format.js { render :action => "update_reorder_failled" }
				else
					format.html { render :action => "edit" }
					format.js { render :action => "edit" }
				end
				
				
			end
		end
		
	end

	def destroy
		@menu_item = MenuItem.find(params[:id])
		@menu_item.destroy
		flash[:notice] = "L'élément de menu a été supprimé avec succès."
		respond_to do |format|
		
				
				format.html { redirect_to(admin_menu_items_path(:menu_id => @menu_item.menu_id, :parent_id => @menu_item.parent_id)) }
				format.js
		
	
		end
	end

	def show
		@menu_item= MenuItem.find(params[:id])
	end
	
	def cible
		
		params[:menu_id] = params[:menu_id] || 1

			if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])


				params[:parent_id] = nil if !params[:parent_id]

				@menu_parent = MenuItem.find(params[:parent_id]) if params[:parent_id]



				@order = "position ASC"

				@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order).page(magick_page()).per(magick_per_page())

				if @menu_items.num_pages.to_i < magick_page().to_i
					params[:page] = @menu_items.num_pages
					@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order).page(magick_page()).per(magick_per_page())
				end




			else
				redirect_to admin_root_path, :alert => "Un menu doit être séléctionné."
			end
		
		render :layout => false
	
	end
	
	def clone
		@menu_item= MenuItem.find(params[:id])
		
		@new = @menu_item.dup
		@new.name = @new.name+" copie"
		@slug = @new.slug+"-copie"
		@new.slug = @slug
		i = 0
		while MenuItem.where(:slug => @new.slug, :menu_id => @new.menu_id, :parent_id => @new.parent_id).exists?
			i= i+1
			@new.slug = @slug+"-#{i}"
		end
		@new.name = @new.name+" (#{i})" if i >0
		@new.id
		@new.menu_content = @menu_item.menu_content.dup
		@new.save
		MenuItem.all.each do |mi|
			mi.set_permalink
		
		end
		redirect_to :back
	end

end
