class SpecialPricesController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  ##--- Breadcrum_rails
  add_breadcrumb "Nuevo precio(s) especial", :new_special_price_path, only: :new

  before_action :set_special_price, only: [:show, :edit, :update, :destroy]

  # GET /special_prices/new
  def new
    @special_price = SpecialPrice.new
  end

  # POST /special_prices
  # POST /special_prices.json
  def create
    @special_price = SpecialPrice.new(special_price_params)
    @special_price.user_id = current_user.id
    existence = SpecialPrice.where(product_id: @special_price.product_id, customer_id: @special_price.customer_id).any?
    @variantes = Product.where(parent_id: @special_price.product_id)

    respond_to do |format|
      if existence == true
        @error = true
      else
        @special_price.save
      end
      format.js
    end
  end

  # PATCH/PUT /special_prices/1
  # PATCH/PUT /special_prices/1.json
  def update
    respond_to do |format|
      if params[:special_price][:percent].blank?
        @special_price.percent = nil
      else
        @special_price.price = nil
      end

      @special_price.audit_comment = "Precio especial #{@special_price.product.name} #{@special_price.product.display_attributes} con #{'$' + params[:special_price][:price] if params[:special_price][:price].present?} #{params[:special_price][:percent] + '%' if params[:special_price][:percent].present?} de descuento"
      if @special_price.update(special_price_params)
        format.html { redirect_to @special_price, notice: 'Special price was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :edit }
        format.json { render json: @special_price.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /special_prices/1
  # DELETE /special_prices/1.json
  def destroy
    @special_price.audit_comment = "Precio especial #{@special_price.product.name} #{@special_price.product.display_attributes} eliminado"
    @special_price.destroy
    respond_to do |format|
      format.html { redirect_to special_prices_url, notice: 'Special price was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def get_special_price_by_customer
    @special_prices = SpecialPrice.includes(:product).where(customer_id: params[:customer_id]).order("id ASC")
    respond_to do |format|
      format.js
    end
  end

  def add_special_price_by_barcode
    respond_to do |format|
      @product = Product.find_by(barcode: params[:barcode])
      unless @product.blank?
        @special_price = SpecialPrice.new
        @special_price.product_id = @product.id
        @special_price.customer_id = params[:customer_id]
        @special_price.user_id = current_user.id
        @existence = SpecialPrice.where(product_id: @special_price.product_id, customer_id: @special_price.customer_id).any?
        @special_price.save
      end
      format.js
    end
  end

  private

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def special_price_params
    params.require(:special_price).permit(:product_id, :customer_id, :price, :percent, :status, :user_id)
  end
end
