class WarehousesController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :warehouses_path
  add_breadcrumb "Nuevo Almacén " , :new_warehouse_path, only: :new
  add_breadcrumb "Editar Almacén"  , :edit_warehouse_path, only: :edit

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

  # GET /warehouses
  # GET /warehouses.json
  def index
    @warehouses = Warehouse.all
  end

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

  # GET /warehouses/new
  def new
    @warehouse = Warehouse.new
  end

  # GET /warehouses/1/edit
  def edit
  end

  # POST /warehouses
  # POST /warehouses.json
  def create
    @warehouse = Warehouse.new(warehouse_params)
    @warehouse.prefix.upcase!
    respond_to do |format|
      message = "Almacén #{@warehouse.name} creado."
      @warehouse.audit_comment = message
      if @warehouse.save
        format.html { redirect_to warehouses_path, success: message }
        format.json { render :show, status: :created, location: @warehouse }
      else
        format.html { render :new }
        format.json { render json: @warehouse.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /warehouses/1
  # PATCH/PUT /warehouses/1.json
  def update
    respond_to do |format|
      message = "Almacén #{@warehouse.name} modificado."
      @warehouse.audit_comment = message
      if @warehouse.update(warehouse_params)
        format.html { redirect_to warehouses_path, success: message }
        format.json { render :show, status: :ok, location: @warehouse }
      else
        format.html { render :edit }
        format.json { render json: @warehouse.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /warehouses/1
  # DELETE /warehouses/1.json
  def destroy
    message = "Almacén #{@warehouse.name} eliminado."
    @warehouse.audit_comment = message
    @warehouse.destroy
    respond_to do |format|
      format.html { redirect_to warehouses_url, notice: message }
      format.json { head :no_content }
    end
  end

  def update_status
    warehouse = Warehouse.find(params[:warehouse_id])
    if warehouse.active?
      warehouse.status = "inactive"
    elsif warehouse.inactive?
      warehouse.status = "active"
    end
    respond_to do |format|
      message = "El almacen " + warehouse.name + " fue "+ (warehouse.active? ? "activado" : "desactivado")+"."
      warehouse.audit_comment = message
      if warehouse.save(:validate => false)
        format.html { redirect_to warehouses_path, warning: message }
        format.json { head :no_content }
      else
        format.html { redirect_to warehouses_path }
        format.json { render json: warehouse.errors, status: :unprocessable_entity }
      end
    end
  end

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

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def warehouse_params
      params.require(:warehouse).permit(:name, :status, :prefix)
    end
end
