class CustomersController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :customers_path, only: [:index, :new, :show, :edit]
  add_breadcrumb "Clientes deudores", :debtors_path, only: [:debtors, :customer_sales]
  add_breadcrumb "Detalle de ventas de clientes deudores", :customer_sales_path, only: :customer_sales
  add_breadcrumb "Nuevo " + I18n.t("breadcrumbs." + controller_name).singularize, :new_customer_path, only: :new
  add_breadcrumb "Detalle del " + I18n.t("breadcrumbs." + controller_name).singularize, :customer_path, only: :show
  add_breadcrumb "Editar " + I18n.t("breadcrumbs." + controller_name).singularize, :edit_customer_path, only: :edit

  before_action :set_customer, only: [:show, :edit, :update, :destroy]
  before_action :get_filters, only: [:index, :show, :edit, :new, :debtors, :customer_sales]

  # GET /customers
  # GET /customers.json
  def index
    @customers = Customer.includes(:billing_information).vigentes.where.not(id: 1)
  end

  def debtors
    @customers = Customer.where("customers.status != 0").includes(:sales, :credits).order('credits.id DESC')
  end

  def customer_sales
    @custom = Customer.find(params[:customer_id])
    @sales = Sale.where(customer_id: @custom.id, saletype: 0).order('created_at desc')
    @credit = Credit.where(customer_id: @custom.id)
  end

  # apartados por cliente
  def reserved_sales_by_customer
    respond_to do |format|
      @customer = Customer.find(params[:customer_id])
      @sales = Sale.where(customer_id: @customer.id, saletype: 2, status: 3)
      format.js
    end
  end

  # GET /customers/1
  # GET /customers/1.json
  def show
    @sales = Sale.where(customer_id: @customer.id, saletype: 0).order('created_at desc')
    @credit = Credit.where(customer_id: @customer.id)
  end

  # GET /customers/new
  def new
    @customer = Customer.new
    @customer.credit = false
  end

  # GET /customers/1/edit
  def edit; end

  # POST /customers
  # POST /customers.json
  def create
    @customer = Customer.new(customer_params)
    @customer.audit_comment = "El cliente " + @customer.nick_name + " fue registrado."
    respond_to do |format|
      if @customer.save
        format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue registrado." }
        format.json { render :show, status: :created, location: @customer }
      else
        format.html { render :new }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
      format.js
    end
  end

  # PATCH/PUT /customers/1
  # PATCH/PUT /customers/1.json
  def update
    respond_to do |format|
      @customer.audit_comment = "El cliente " + params[:customer][:nick_name] + " fue registrado."
      if @customer.update(customer_params)
        format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue modificado." }
        format.json { render :show, status: :ok, location: @customer }
      else
        format.html { render :edit }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end

  def update_status
    customer = Customer.find(params[:customer_id])
    if customer.active?
      customer.status = 2
    elsif customer.inactive?
      customer.status = 1
    end
    respond_to do |format|
      if customer.save(validate: false)
        format.html { redirect_to customers_url, warning: "El cliente " + customer.nick_name + " fue " + (customer.active? ? "activado" : "desactivado") + "." }
        format.json { head :no_content }
      else
        format.html { redirect_to customers_url }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /customers/1
  # DELETE /customers/1.json
  def destroy
    respond_to do |format|
      if @customer.update_attributes(status: :erased)
        @customer.audit_comment = "El cliente " + @customer.nick_name + " fue dado de baja."
        format.html { redirect_to customers_url, warning: "El cliente " + @customer.nick_name + " fue dado de baja." }
        format.json { head :no_content }
      end
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_customer
    @customer = Customer.find(params[:id])
  end

  def get_filters
    @current_page = params[:current_page].blank? ? 1 : params[:current_page]
    @filter = params[:filter]
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def customer_params
    params.require(:customer).permit(:nick_name, :phone, :email, :credit, :credit_limit, :time_limit, :notes, :status, billing_information_attributes: [:id, :name, :rfc, :address, :num_ext, :num_int, :zipcode, :state_id, :county_id, :city, :suburb], contact_attributes: [:id, :name, :last_name, :phone, :email])
  end
end
