#!/usr/bin/python3

# -------------------------------------------------------------------------------------------
# produces the ASF Alumni Dashboard page for logged in users
# -------------------------------------------------------------------------------------------
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/asfmex_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)

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

# headings and nav bar

# -------------------------------------------------------------------------------------------
print("Content-Type: text/html; charset=utf-8\n")

dash_title = 'Alumni Listing'
temp_dict = {
    'user_first_last': user_first_last,
    'dash_title' : dash_title,
    'message': message,
    'severity': 'primary'
    }
template = env.get_template('./alumni_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 person data for the datatable
template = env.get_template('./alumni_dash.sql')
sql = template.render({})
# execute the SQL to produce the cursor, results in cursor
rc = curs.execute(sql)

# -------------------------------------------------------------------------------------------
template = env.get_template('./datatables_row.html')
sttc = { # colors for the status_type column
    'Ok'        : 'success',  # green
    'Help'      : 'warning',  # danger
    'Deceased'  : 'info',     # turquoise
    'Lost'      : 'danger',   # red
    'New'       : 'primary',  # blue
    'Unknown'   : 'secondary' # gray
}
for row in curs:
    tc = 'secondary'
    row['tc'] = sttc[row['status_type']]
    # print(html.encode(row['person_id'])  ##debug
    output = template.render(row)
    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({})
print(output)
