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

class Admin::ProductsController < ApplicationController
	before_filter :auth_admin

	layout "admin"


	before_filter :find_products


  before_filter :collection_for_parent_select, :except => [:index, :show]

  def collection_for_parent_select
    @categories_select = []
    
    ProductCategory.where("parent_id is null").each do |product_category|
      ancestry_options product_category
      
    end
    
    
    
    #ancestry_options(ProductCategory.joins(:product_category_langs).order('product_category_langs.name').unscoped.all) {|i| "#{'-' * i.ancestors.size} #{i.lang('fr').name}" }
  end

  def ancestry_options(item)
    @categories_select << [('--' * item.ancestors.size).to_s+item.lang("fr").name,item.id]
    
    item.children.each do |product_category|
      ancestry_options product_category
      
    end
    
  end


	def index 

	end


  def reorder

    i = 1
    
    
    params[:products_ids].each do |id|
      

      product = Product.find(id)
      product.position = i
      puts i
      product.update_attributes :position => i
      
      i = i + 1
      
      
    end
    
		if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
			
      find_products
    end
    
    flash[:success] = "Menus réorganisés avec succcès."
    
   
    
  end


	def cible
		@products = Product.all

		render :layout => false
	end

	
	def new
		@product = Product.new()
    
    LangSite.all.each do |ls|
      @product.product_langs << ProductLang.new(:lang => ls.slug)
    end

	end

	def edit
		@product = Product.find(params[:id])
	end
  
	def stock
		@product = Product.find(params[:id])
	end
  
	def show
		@product = Product.find(params[:id])
	end


	def create
		@product = Product.new(params.require(:product).permit!)


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

		else
			render :action => "new" 
		
		end
	
	end
	

	def update
		@product = Product.find(params[:id])

		
		
		
	
		
		@reorder = true if params[:reorder]
		

		
		
	
		
		
		respond_to do |format|
			if @product.update_attributes(params.require(:product).permit!)
				
				#Product.all.each do |mi|
				#	mi.set_permalink
				
				#end
			
				flash[:notice] = "Le menu à été modifié avec succès."
				format.html { redirect_to(admin_products_path(:parent_id => @product.parent_id)) }
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					   @product = Product.find(@product.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
		@product = Product.find(params[:id])

		@product.destroy
    
  
    flash[:notice] = "Le produit à bien été supprimé."
	end
	
	
	protected
	
	def find_products    
    @products = Product.not_archived.order("position ASC")
		

	
  end
  
end
