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

class Admin::DashboardsController < ApplicationController
  before_action :auth_admin

  layout "admin"

  before_action {qi_menu(:dashboard_commercial)}
  
  before_action :admin_space

  def admin_space
    @admin_space = "statistiques"
  end
  
  
  def commercial

    

    @current_p_commercial_id = PCommercial.where(:id => current_admin.p_commercial.id) if current_admin.p_commercial

    if @current_p_commercial_id.present? && @current_p_commercial_id != nil
      @current_p_commercial_id.each do |c|
        params[:p_commercial_ids] = c.id
      params[:start] = Date.parse('01-05-2020').strftime('%d/%m/%Y') if !params[:start]
      params[:stop] = Date.parse('30/04/2021').strftime('%d/%m/%Y') if !params[:stop]
      end
    else
      params[:p_commercial_ids] = params[:p_commercial_ids] || []
      params[:p_commercial_ids] << params[:p_commercial_id] if params[:p_commercial_id].to_s != ""
    end

    params[:p_product_ref_ids] = params[:p_product_ref_ids] || []
    params[:p_product_ref_ids] << params[:p_product_ref_id] if params[:p_product_ref_id].to_s != ""

    params[:p_customer_ids] = params[:p_customer_ids] || []
    params[:p_customer_ids] << params[:p_customer_id] if params[:p_customer_id].to_s != ""



    params[:p_product_cat_ids] = params[:p_product_cat_ids] || []
    params[:p_product_cat_ids] << params[:p_product_cat_id] if params[:p_product_cat_id].to_s != ""


    params[:s_brand_ids] = params[:s_brand_ids] || []
    params[:s_brand_ids] << params[:s_brand_id] if params[:s_brand_id].to_s != ""



    date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i

    params[:start] = Date.today.beginning_of_year.strftime('%d/%m/%Y') if !params[:start]

    # if @current_p_commercial_id.present? && @current_p_commercial_id != nil
    #   params[:start] = Date.parse('01-05-2020').strftime('%d/%m/%Y') if !params[:start]
    # end



    if params[:start] and params[:start] =~ date_regex
      #fsfds = sdfsfd
      @start = Date.parse(params[:start]).beginning_of_day
      params[:start]= @start.strftime('%d/%m/%Y')
    else
      @start = nil
    end

    if true
      params[:stop] = Date.today.end_of_year.strftime('%d/%m/%Y') if !params[:stop]
      # params[:stop] = Date.parse('30/04/2021').strftime('%d/%m/%Y') if !params[:stop]


      if params[:stop].to_s != "" # and params[:stop] =~ date_regex
        @stop = Date.parse(params[:stop]).end_of_day

        params[:stop]= @stop.strftime('%d/%m/%Y')


      else
         @stop = nil
      end

    end

    @past_start = (@start - 1.year).beginning_of_year.beginning_of_day

    @past_stop = @past_start.end_of_year.end_of_day

    @price_documents = PriceDocument.where(:price_document_type_id => [4,5])


    @periode_price_documents = @price_documents.where("date >= ? and date <= ?", @start, @stop)
    @past_periode_price_documents = @price_documents.where("date >= ? and date <= ?", @past_start, @past_stop)


    @general_periode_price_documents = @periode_price_documents
    @past_general_periode_price_documents = @past_periode_price_documents


    @periode_lines = StatLine.where("date >= ? and date <= ?", @start, @stop)
    @past_periode_lines = StatLine.where("date >= ? and date <= ?", @past_start, @past_stop)




    if params[:p_customer_ids].size > 0
      @periode_price_documents = @periode_price_documents.where(:p_customer_id => params[:p_customer_ids])
      @past_periode_price_documents = @past_periode_price_documents.where(:p_customer_id => params[:p_customer_ids])

      @periode_lines = @periode_lines.where(:p_customer_id => params[:p_customer_ids])
      @past_periode_lines = @past_periode_lines.where(:p_customer_id => params[:p_customer_ids])
    end


    if params[:p_product_ref_ids].size > 0
      @periode_lines = @periode_lines.where(:p_product_ref_id => params[:p_product_ref_ids])
      @past_periode_lines = @past_periode_lines.where(:p_product_ref_id => params[:p_product_ref_ids])
    end





    if params[:p_product_cat_ids].size > 0
      @periode_lines = @periode_lines.where(:p_product_cat_id => params[:p_product_cat_ids])
      @past_periode_lines = @past_periode_lines.where(:p_product_cat_id => params[:p_product_cat_ids])



    end


    if params[:s_brand_ids].size > 0
      @periode_lines = @periode_lines.where(:s_brand_id => params[:s_brand_ids])
      @past_periode_lines = @past_periode_lines.where(:s_brand_id => params[:s_brand_ids])



    end

    if current_admin.p_commercial

      @gen_periode_lines = @periode_lines
      @gen_past_periode_lines = @past_periode_lines

      @gen_periode_price_documents = @periode_price_documents
      @gen_past_periode_price_documents = @past_periode_price_documents
    end


    if params[:p_commercial_ids].size > 0
      @periode_lines = @periode_lines.where(:p_commercial_id => params[:p_commercial_ids])
      @past_periode_lines = @past_periode_lines.where(:p_commercial_id => params[:p_commercial_ids])

      @periode_price_documents = @periode_price_documents.where(:p_commercial_id => params[:p_commercial_ids])
      @past_periode_price_documents = @past_periode_price_documents.where(:p_commercial_id => params[:p_commercial_ids])


    end






  end

  def adv

  end

  # def objectives
  #   redirect_to :controller => 'p_commercial_objectives', :action => 'index'
  # end


end