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

class Admin::PProductSubCatsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def admin_space
    @admin_space = "stocks"
  end
  
	before_action :find_p_product_sub_cats


	def index 

	end


  def reorder

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

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


	def cible
		@p_product_sub_cats = PProductSubCat.all

		render :layout => false
	end

	
	def new
		@p_product_sub_cat = PProductSubCat.new(:parent_id => params[:parent_id])
    
	end

	def edit
		@p_product_sub_cat = PProductSubCat.find(params[:id])
	end


	def create
		@p_product_sub_cat = PProductSubCat.new(params.require(:p_product_sub_cat).permit!)


		if @p_product_sub_cat.save
      params[:parent_id] =@p_product_sub_cat.parent_id
      
			self.find_p_product_sub_cats
			flash[:notice] = "La catégorie à été ajouté avec succès."
		

		else
			render :action => "new" 
		
		end
	
	end
	

	def update
		@p_product_sub_cat = PProductSubCat.find(params[:id])

		
		
		
		if request.xhr?
			@p_product_sub_cat_parent = @p_product_sub_cat.parent if @p_product_sub_cat.parent
			@p_product_sub_cats = PProductSubCat.where(:parent_id => @p_product_sub_cat.parent_id).order(:position)
		end
	
		if params[:p_product_sub_cat][:parent_id] and params[:p_product_sub_cat][:parent_id] == "no-menu-selected"
			params[:p_product_sub_cat][:parent_id] = nil
		end
		
		@reorder = true if params[:reorder]
		

		
		
	
		
		
		respond_to do |format|
			if @p_product_sub_cat.update_attributes(params.require(:p_product_sub_cat).permit!)
				
				#PProductSubCat.all.each do |mi|
				#	mi.set_permalink
				
				#end
			
				flash[:notice] = "Le menu à été modifié avec succès."
				format.html { redirect_to(admin_p_product_sub_cats_path(:parent_id => @p_product_sub_cat.parent_id)) }
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					   @p_product_sub_cat = PProductSubCat.find(@p_product_sub_cat.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
		@p_product_sub_cat = PProductSubCat.find(params[:id])
    params[:parent_id] = @p_product_sub_cat.parent_id
		@p_product_sub_cat.destroy
    find_p_product_sub_cats
  
    flash[:notice] = "La catégorie à bien été supprimée."
	end
	
	
	protected
	
	def find_p_product_sub_cats    
    params[:parent_id] = nil if !params[:parent_id] or params[:parent_id] == ""
			
		@p_product_sub_cat_parent = PProductSubCat.find(params[:parent_id]) if params[:parent_id]
    
    
    @p_product_sub_cats = PProductSubCat.where(:parent_id => params[:parent_id]).order("position ASC")
		

	
  end
end
