module Contabilidade
class FundosDeInvestimentoController < ApplicationController
	include ControllerConcern
	before_action :authenticate_usuario!
	before_action :autoriza_usuario!, except: [:new, :edit, :create, :update, :destroy]
	before_action :set_fundo_de_investimento, only: [:show, :edit, :update, :destroy]
	before_action :set_conta_bancaria, only: [:new, :create]
	before_action :disponibiliza_dependencias, only: [:create, :new, :update, :edit]

	# GET /contabilidade/fundos_de_investimento/new
	def new
		return if bloqueia_usuario_com_base_em 'edit', 'base/contas_bancarias_controller'
		@fundo_de_investimento = @conta_bancaria.fundos_de_investimento.new

	end

	# GET /contabilidade/fundos_de_investimento/1/edit
	def edit
		return if bloqueia_usuario_com_base_em 'edit', 'base/contas_bancarias_controller'
	end

	# POST /contabilidade/fundos_de_investimento
	def create
		return if bloqueia_usuario_com_base_em 'edit', 'base/contas_bancarias_controller'
		@fundo_de_investimento = @conta_bancaria.fundos_de_investimento.new(fundo_de_investimento_params)

		if @fundo_de_investimento.save
			redirect_to @fundo_de_investimento.conta_bancaria, notice: 'Fundo de investimento foi criado(a) com sucesso.'
		else
			render :new
		end
	end

	# PATCH/PUT /contabilidade/fundos_de_investimento/1
	def update
		return if bloqueia_usuario_com_base_em 'edit', 'base/contas_bancarias_controller'
		if @fundo_de_investimento.update( fundo_de_investimento_params )
			redirect_to @fundo_de_investimento.conta_bancaria, notice: 'Fundo de investimento foi atualizado(a) com sucesso.'
		else
			render :edit
		end
	end

	# DELETE /contabilidade/fundos_de_investimento/1
	def destroy
		return if bloqueia_usuario_com_base_em 'edit', 'base/contas_bancarias_controller'
		mensagem = apaga_e_retorna_mensagem(@fundo_de_investimento)
		redirect_to @fundo_de_investimento.conta_bancaria, mensagem
	end

	private
	def set_fundo_de_investimento
		@fundo_de_investimento = FundoDeInvestimento.find( params[:id] )
	end

	def set_conta_bancaria
		@conta_bancaria = Base::ContaBancaria.find( params[:conta_bancaria] )
	end

	def disponibiliza_dependencias
		@percentuais_da_alocacao_de_recurso = contexto_atual.percentuais_da_alocacao_de_recurso
		@pessoas =  Base::Pessoa.where(fundo_de_investimento: true)
	end

	# Permite apenas os parâmetros específicos
	def fundo_de_investimento_params
		params.require(:contabilidade_fundo_de_investimento).permit(:conta_bancaria_id, :percentual_da_alocacao_de_recurso_id,
			:pessoa_id, :nome_do_fundo)
	end
end
end
