class Administrativo::Relatorios::PagamentosController < ApplicationController
	include ControllerConcern

	before_action :authenticate_usuario!
	before_action :autoriza_usuario!, except: [:imprimir]
	before_action :disponibiliza_dependencias

	def imprimir
		bloqueia_usuario_com_base_em :read, "contabilidade/pagamentos"
		relatorio = params[:relatorio]
		@nao_imprimir_na_funcao = false

		if respond_to? relatorio.to_s, :private
			loa = CombinePDF.new
			loa << CombinePDF.parse(send(relatorio)) 
			if !@nao_imprimir_na_funcao
				send_data loa.to_pdf, filename: "#{relatorio}.pdf", type: "application/pdf", disposition: 'inline'
			end
		else
			redirect_to :back, alert: 'Demonstrativo selecionado não existe'
		end
	end

	private
	def nota_de_pagamento
		@nao_imprimir_na_funcao = true
		@com_capa = params[:com_capa].to_boolean
		@pagamento = Contabilidade::Pagamento.find_by(id: params[:pagamento_id])
		@unidade_orcamentaria = @pagamento.empenho.unidade_orcamentaria

		arquivo = CombinePDF.new

		if @com_capa
			arquivo << CombinePDF.parse(capa_relacao_de_processos_pagos)
		end

		arquivo << CombinePDF.parse(arquivo_nota_de_pagamento)

		send_data arquivo.to_pdf, filename: "nota_de_pagamento.pdf", type: "application/pdf", disposition: 'inline'
	end

	def capa_relacao_de_processos_pagos
		render_to_string pdf: "restos_a_pagar",
			template: "contabilidade/relatorios_da_contabilidade/capa_relacao_de_processos_pagos.pdf.slim",
			orientation: 'Portrait',
			disable_smart_shrinking: true,

			dpi: '96',
			footer: {
				html: {
					template: 'layouts/_rodape_pdf.html.slim'
				}
			},
			margin: @configuracoes.margens_customizadas(top: 5)
	end

	def arquivo_nota_de_pagamento

		if @configuracoes.customizacao.exibir_nome_orgao?
			titulo1 = @pagamento&.unidade_orcamentaria_atual.nome
		else
			titulo1 = ''
		end

		render_to_string pdf: "nota_de_pagamento",
			template: "administrativo/relatorios/pagamentos/nota_de_pagamento.pdf.slim",
			orientation: 'Portrait',
			disable_smart_shrinking: true,
			
			dpi: '96',
			header: {
				html: {
					template: 'layouts/_cabecalho_pdf.html.slim',
					locals: {
						titulo1: titulo1,
						titulo2: "Pagamento " + @pagamento.numero
					}
				},
				spacing: 0
			},
			footer: {
				html: {
					template: 'layouts/_rodape_pdf.html.slim'
				}
			},
			margin: @configuracoes.margens_customizadas
	end

	def nota_de_estorno_de_pagamento
		@pagamento = Contabilidade::Pagamento.find_by(id: params[:pagamento_id])
		@unidade_orcamentaria = @pagamento.empenho.unidade_orcamentaria

		if @configuracoes.customizacao.exibir_nome_orgao?
			titulo1 = @pagamento.empenho.unidade_orcamentaria.nome
		else
			titulo1 = ''
		end

		render_to_string pdf: "nota_de_estorno_de_pagamento",
			template: "administrativo/relatorios/pagamentos/nota_de_estorno_de_pagamento.pdf.slim",
			orientation: 'Portrait',
			disable_smart_shrinking: true,
			
			dpi: '96',
			header: {
				html: {
					template: 'layouts/_cabecalho_pdf.html.slim',
					locals: {
						titulo1: titulo1,
						titulo2: "Relatório de Estorno de Pagamento",
						titulo3: "Estorno de Pagamento " + @pagamento.estorno_de_pagamento.numero_do_estorno
					}
				},
				spacing: 0
			},
			footer: {
				html: {
					template: 'layouts/_rodape_pdf.html.slim'
				}
			},
			margin: @configuracoes.margens_customizadas
	end

	def disponibiliza_dependencias
		@configuracoes = Configuracao.last
	end
end
