#!/usr/bin/python3

# -------------------------------------------------------------------------------------------
# Produces the CP Admin Dashboard page for logged in users
# It is able to switch between two views, 'performances' and 'venues'
# -------------------------------------------------------------------------------------------
import sys
sys.stdout.reconfigure(encoding='utf-8')
import os
import session_check
from render_response import render_response
user_info = session_check.check_login()
if not user_info:
    render_response(redirect="/cgi-bin/cpadmin_login.py")
    sys.exit()
# -------------------------------------------------------------------------------------------
# only if logged in ...
first_name = user_info["first_name"]
last_name = user_info["last_name"]
user_first_last = f"{ first_name } { last_name }"
message = ''

# import some home-grown dashboard utilities
from dash_utils import cursor_execute_logged, debug_print, get_connection, get_parameters, get_person_name

# -------------------------------------------------------------------------------------------
from jinja2 import Environment, FileSystemLoader, Template
env = Environment(loader=FileSystemLoader('templates'), trim_blocks=True)

# -------------------------------------------------------------------------------------------
args_dict = get_parameters()
view = args_dict.get('view','performances')  # view will be 'performances' (default) or 'venues'

# -------------------------------------------------------------------------------------------
# begin page production
# -------------------------------------------------------------------------------------------

# -------------------------------------------------------------------------------------------
# headings and nav bar
print("Content-Type: text/html; charset=utf-8\n")
dash_title = view.capitalize()  # Performances, Venues
temp_dict = {
    'user_first_last': user_first_last,
    'dash_title' : dash_title,
    'view': view,
    'message': message,
    'severity': 'primary'
    }
template = env.get_template('./cpadmin_head_nav.html')
output = template.render(temp_dict)
print(output)

# -------------------------------------------------------------------------------------------
# datatables headings - this holds the 'Add a Person' button, coded for cg=person and action=add
template = env.get_template('./datatables_head.html')
output = template.render(temp_dict)
print(output)

# -------------------------------------------------------------------------------------------
# connect to database and create a cursor
db = get_connection()
curs = db.cursor()

# -------------------------------------------------------------------------------------------
# obtain the SQL code to fetch the performance or venue data for the datatable
# two possible views
template_name = './cpadmin_performance_dash.sql'  # default
if view == 'performances':
    template_name = './cpadmin_performance_dash.sql'
elif view == 'venues':
    template_name = './cpadmin_venue_dash.sql'
template = env.get_template(template_name)
sql = template.render(temp_dict)

# execute the SQL to produce the cursor, results in cursor
rc = curs.execute(sql)

# -------------------------------------------------------------------------------------------
template = env.get_template('./datatables_row.html')
for row in curs:
    row_td = { **row, **temp_dict }  # don't match any variable names
    output = template.render(row_td)
    print(output)

curs.close()
db.close()

# -------------------------------------------------------------------------------------------
# done with rows, close off datatables, add javascript and datatables scripts
template = env.get_template('./datatables_footer.html')
output = template.render(temp_dict)
print(output)
