class PosConfigsController < ApplicationController

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :pos_configs_path

  before_action :set_pos_config, only: [:update, :destroy]

  # GET /pos_configs
  # GET /pos_configs.json
  def index
    @pos_config = PosConfig.first.blank? ? PosConfig.new : PosConfig.first
  end

  # POST /pos_configs
  # POST /pos_configs.json
  def create
    @pos_config = PosConfig.new(pos_config_params)
    @pos_config.skip_haggle_percent = true if params[:pos_config][:enable_haggle] == "false"
    respond_to do |format|
      if @pos_config.save
        format.html { redirect_to root_path, notice: 'Configuración de parámetros generales guardada correctamente.' }
        format.json { render :show, status: :created, location: @pos_config }
      else
        format.html { render action: "index" }
        format.json { render json: @pos_config.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pos_configs/1
  # PATCH/PUT /pos_configs/1.json
  def update
    @pos_config.skip_haggle_percent = true if params[:pos_config][:enable_haggle] == "false"
    respond_to do |format|
      @pos_config.audit_comment = "Se actualizaron los parámetros generales"
      if @pos_config.update(pos_config_params)
        format.html { redirect_to root_path, notice: 'Configuración de parámetros generales guardada correctamente.' }
        format.json { render :show, status: :ok, location: @pos_config }
      else
        format.html { render action: "index" }
        format.json { render json: @pos_config.errors, status: :unprocessable_entity }
      end
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_pos_config
    @pos_config = PosConfig.first
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def pos_config_params
    params[:pos_config]
    params.require(:pos_config).permit(:cancel_partial_payment, :refund_sale, :days_cancel_sale, :days_cancel_purchase, :tax_percent, :time_zone, :gain_margin, :ticket_description, :reserve_sale_percent, :days_cancel_reserved, :commission_percent, :ticket_footer, :ticket_img, :ticket_img_cache, :haggle_in_sale_percent, :enable_haggle)
  end
end
