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

class Admin::PProductsController < ApplicationController
  layout "admin"
  before_action :auth_admin
  
  before_action :admin_space
  
  
  def admin_space
    @admin_space = "stocks"
  end
  

  def index
    @title =" Produits"
    @p_products = PProduct.includes(:p_product_cat)
    
    params[:search] = params[:search] || {}
    
    
    params[:search][:enabled] = "Oui" if !params[:search][:enabled]
    
    if params[:search][:enabled].to_s == "Oui"
      @p_products = @p_products.where(:enabled => true)
    elsif params[:search][:enabled].to_s == "Non"
      @p_products = @p_products.where(:enabled => false)
    end
    
    if params[:search][:code].to_s != ""
      @p_products = @p_products.joins(:p_product_refs).where("p_product_refs.ref LIKE ?","#{params[:search][:code]}%")
    end
  
    if params[:search][:name].to_s != ""
      @p_products = @p_products.where("p_products.name LIKE ? or p_products.description LIKE ?","%#{params[:search][:name]}%", "%#{params[:search][:name]}%")
    end
    
    if params[:search][:p_product_cat_id].to_s != ""
      if params[:search][:p_product_cat_id].to_s == "null"
        @p_products = @p_products.where(:p_product_cat_id => nil)
      else
        @p_products = @p_products.where(:p_product_cat_id => params[:search][:p_product_cat_id])
      end
    end
    
    
    if params[:search][:s_brand_id].to_s != ""
      @p_products = @p_products.where(:s_brand_id => params[:search][:s_brand_id])
    end
    
    @p_products = sort_by_sorting(@p_products, "id DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 50
    		per_page = params[:search][:per_page]
    		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    		@p_products = @p_products.page(page).per(per_page)
      
      }
    end
    
   
  end

  def show
    @p_product = PProduct.find(params[:id])

  end

  def new
    
    
		@p_product = PProduct.new #(:p_customer_cat_ids => [3])
    
    # @p_product.p_product_refs << PProductRef.new
    @p_product_refs = @p_product.p_product_refs.build
    @p_product_ref_specs = @p_product_refs.p_product_ref_specs.build
    @p_spec_type = @p_product_ref_specs.build_p_spec_type
    @p_spec_value = @p_product_ref_specs.build_p_spec_value
  end

 

  def edit
  
    
    @p_product = PProduct.find(params[:id])
  end

  def create
    
    if params[:p_product][:create_matiere] == "1"
      
      params[:p_product].delete(:p_product_brut_products_attributes)

    end
    @p_product = PProduct.new(params.require(:p_product).permit!)


    if @p_product.save
      @p_products = PProduct.joins(:p_product_cat).order("p_product_cats.name ASC, p_products.name ASC").all
      
      
      redirect_to admin_p_products_path()
    else
      render action: "new"
 
    end

  end


  def update
    @p_product = PProduct.find(params[:id])


    if @p_product.update_attributes(params.require(:p_product).permit!)
      
      @p_products = PProduct.joins(:p_product_cat).order("p_product_cats.name ASC, p_products.name ASC").all
      
      redirect_to admin_p_products_path()
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_product = PProduct.find(params[:id])
    @p_product.destroy
    
  end
  
  def autocomplete
    @p_products = PProduct.for_search(params[:search]).distinct.limit(50)
    
    
    
    
    respond_to do |format|
        format.json { render json: @p_products }
    end
  end
  
  def autocomplete_apercu
    @p_product = PProduct.find(params[:id])
    
  end
  
  
end
