module ComponentesHelper
	def voltar_agrupado(opcoes, params={})
		content_tag(:div, class: "btn-group", role: "group") do
			button = content_tag(:button, class: %w[btn btn-default btn-sm dropdown-toggle], type: "button", data: {toggle: "dropdown"}, aria: {haspopup: "true", expanded: "false"} ) do
				content_tag(:i, nil, class: "icone-voltar") + " Voltar " + content_tag(:span, nil, class: "caret")
			end
			list = content_tag(:ul, class: "dropdown-menu") do
				opcoes.map { |opcao| content_tag(:li, link_to(opcao[:texto], opcao[:path]))}.join.html_safe
			end
			button + list
		end
	end

	def badge numero
		options = {class: 'badge'}
		content_tag(:span, numero, options) if numero > 0
	end

	def badge_porcentagem numero
		options = {class: 'badge'}
		string = numero.to_s + '%'
		content_tag(:span, string, options)
	end

	def botao(path, params={})

		classe_padrao = 'btn btn-default btn-sm'

		params[:texto] ||= ''
		params[:class_icone] ||= ''
		params[:class_texto] ||= ''
		params[:params] ||= {}

		params[:params][:class] = classe_padrao if params[:params][:class].nil?
		params[:id] = params[:params][:id] if params[:params][:id].present?

		link_to path, params[:params] do
			content_tag(:span, '', id: params[:params][:id], class: params[:class_icone]) <<
			content_tag(:span, class: params[:class_texto]) do
				params[:texto]
			end
		end
	end

	def botao_com_permissao(path, permissao, params={})
		botao( path, params ) if tem_permissao?( permissao )
	end

	private
	def tem_permissao?(permissao)
		esta_autorizado? permissao[:acao], permissao[:controle]
	end

	# Renders a Bootstrap label.
	#
	# @param text [String] the text rendered in the label
	# @param style [Symbol, Symbol] the style used to render the label
	# @param options [Hash] a hash of options. Passed straight through to the
	# underlying "span" tag.
	#
	def bs_label (text, style = "default", options = {})
		options = options.dup

		cls = options[:class]
		cls ||= "label"
		cls << " label-" + style

		options[:class] = cls

		content_tag :span, text, options
	end

	def label_negativo_positivo (obj)
		text = obj ? "Positivo" : "Negativo"
		style = obj ? "success" : "danger"
		bs_label(text, style)
	end

	def label_sim_nao(obj)
		text = obj ? "Sim" : "Não"
		style = obj ? "success" : "danger"
		bs_label(text, style)
	end

	def label_pendente_pagar(obj)
		text = obj ? "Pago" : "A Pagar"
		style = obj ? "sucess" : "danger"
		bs_label(text, style)
	end

	def label_pendente(obj)
		if obj
			text = "Pendente"
			style = "warning"
			bs_label(text, style)
		end
	end

	def label_empate(obj)
		if obj
			text = "Rodada Final"
			style = "warning"
			options = {
				style: "margin-top:-5px;"
			}
			bs_label(text, style, options)
		end
	end

	def label_pendente_concluido(status)
		if status.eql?("concluido")
			text = "Concluído"
			style = "success"
		elsif status.eql?("enviado_ao_setor_juridico")
			text = "Enviado ao Setor Jurídico"
			style = "warning"
		elsif status.eql?("recebido_pelo_setor_juridico")
			text = "Recebido pelo Setor Jurídico"
			style = "primary"
		elsif status.eql?("devolvido")
			text = "Parecer Devolvido"
			style = "success"
		else
			text = "Pendente"
			style = "warning"
		end

		bs_label(text, style)
	end
end
