module Base
class ResponsaveisController < ApplicationController
	include ControllerConcern
	before_action :authenticate_usuario!
	before_action :autoriza_usuario!
	before_action :set_responsavel, only: [:edit, :update, :destroy]
	before_action :set_pessoa, only: [:new, :create]

	# GET /base/responsaveis/new
	def new
		@responsavel = @pessoa.responsaveis.new
	end

	# GET /base/responsaveis/1/edit
	def edit
	end

	# POST /base/responsaveis
	def create
		@responsavel = @pessoa.responsaveis.new(responsavel_params)
		if @responsavel.save
			if params[:pessoa_do_projeto].present? && params[:form_de_pessoa_do_projeto].present?
				redirect_to edit_licitacao_pessoa_do_projeto_path(params[:pessoa_do_projeto]), notice: 'Responsável foi criado(a) com sucesso.'
			elsif params[:pessoa_do_projeto].present?
				redirect_to licitacao_pessoa_do_projeto_path(params[:pessoa_do_projeto]), notice: 'Responsável foi criado(a) com sucesso.'
			else
				redirect_to base_pessoa_path(@pessoa), notice: 'Responsável foi criado(a) com sucesso.'
			end
		else
			render :new
		end
	end

	# PATCH/PUT /base/responsaveis/1
	def update
		if @responsavel.update( responsavel_params )
			redirect_to base_pessoa_path(@responsavel.pessoa), notice: 'Responsável foi atualizado(a) com sucesso.'
		else
			render :edit
		end
	end

	# DELETE /base/responsaveis/1
	def destroy
		mensagem = apaga_e_retorna_mensagem(@responsavel)
		redirect_to base_pessoa_path(@responsavel.pessoa) + "?tab=responsavel", mensagem
	end

	private
	def set_responsavel
		@responsavel = Responsavel.find( params[:id] )
	end

	def set_pessoa
		@pessoa = Base::Pessoa.find(params[:pessoa_id])
	end

	# Permite apenas os parâmetros específicos
	def responsavel_params
		params.require(:base_responsavel).permit(:nome, :cpf, :rg, :pessoa_id)
	end
end
end
