from flask import Blueprint, render_template, request, url_for
from elasticsearch import Elasticsearch

es = Elasticsearch()

form_bp = Blueprint('form_bp',__name__)


########################### FUNCTION FOR OPTION DATA ###########################

def select_list():

	res_flag = es.search(index="lib-index", size= 1000, body={"query": {"match_all": {} }})

	if res_flag:
		res2_flag = []
		output_list = [] 			#List with datagroup values to be appended

		for hit in res_flag['hits']['hits']:
			temp = {}
			for doc_record in hit['_source']:
				temp.update(hit['_source'])

			res2_flag.append(temp)


		for res_datagroup in res2_flag:
			for data_keys, data_values in res_datagroup.items():
				if data_keys == 'datagroup':
					for values in data_values:
						for key, value in values.items():
							if key == 'taggroup' and value not in output_list:
								output_list.append(value)

	output_list.sort()
	return output_list

########################### FUNCTION FOR OPTION DATA ###########################

@form_bp.route('/')
def index_page():
	
	warning = request.args.get("warning")
	no_result = request.args.get("no_result")
	data_list = select_list()


	return render_template("index_page.html", data_list = data_list, warning=warning, no_result=no_result)
