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

class Admin::PProductsController < ApplicationController
  layout "admin"
  before_action :auth_admin
  
  before_action :admin_space
  
  def import
    if false
      require "csv"
      csv_text = File.read("#{Rails.root}/import_csv/produits.csv", :encoding => 'UTF-8')
      @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
      
      PProduct.destroy_all
      
      @csv.each do |row|
        
        p_product = PProduct.new(:enabled => true, :p_customer_cats => PCustomerCat.all)
        p_product.code = row["CodeArticle"]
        p_product.sub_code = row["CodeArticle2"]
        p_product.name = row["Intitule1"]
        p_product.ct_purchase_price_ht = row["PxAchatBrut2019"].to_s.sub(",",".").to_f
        
        if p_product_cat = PProductCat.where(:name => row["Famille_lib"]).first
        
        else
          p_product_cat = PProductCat.create(:name => row["Famille_lib"])
        end
        p_product.p_product_cat_id = p_product_cat.id
       
        p_product_ref = PProductRef.new
        
        p_product_ref.ct_price_ht = row["PxVenteHtEuro2019"].to_s.sub(",",".").to_f
        #p_product_ref.uv = 
        p_product_ref.weight = row["PoidsUnitaire"].to_s.sub(",",".").to_f if row["PoidsUnitaire"].to_s != "0"
        
        
        ss_famille_1 = PProductSubCat.where(:parent_id => nil, :name => row["Sous-famille_lib"]).first
        
        if ss_famille_1
          if ss_famille_1
            p_product.p_product_sub_cats << ss_famille_1
          
            if row["ss-famille_n2_lib"].to_s != ""
              ss_famille_2 = PProductSubCat.where(:parent_id => ss_famille_1.id, :name => row["ss-famille_n2_lib"]).first
          
              if ss_famille_2
                p_product.p_product_sub_cats << ss_famille_2
              
                if row["ss-famille_n3_lib"].to_s != ""
                  ss_famille_3 = PProductSubCat.where(:parent_id => ss_famille_2.id, :name => row["ss-famille_n3_lib"]).first
          
                  p_product.p_product_sub_cats << ss_famille_3 if ss_famille_3
                end
                
              
              end
            end
          end
        end
        
        
        p_product.p_product_refs << p_product_ref
        
        puts p_product.save
        
        puts p_product.errors.messages.to_s
        
        
        
      end
      
      render :inline  => "ok"
      
    end 
  end
  
  
  def admin_space
    @admin_space = "stocks"
  end
  

  def index
    @p_products = PProduct.joins(:p_product_cat).order("p_product_cats.name ASC, p_products.name ASC")
    
    if params[:archived]
      @p_products = @p_products.where(:enabled => false)
    else
      @p_products = @p_products.where(:enabled => true)
    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

  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]).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
