#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.20
#  in conjunction with Tcl version 8.6
#    Mar 02, 2019 09:01:42 AM +0800  platform: Windows NT

import sys
from tkinter import *
import mysql.connector
from mysql.connector import Error
from tkinter import messagebox

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import PageAttMas_support
import time
import datetime
import random

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    PageAttMas_support.set_Tk_var()
    top = AtMas (root)
    PageAttMas_support.init(root, top)
    root.mainloop()

w = None
def create_AtMas(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = tk.Toplevel (root)
    PageAttMas_support.set_Tk_var()
    top = AtMas (w)
    PageAttMas_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_AtMas():
    global w
    w.destroy()
    w = None  

class AtMas:
    
    def __init__(self, top=None):
        try:
            self.mydb = mysql.connector.connect(
                host = "localhost",
                user = "root",
                password = "",
                database = "attenddb"
                )
        except Exception:
            messagebox.showerror("Error!", "Unable to connect to database.")
        
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85' 
        _ana2color = '#ececec' # Closest X11 color: 'gray92' 
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=[('selected', _compcolor), ('active',_ana2color)])
        
        top.geometry("1300x680+333+131")
        top.title("Attendance Master")
        top.configure(background="black")
        

#=====================TO ADD STUDENTS AND RECORDS==================================================================================        

        def add_stud():
            try:
                self.list_1.insert(tk.END, "ID_No.: "+self.entID.get()+" -- "+self.entLname.get()+", "+self.entFname.get()+" -- "+self.gencombo.get()+" -- "+self.StatCombo.get())

                self.mycursor = self.mydb.cursor()

                self.sql = "INSERT INTO class01 (ID_No, Firstname, Lastname, Gender, Status) VALUES (%s, %s, %s, %s, %s)"
                self.val = (self.entID.get(), self.entFname.get(), self.entLname.get(), self.gencombo.get(), self.StatCombo.get())
                self.mycursor.execute(self.sql, self.val)

                self.mydb.commit()

                messagebox.showinfo("YEY!!!", "Record successfully inserted.")

            except Exception:
                messagebox.showerror("Error!","Same ID not allowed.")

            
#=====================RETRIEVING RECORDS==================================================================================
            
        def showData():
            try:
                self.mycursor = self.mydb.cursor()

                self.mycursor.execute("SELECT * FROM class01 ORDER BY ID_No")

                self.myresult = self.mycursor.fetchall()

                self.list_2.delete(0, tk.END)

                for x in self.myresult:
                    self.list_2.insert(tk.END, x)

            except Exception:
                messagebox.showerror("Error!", "No Database Found.")
                
#=====================TO RESET THE ENTRY==================================================================================            

        def reset_data():
            self.Fnamevar.set("")
            self.Lnamevar.set("")
            self.IDvar.set("")
            PageAttMas_support.gencombobox.set("")
            PageAttMas_support.statcombobox.set("")
        
#=====================TO CLEAR LISTBOX==================================================================================
        def clr_data():
            self.list_1.delete(0, tk.END)

        def clr_data2():
            self.list_2.delete(0, tk.END)

#===================================DELETE ALL RECORDS===================
        def delete_all():
            del_all = messagebox.askyesno("Delete All?", "It will clear the table, continue?")
            mycursor = self.mydb.cursor()
            if del_all > 0:
                sql = "DELETE FROM class01"
                mycursor.execute(sql)
                self.mydb.commit()
                return
#=============================DELETE RECORD=============================================

        def del_stud():
            
            x = self.list_2.get(tk.ACTIVE)
            del_mes = messagebox.askyesno("Delete?", "Are you sure?")
            mycursor = self.mydb.cursor()
            if del_mes > 0:
                mycursor.execute("DELETE FROM class01 WHERE ID_No=%s AND Firstname=%s AND Lastname=%s AND Gender=%s AND Status=%s", (x))

                self.mydb.commit()
                mycursor.close()

                self.list_2.delete(tk.ANCHOR)
                return
                

        def __del__(self):
            self.mydb.close()


#===========================================================UPDATE DB===========================================

        def db_update():
            
            mycursor = self.mydb.cursor()
            sql = """UPDATE class01
                        SET Firstname=%s, Lastname=%s, Gender=%s, Status=%s
                        WHERE ID_No=%s
                        """
            val = (self.entFname.get(), self.entLname.get(), self.gencombo.get(), self.StatCombo.get(), (self.entID.get()))

            messagebox.showinfo("Update","Record Successfully updated.")

            mycursor.execute(sql, val)
            

            self.mydb.commit()
            #self.mydb.close()
            #self.list_2.update()


#=============================================TOP FRAME===============================================
        self.TopFrame = tk.Frame(top)
        self.TopFrame.place(relx=0.278, rely=0.0, relheight=0.333, relwidth=0.443)
        self.TopFrame.configure(relief='raised')
        self.TopFrame.configure(borderwidth="8")
        self.TopFrame.configure(relief='raised')
        self.TopFrame.configure(background="#d1d802")
        self.TopFrame.configure(width=605)

        self.date_time = tk.StringVar()
        self.date_time.set(time.strftime("%d/%m/%Y"))
        self.lbldate = tk.Label(self.TopFrame, textvariable=self.date_time)
        self.lbldate.place(relx=0.017, rely=0.043, height=25, width=80)
        self.lbldate.configure(background="#d9d9d9")
        self.lbldate.configure(disabledforeground="#a3a3a3")
        self.lbldate.configure(foreground="#000000")
        self.lbldate.configure(font=('arial',10,'bold'), bg='yellow', bd=5, relief='sunken', fg='blue')

        self.Time_date = tk.StringVar()
        self.Time_date.set(time.strftime("%H:%M"))
        self.lbltime = tk.Label(self.TopFrame, textvariable=self.Time_date)
        self.lbltime.place(relx=0.823, rely=0.043, height=25, width=60)
        self.lbltime.configure(background="#d9d9d9")
        self.lbltime.configure(disabledforeground="#a3a3a3")
        self.lbltime.configure(foreground="#000000")
        self.lbltime.configure(font=('arial',10,'bold'), bg='yellow', bd=5, relief='sunken', fg='blue')

        self.lblFname = tk.Label(self.TopFrame)
        self.lblFname.place(relx=0.017, rely=0.220, height=21, width=61)
        self.lblFname.configure(background="#d1d802")
        self.lblFname.configure(disabledforeground="#a3a3a3")
        self.lblFname.configure(foreground="#000000")
        self.lblFname.configure(text='''Firstname:''', font=('Arial',8,'bold'))

        self.Fnamevar = tk.StringVar()
        self.entFname = tk.Entry(self.TopFrame, textvariable=self.Fnamevar)
        self.entFname.place(relx=0.132, rely=0.220,height=20, relwidth=0.271)
        self.entFname.configure(background="white")
        self.entFname.configure(disabledforeground="#a3a3a3")
        self.entFname.configure(font="TkFixedFont")
        self.entFname.configure(foreground="#000000")
        self.entFname.configure(insertbackground="black")

        self.lblLname = tk.Label(self.TopFrame)
        self.lblLname.place(relx=0.446, rely=0.220, height=21, width=60)
        self.lblLname.configure(background="#d1d802")
        self.lblLname.configure(disabledforeground="#a3a3a3")
        self.lblLname.configure(foreground="#000000")
        self.lblLname.configure(text='''Lastname:''', font=('Arial',8,'bold'))

        self.Lnamevar = tk.StringVar()
        self.entLname = tk.Entry(self.TopFrame, textvariable=self.Lnamevar)
        self.entLname.place(relx=0.562, rely=0.220,height=20, relwidth=0.271)
        self.entLname.configure(background="white")
        self.entLname.configure(disabledforeground="#a3a3a3")
        self.entLname.configure(font="TkFixedFont")
        self.entLname.configure(foreground="#000000")
        self.entLname.configure(insertbackground="black")

        self.lblid = tk.Label(self.TopFrame)
        self.lblid.place(relx=0.017, rely=0.355, height=21, width=42)
        self.lblid.configure(background="#d1d802")
        self.lblid.configure(disabledforeground="#a3a3a3")
        self.lblid.configure(foreground="#000000")
        self.lblid.configure(text='''ID No.:''', font=('Arial',8,'bold'))

        self.IDvar = tk.StringVar()
        self.entID = tk.Entry(self.TopFrame, textvariable=self.IDvar)
        self.entID.place(relx=0.132, rely=0.355,height=20, relwidth=0.271)
        self.entID.configure(background="white")
        self.entID.configure(disabledforeground="#a3a3a3")
        self.entID.configure(font="TkFixedFont")
        self.entID.configure(foreground="#000000")
        self.entID.configure(insertbackground="black")
        self.entID.configure(width=164)

        self.lblGen = tk.Label(self.TopFrame)
        self.lblGen.place(relx=0.017, rely=0.500, height=21, width=47)
        self.lblGen.configure(background="#d1d802")
        self.lblGen.configure(disabledforeground="#a3a3a3")
        self.lblGen.configure(foreground="#000000")
        self.lblGen.configure(text='''Gender:''', font=('Arial',8,'bold'))

        self.gencombo = ttk.Combobox(self.TopFrame)
        self.gencombo.place(relx=0.132, rely=0.500, relheight=0.095, relwidth=0.236)
        self.gencombo.configure(textvariable=PageAttMas_support.gencombobox, state='readonly', values=("Male","Female","Shemale","Undecided"))
        self.gencombo.configure(takefocus="")

        self.lblStat = tk.Label(self.TopFrame)
        self.lblStat.place(relx=0.017, rely=0.630, height=21, width=41)
        self.lblStat.configure(background="#d1d802")
        self.lblStat.configure(disabledforeground="#a3a3a3")
        self.lblStat.configure(foreground="#000000")
        self.lblStat.configure(text='''Status:''', font=('Arial',8,'bold'))

        self.StatCombo = ttk.Combobox(self.TopFrame)
        self.StatCombo.place(relx=0.132, rely=0.630, relheight=0.095, relwidth=0.236)
        self.StatCombo.configure(textvariable=PageAttMas_support.statcombobox, state='readonly', values=("Present","Absent","Late","Excuse"))
        self.StatCombo.configure(takefocus="")

        self.btnAdd = tk.Button(self.TopFrame)
        self.btnAdd.place(relx=0.235, rely=0.851, height=24, width=77)
        self.btnAdd.configure(activebackground="#ececec")
        self.btnAdd.configure(activeforeground="#000000")
        self.btnAdd.configure(background="#d9d9d9")
        self.btnAdd.configure(disabledforeground="#a3a3a3")
        self.btnAdd.configure(foreground="#000000")
        self.btnAdd.configure(highlightbackground="#d9d9d9")
        self.btnAdd.configure(highlightcolor="black")
        self.btnAdd.configure(pady="0")
        self.btnAdd.configure(command=add_stud, text='''Add Student''', font=('Arial',8,'bold'))

        self.btnReset = tk.Button(self.TopFrame)
        self.btnReset.place(relx=0.650, rely=0.851, height=24, width=42)
        self.btnReset.configure(activebackground="#ececec")
        self.btnReset.configure(activeforeground="#000000")
        self.btnReset.configure(background="#d9d9d9")
        self.btnReset.configure(disabledforeground="#a3a3a3")
        self.btnReset.configure(foreground="#000000")
        self.btnReset.configure(highlightbackground="#d9d9d9")
        self.btnReset.configure(highlightcolor="black")
        self.btnReset.configure(pady="0")
        self.btnReset.configure(command=reset_data, text='''Reset''', font=('Arial',8,'bold'))

        
#=============================================BOTTOM FRAME_1===============================================
        self.BtmFrame = tk.Frame(top)
        self.BtmFrame.place(relx=0.0, rely=0.326, relheight=0.674, relwidth=0.494)
        self.BtmFrame.configure(relief='groove')
        self.BtmFrame.configure(borderwidth="2")
        self.BtmFrame.configure(relief='groove')
        self.BtmFrame.configure(background="red")
        self.BtmFrame.configure(width=675)

        self.scroll = tk.Scrollbar(self.BtmFrame, orient='vertical')
        self.list_1 = tk.Listbox(self.BtmFrame, yscrollcommand=self.scroll.set, width=101, height=25, bd=5, relief='sunken')
        self.scroll.config(command=self.list_1.yview, width=20)
        self.scroll.grid(row=0, column=1, sticky=w)
        self.list_1.grid(row=0, column=0, sticky=w)

        self.btnClear = tk.Button(self.BtmFrame)
        self.btnClear.place(relx=0.840, rely=0.920, height=24, width=75)
        self.btnClear.configure(activebackground="#ececec")
        self.btnClear.configure(activeforeground="#000000")
        self.btnClear.configure(background="#d9d9d9")
        self.btnClear.configure(disabledforeground="#a3a3a3")
        self.btnClear.configure(foreground="#000000")
        self.btnClear.configure(highlightbackground="#d9d9d9")
        self.btnClear.configure(highlightcolor="black")
        self.btnClear.configure(pady="0")
        self.btnClear.configure(command=clr_data, text='''Clear Data''', font=('Arial',8,'bold'))

        
#=============================================3RD FRAME===============================================
        self.Frame3 = tk.Frame(top)
        self.Frame3.place(relx=0.49, rely=0.326, relheight=0.674, relwidth=0.509)

        self.Frame3.configure(relief='groove')
        self.Frame3.configure(borderwidth="2")
        self.Frame3.configure(relief='groove')
        self.Frame3.configure(background="blue")
        self.Frame3.configure(width=695)

        self.scroll = tk.Scrollbar(self.Frame3, orient='vertical')
        self.list_2 = tk.Listbox(self.Frame3, yscrollcommand=self.scroll.set, width=105, height=25, bd=5, relief='sunken')
        self.scroll.config(command=self.list_2.yview, width=20)
        self.scroll.grid(row=0, column=1, sticky=w)
        self.list_2.grid(row=0, column=0, sticky=w)

        self.btnShowStud = tk.Button(self.Frame3)
        self.btnShowStud.place(relx=0.035, rely=0.920, height=24, width=90)
        self.btnShowStud.configure(activebackground="#ececec")
        self.btnShowStud.configure(activeforeground="#000000")
        self.btnShowStud.configure(background="#d9d9d9")
        self.btnShowStud.configure(disabledforeground="#a3a3a3")
        self.btnShowStud.configure(foreground="#000000")
        self.btnShowStud.configure(highlightbackground="#d9d9d9")
        self.btnShowStud.configure(highlightcolor="black")
        self.btnShowStud.configure(pady="0")
        self.btnShowStud.configure(command=showData, text='''Show Students''', font=('Arial',8,'bold'))

        self.btnDelete = tk.Button(self.Frame3)
        self.btnDelete.place(relx=0.850, rely=0.920, height=24, width=75)
        self.btnDelete.configure(activebackground="#ececec")
        self.btnDelete.configure(activeforeground="#000000")
        self.btnDelete.configure(background="#d9d9d9")
        self.btnDelete.configure(disabledforeground="#a3a3a3")
        self.btnDelete.configure(foreground="#000000")
        self.btnDelete.configure(highlightbackground="#d9d9d9")
        self.btnDelete.configure(highlightcolor="black")
        self.btnDelete.configure(pady="0")
        self.btnDelete.configure(command=delete_all, text='''Delete All''', font=('Arial',8,'bold'))

        self.btnClr = tk.Button(self.Frame3)
        self.btnClr.place(relx=0.450, rely=0.920, height=24, width=50)
        self.btnClr.configure(activebackground="#ececec")
        self.btnClr.configure(activeforeground="#000000")
        self.btnClr.configure(background="#d9d9d9")
        self.btnClr.configure(disabledforeground="#a3a3a3")
        self.btnClr.configure(foreground="#000000")
        self.btnClr.configure(highlightbackground="#d9d9d9")
        self.btnClr.configure(highlightcolor="black")
        self.btnClr.configure(pady="0")
        self.btnClr.configure(command=clr_data2, text='''Clear''', font=('Arial',8,'bold'))

        self.btnDel_rec = tk.Button(self.Frame3)
        self.btnDel_rec.place(relx=0.635, rely=0.920, height=24, width=90)
        self.btnDel_rec.configure(activebackground="#ececec")
        self.btnDel_rec.configure(activeforeground="#000000")
        self.btnDel_rec.configure(background="#d9d9d9")
        self.btnDel_rec.configure(disabledforeground="#a3a3a3")
        self.btnDel_rec.configure(foreground="#000000")
        self.btnDel_rec.configure(highlightbackground="#d9d9d9")
        self.btnDel_rec.configure(highlightcolor="black")
        self.btnDel_rec.configure(pady="0")
        self.btnDel_rec.configure(command=del_stud, text='''Delete Record''', font=('Arial',8,'bold'))

        self.btnUpdate = tk.Button(self.Frame3)
        self.btnUpdate.place(relx=0.250, rely=0.920, height=24, width=50)
        self.btnUpdate.configure(activebackground="#ececec")
        self.btnUpdate.configure(activeforeground="#000000")
        self.btnUpdate.configure(background="#d9d9d9")
        self.btnUpdate.configure(disabledforeground="#a3a3a3")
        self.btnUpdate.configure(foreground="#000000")
        self.btnUpdate.configure(highlightbackground="#d9d9d9")
        self.btnUpdate.configure(highlightcolor="black")
        self.btnUpdate.configure(pady="0")
        self.btnUpdate.configure(command=db_update, text='''Update''', font=('Arial',8,'bold'))

if __name__ == '__main__':
    vp_start_gui()





