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

class Admin::ProductSizesController < ApplicationController
	before_filter :auth_admin

	layout "admin"


	before_filter :find_product_sizes


	def index 

	end


  def reorder

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

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


	def cible
		@product_sizes = ProductSize.all

		render :layout => false
	end

	
	def new
		@product_size = ProductSize.new()
    


	end

	def edit
		@product_size = ProductSize.find(params[:id])
	end
  
	def show
		@product_size = ProductSize.find(params[:id])
	end


	def create
		@product_size = ProductSize.new(params.require(:product_size).permit!)


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

		else
			render :action => "new" 
		
		end
	
	end
	

	def update
		@product_size = ProductSize.find(params[:id])


		
	
		
		
		respond_to do |format|
			if @product_size.update_attributes(params.require(:product_size).permit!)
				

			
				flash[:notice] = "Le menu à été modifié avec succès."
				
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					   @product_size = ProductSize.find(@product_size.id)
						render :action => "update_row" }
				end
      end
			
		end
    
    
    
    

	end


	def destroy
		@product_size = ProductSize.find(params[:id])
    @product_size.destroy
    find_product_sizes
  
    flash[:notice] = "La catégorie à bien été supprimée."
	end
	
	
	protected
	
	def find_product_sizes    
    
    @product_sizes = ProductSize.where("archived is null").order("position ASC")
		

	
  end
  
end
