class SuppliersController < ApplicationController
  ##--- Abilities
  load_and_authorize_resource

  ##--- Breadcrum_rails
  add_breadcrumb I18n.t("breadcrumbs." + controller_name), :suppliers_path
  add_breadcrumb "Nuevo Proveedor", :new_supplier_path, only: :new
  add_breadcrumb "Detalle del Proveedor", :supplier_path, only: :show
  add_breadcrumb "Editar Proveedor", :edit_supplier_path, only: :edit

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

  # GET /suppliers
  # GET /suppliers.json
  def index
    @suppliers = Supplier.vigentes.includes(:billing_information)
  end

  # GET /suppliers/1
  # GET /suppliers/1.json
  def show; end

  # GET /suppliers/new
  def new
    @supplier = Supplier.new
  end

  # GET /suppliers/1/edit
  def edit; end

  # POST /suppliers
  # POST /suppliers.json
  def create
    @supplier = Supplier.new(supplier_params)
    @supplier.audit_comment = "El proveedor " + @supplier.nick_name + " fue registrado."
    respond_to do |format|
      if @supplier.save
        format.html { redirect_to suppliers_url, success: "El proveedor " + @supplier.nick_name + " fue registrado." }
        format.json { render :show, status: :created, location: @supplier }
      else
        format.html { render :new }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /suppliers/1
  # PATCH/PUT /suppliers/1.json
  def update
    respond_to do |format|
      @supplier.audit_comment = "El proveedor " + params[:supplier][:nick_name] + " fue modificado."
      if @supplier.update(supplier_params)
        format.html { redirect_to suppliers_url, success: "El proveedor " + @supplier.nick_name + " fue modificado."  }
        format.json { render :show, status: :ok, location: @supplier }
      else
        format.html { render :edit }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  def update_status
    supplier = Supplier.find(params[:supplier_id])
    if supplier.active?
      supplier.status = 2
    elsif supplier.inactive?
      supplier.status = 1
    end
    respond_to do |format|
      if supplier.save(:validate => false)
        format.html { redirect_to suppliers_url, warning: "El proveedor " + supplier.nick_name + " fue "+ (supplier.active? ? "activado" : "desactivado")+"." }
        format.json { head :no_content }
      else
        format.html { redirect_to suppliers_url }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /suppliers/1
  # DELETE /suppliers/1.json
  def destroy
    respond_to do |format|
      if @supplier.update_attributes(status: 'erased')
        @supplier.contact.update_attributes(status: 'erased') if @supplier.contact.present?
        @supplier.billing_information.update_attributes(status: 'erased') if @supplier.billing_information.present?
        @supplier.audit_comment = "El proveedor " + @supplier.nick_name + " fue dado de baja."
        format.html { redirect_to suppliers_url, warning: "El proveedor " + @supplier.nick_name + " fue dado de baja." }
        format.json { head :no_content }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_supplier
      @supplier = Supplier.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 supplier_params
      params.require(:supplier).permit(:nick_name, :phone, :email, :credit, :credit_limit, :time_limit, :notes, :status, billing_information_attributes:[ :id, :name, :rfc, :address, :num_ext, :num_int, :zipcode, :state_id, :county_id, :city, :suburb], contact_attributes:[ :id, :name, :last_name, :phone, :email ] )
    end
end
