class ExpensesconceptsController < ApplicationController
	##--- Abilities
	load_and_authorize_resource

	##--- Breadcrum_rails
	add_breadcrumb I18n.t("breadcrumbs." + controller_name), :expensesconcepts_path
	add_breadcrumb "Nuevo Concepto de egreso" , :new_expensesconcept_path, only: :new
	add_breadcrumb "Detalle del Concepto de egreso" , :expensesconcept_path, only: :show
	add_breadcrumb "Editar Concepto de egreso" , :edit_expensesconcept_path, only: :edit

	before_action :set_expensesconcept, only: [:show, :edit, :update, :destroy]
	before_action :get_filters, only: [:index, :show, :edit, :new]
	# before_action :get_pointsales, only: [:create, :update]

	# GET /expensesconcepts
	# GET /expensesconcepts.json
	def index
    @concept_purchase_payment = Expensesconcept.find_by(:expense_from_purchase => 1)
    if current_user.usertype == 'A'
      @expensesconcepts = Expensesconcept.vigentes
    else
      @expensesconcepts = Array.new
      Expensesconcept.where("status = 1 and allpoints = 'true'").each do |expense|
        @expensesconcepts << expense
      end

      Pointsale.find(current_user.pointsale_id).expensesconcepts.activos.each do |expense|
        @expensesconcepts << expense
      end
      @expensesconcepts.uniq!
    end
  end


  # GET /expensesconcepts/new
  def new
    @expensesconcept = Expensesconcept.new
  end

  def create
    @expensesconcept = Expensesconcept.new(expensesconcept_params)
		respond_to do |format|
			@expensesconcept.audit_comment = "Concepto de egreso #{@expensesconcept.name} creado."
			if @expensesconcept.save
				# @expensesconcept.pointsales = @pointsales
				format.html { redirect_to expensesconcepts_url, success: 'El concepto ' + @expensesconcept.name + ' fue creado.' }
				format.json { render :show, status: :created, location: @expensesconcept }
			else
				format.html { render :new }
				format.json { render json: @expensesconcept.errors, status: :unprocessable_entity }
			end
		end
	end

	# PATCH/PUT /expensesconcepts/1
	# PATCH/PUT /expensesconcepts/1.json
	def update
		respond_to do |format|
			@expensesconcept.audit_comment = "Concepto de egreso #{@expensesconcept.name} modificado."
			if @expensesconcept.update(expensesconcept_params)

				# @expensesconcept.pointsales = @pointsales
				format.html { redirect_to expensesconcepts_url, success: 'El concepto ' + @expensesconcept.name + ' fue modificado.' }
					format.json { render :show, status: :ok, location: @expensesconcept }
			else
				format.html { render :edit }
				format.json { render json: @expensesconcept.errors, status: :unprocessable_entity }
			end
		end
	end

	# DELETE /expensesconcepts/1
	# DELETE /expensesconcepts/1.json
	def destroy
		#@expensesconcept.destroy
		respond_to do |format|
			@expensesconcept.audit_comment = "Concepto de egreso #{@expensesconcept.name} eliminado."
			if @expensesconcept.update_attributes(:status => 0)
						format.html { redirect_to expensesconcepts_url, warning: "El concepto " + @expensesconcept.name + " fue eliminado." }
						format.json { head :no_content }
			else
				format.html { render :edit }
				format.json { render json: @expensesconcept.errors, status: :unprocessable_entity }
			end
		end
	end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def expensesconcept_params
      params.require(:expensesconcept).permit(:name, :description, :allpoints, :status, :pointsale_ids => [])
    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_pointsales
        unless params[:expensesconcept][:pointsale_ids].nil? && params[:expensesconcept][:pointsale_ids].empty?
            ## Issue arreglo primer elemento en blanco ...
            params[:expensesconcept][:pointsale_ids] = params[:expensesconcept][:pointsale_ids].delete_if{ |x| x.empty? }
            @pointsales = params[:expensesconcept][:pointsale_ids].map do |k|
                Pointsale.find(k)
            end
        else
            @pointsales = []
        end
    end
end
