class SellersController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  before_action :set_seller, only: [:show, :edit, :update, :destroy]
  before_action :get_info, only: [:new, :edit, :create, :update]
  before_action :get_filters, only: [:index, :show, :edit, :new]

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :sellers_path
  add_breadcrumb "Nuevo vendedor " , :new_seller_path, only: :new
  add_breadcrumb "Detalle del vendedor "  , :seller_path, only: :show
  add_breadcrumb "Editar vendedor "  , :edit_seller_path, only: :edit

  # GET /sellers
  # GET /sellers.json
  def index
    @sellers = (current_user.usertype == "A" ? Seller.vigentes.includes(:pointsale) : Seller.where(:pointsale_id => current_user.pointsale_id).vigentes)
  end

  # GET /sellers/1
  # GET /sellers/1.json
  def show
  end

  # GET /sellers/new
  def new
    @seller = Seller.new
    if current_user.usertype != "A"
      @pointsale_id = current_user.pointsale_id
    end
  end

  # GET /sellers/1/edit
  def edit
    @pointsale_id = @seller.pointsale_id
  end

  # POST /sellers
  # POST /sellers.json
  def create
    @seller = Seller.new(seller_params)
    if current_user.usertype != "A"
      @seller.skip_pointsale = true
      @seller.pointsale_id = current_user.pointsale_id
    end
    respond_to do |format|
      message = "El vendedor " + @seller.full_name + " fue registrado."
      @seller.audit_comment = message
      if @seller.save
        format.html { redirect_to sellers_path, success: message }
        format.json { render :show, status: :created, location: @seller }
      else
        format.html { render :new }
        format.json { render json: @seller.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /sellers/1
  # PATCH/PUT /sellers/1.json
  def update
    respond_to do |format|
      message = "El vendedor " + @seller.full_name + " fue modificado."
      @seller.audit_comment = message
      if @seller.update(seller_params)
        format.html { redirect_to sellers_path, success: message }
        format.json { render :show, status: :ok, location: @seller }
      else
        format.html { render :edit }
        format.json { render json: @seller.errors, status: :unprocessable_entity }
      end
    end
  end

  def update_status
    seller = Seller.find(params[:seller_id])
    if seller.active?
      seller.status = 2
    elsif seller.inactive?
      seller.status = 1
    end
    respond_to do |format|
      message = "El vendedor " + seller.full_name + " " + seller.last_name + " fue "+ (seller.active? ? "activado" : "desactivado")+"."
      seller.audit_comment = message
      if seller.save(:validate => false)
        format.html { redirect_to sellers_url, warning: message }
        format.json { head :no_content }
      else
        format.html { redirect_to sellers_url }
        format.json { render json: @seller.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /sellers/1
  # DELETE /sellers/1.json
  def destroy
    #@expensesconcept.destroy
    respond_to do |format|
      message = "El vendedor " + @seller.full_name + " fue eliminado."
      @seller.audit_comment = message
      if @seller.update_attributes(:status => 0)
        format.html { redirect_to sellers_path, warning: message }
        format.json { head :no_content }
      else
        format.html { render :edit }
        format.json { render json: @seller.errors, status: :unprocessable_entity }
      end
    end
  end



  private
    # Use callbacks to share common setup or constraints between actions.
    def set_seller
      @seller = Seller.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def seller_params
      params.require(:seller).permit(:pointsale_id, :name, :last_name)
    end

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

    def get_info
      @users = (current_user.usertype == "A" ? User.active : User.active.where(:pointsale_id => current_user.pointsale_id))
      @pointsales = Pointsale.activos
    end
end
