class UsersController < ApplicationController
  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :users_path
  add_breadcrumb "Cambio de contraseña", :pwdchange_path, only: :update_password
  add_breadcrumb "Nuevo Usuario", :new_user_path, only: :new
  add_breadcrumb "Editar usuario", :edit_user_path, only: :edit

  before_action :set_user, only: [:edit, :update]
  before_action :get_filters, only: [:index, :show, :edit, :new]

  def index
    @users = current_user.usertype == 'A' ? User.includes(:pointsale, :warehouse).where('status > 0').order("id desc") : User.includes(:pointsale, :warehouse).where('status > 0 and pointsale_id = ?', current_user.pointsale_id).order("id desc")
  end

  def new
    @user = User.new
  end

  def edit; end

  def update
    respond_to do |format|
      @user.skip_validations_when_edit = true
      message = "Usuario #{@user.userid} ha sido modificado."
      @user.audit_comment = message
      if @user.update(user_params)
        format.html { redirect_to users_path, success: message }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def create
    @user = User.new(user_params)
    respond_to do |format|
      message =
        if @user.usertype == 'A'
          "usuario #{@user.userid} con perfil ADMINISTRADOR creado."
        else
          "Usuario #{@user.userid} creado y asignado al " + (@user.pointsale.present? ? "punto de venta #{@user.pointsale.name}" : "almacén #{@user.warehouse.name}")
        end
      @user.audit_comment = message
      if @user.save
        format.html { redirect_to users_path, success: message }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def edit_password
    @user = current_user
  end

  def update_password
    @user = User.find(current_user.id)
    respond_to do |format|
      message = "La contraseña de " + @user.full_name + " fue modificada."
      @user.audit_comment = message
      if @user.update_with_password(user_params)
        # Sign in the user by passing validation in case their password changed
        sign_in @user, bypass: true
        format.html { redirect_to root_path, success: message }
        # format.json { render :show, status: :created, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update_status
    user = User.find(params[:user_id])
    user.status =
      if user.active?
        "inactive"
      elsif user.inactive?
        "active"
      end
    respond_to do |format|
      message = "El usuario " + user.userid + " fue " + (user.active? ? "activado" : "desactivado") + "."
      user.audit_comment = message
      if user.save(validate: false)
        format.html { redirect_to users_path, warning: message }
        format.json { head :no_content }
      else
        format.html { redirect_to users_path }
        format.json { render json: user.errors, status: :unprocessable_entity }
      end
    end
  end

  private

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

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

  def user_params
    params.require(:user).permit(:current_password, :password, :password_confirmation, :pointsale_id, :warehouse_id, :usertype, :userid, :first_name, :last_name, :email)
  end
end
