# -*- encoding : utf-8 -*-

class Admin::RealisationsController < ApplicationController
	before_filter :auth_admin

	layout "admin"


	before_filter :find_realisations




	def index 

	end


  


	def cible
		@realisations = Realisation.all

		render :layout => false
	end

	
	def new
		@realisation = Realisation.new()
   

	end

	def edit
		@realisation = Realisation.find(params[:id])
	end
  
	def show
		@realisation = Realisation.find(params[:id])
	end


	def create
		@realisation = Realisation.new(params.require(:realisation).permit!)


		if @realisation.save
      self.find_realisations
			flash[:notice] = "La catégorie à été ajouté avec succès."
		

		else
			render :action => "new" 
		
		end
	
	end
	

	def update
		@realisation = Realisation.find(params[:id])

		
		
		
	
		
		@reorder = true if params[:reorder]
		

		
		
	
		
		
		respond_to do |format|
			if @realisation.update_attributes(params.require(:realisation).permit!)
				
				#Realisation.all.each do |mi|
				#	mi.set_permalink
				
				#end
			
				flash[:notice] = "Le menu à été modifié avec succès."
				format.html { redirect_to(admin_realisations_path(:parent_id => @realisation.parent_id)) }
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					   @realisation = Realisation.find(@realisation.id)
						render :action => "update_row" }
				end
			else
				
				flash[:error] = "Cet élément de menu n'a pas pu être déplacé. Vérifiez que son 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
		@realisation = Realisation.find(params[:id])

		@realisation.destroy
    
  
    flash[:notice] = "Le produit à bien été supprimé."
	end
	
	
	protected
	
	def find_realisations    
    @realisations = Realisation.all
		

	
  end
  
end