#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.21
#  in conjunction with Tcl version 8.6
#    Jul 03, 2019 12:33:08 AM PDT  platform: Windows NT
import mysql.connector as sql
import sys

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 bankdb_support
class Account:
    def __init__(self):
        self.conn=sql.MySQLConnection(user="root",password="shrikant2",database="bankdb",charset='utf8')
        print("Connected to Database...")
        
    def __del__(self):
        self.conn.close()
        print("Connection Closed..")
        
    def open(self,nm,bl):
        smt="insert into accmaster(name,balance) values(%s,%s)"
        cur=self.conn.cursor()
        lst=[nm,bl]
        cur.execute(smt,lst)
        smt="select max(accno) from accmaster"
        cur=self.conn.cursor()
        cur.execute(smt)
        row=cur.fetchone()
        accno=row[0]
        self.trans(accno,'d',bl)
        self.conn.commit()
        return accno
    
    def deposit(self,an,amt):
        smt="update accmaster set balance=balance + %s where accno=%s"
        lst=[amt,an]
        cur=self.conn.cursor()
        cur.execute(smt,lst)
        if cur.rowcount==1:
            self.trans(an,'d',amt)
            self.conn.commit()
            return True
        return False
    
    def withdraw(self,an,amt):
        smt="select balance from accmaster where accno="+an
        cur=self.conn.cursor()
        cur.execute(smt)
        row=cur.fetchone()
        if row==None:
            return -1
        bal=row[0]
        if int(amt)>bal:
            return 0
        smt="update accmaster set balance=balance - %s where accno=%s"
        cur=self.conn.cursor()
        params=[amt,an]
        cur.execute(smt,params)
        self.trans(an,'w',amt)
        self.conn.commit()
        return 1
    
    def search(self,an):
        smt="select * from accmaster where accno="+an
        cur=self.conn.cursor()
        cur.execute(smt)
        row=cur.fetchone()
        return row
    
    def gettrans(self,an):
        smt="select * from trans where accno="+an
        cur=self.conn.cursor()
        cur.execute(smt)
        rows=cur.fetchall()
        return rows

    def close(self,an):
        smt="delete from accmaster where accno="+an
        cur=self.conn.cursor()
        cur.execute(smt)
        smt="delete from trans where accno="+an
        cur=self.conn.cursor()
        cur.execute(smt)
        self.conn.commit()

    def listacc(self):
        smt="select * from accmaster"
        cur=self.conn.cursor()
        cur.execute(smt)
        rows=cur.fetchall()
        return rows

    def interest(self,r,n):
        rows=self.listacc()
        for row in rows:
            an=row[0]
            bal=row[2]
            si=bal*float(r)*float(n)/100
            self.deposit(an,si)
    
    def trans(self,an,ty,amt):
        smt="insert into trans(accno,transtype,amount) values(%s,%s,%s)"
        lst=[an,ty,amt]
        cur=self.conn.cursor()
        cur.execute(smt,lst)


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

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

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

class Toplevel:
    def __init__(self, top=None):
        '''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'
        font10 = "-family {Courier New} -size 10"
        font9 = "-family {Segoe UI} -size 9"

        top.geometry("982x762+522+150")
        top.title("bank db")
        top.configure(background="#d9d9d9")
        top.configure(highlightbackground="#d9d9d9")
        top.configure(highlightcolor="black")

        self.Frame1 = tk.Frame(top)
        self.Frame1.place(relx=0.143, rely=0.039, relheight=0.833
                , relwidth=0.667)
        self.Frame1.configure(relief='groove')
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(relief='groove')
        self.Frame1.configure(background="#a2d8d6")
        self.Frame1.configure(highlightbackground="#52d84e")
        self.Frame1.configure(highlightcolor="#000000")
        self.Frame1.configure(width=655)

        self.Listbox1 = tk.Listbox(self.Frame1)
        self.Listbox1.place(relx=0.244, rely=0.031, relheight=0.696
                , relwidth=0.51)
        self.Listbox1.configure(background="white")
        self.Listbox1.configure(disabledforeground="#a3a3a3")
        self.Listbox1.configure(font=font10)
        self.Listbox1.configure(foreground="#000000")
        self.Listbox1.configure(highlightbackground="#d9d9d9")
        self.Listbox1.configure(highlightcolor="black")
        self.Listbox1.configure(selectbackground="#c4c4c4")
        self.Listbox1.configure(selectforeground="black")
        self.Listbox1.configure(width=334)

        self.Label1 = tk.Label(self.Frame1)
        self.Label1.place(relx=0.214, rely=0.787, height=41, width=134)
        self.Label1.configure(activebackground="#f9f9f9")
        self.Label1.configure(activeforeground="black")
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(highlightbackground="#d9d9d9")
        self.Label1.configure(highlightcolor="black")
        self.Label1.configure(text='''Enter your choice here :''')

        self.txtvalue = tk.Text(self.Frame1)
        self.txtvalue.place(relx=0.473, rely=0.787, relheight=0.069
                , relwidth=0.311)
        self.txtvalue.configure(background="white")
        self.txtvalue.configure(font=font9)
        self.txtvalue.configure(foreground="black")
        self.txtvalue.configure(highlightbackground="#d9d9d9")
        self.txtvalue.configure(highlightcolor="black")
        self.txtvalue.configure(insertbackground="black")
        self.txtvalue.configure(selectbackground="#c4c4c4")
        self.txtvalue.configure(selectforeground="black")
        self.txtvalue.configure(width=204)
        self.txtvalue.configure(wrap='word')

        self.menubar = tk.Menu(top, font=('Segoe UI', 9, ), bg=_bgcolor
                ,fg=_fgcolor)
        top.configure(menu = self.menubar)

if __name__ == '__main__':
    vp_start_gui()
acc=Account()
ch=0
while ch!=9:
    menu="1:Open Acc\n2:Depost\n3:Withdraw\n4:Search\n5:Transaction\n6:CloseAcc\n7:List\n8:Interest\n9:Exit"
    self.Listbox1.insert(print(menu))
    ch=self.txtvalue.get()
    ch=int(ch)
    if ch==1:
        nm=input("Enter Name:")
        bl=input("Enter Balance:")
        an=acc.open(nm,bl)
        print("Account Opened : AccNo:",an)
    if ch==2:
        an=input("Enter AccNo:")
        amt=input("Enter Amount to Deposit:")
        if(acc.deposit(an,amt)):
            print("Amount Deposited..")
        else:
            print("AccNo Not Found..")
    if ch==3:
        an=input("Enter AccNo:")
        amt=input("Enter Amount to Withdraw:")
        res=acc.withdraw(an,amt)
        if res==-1:
            print("AccNo Not Found..")
        elif res==0:
            print("Insufficient Balance")
        else:
            print("Amount Withdrawn Successfully..")
    if ch==4:
        an=input("Enter AccNo:")
        row=acc.search(an)
        if row==None:
            print("AccNo not Found..")
        else:
            print("Name:",row[1])
            print("Balance:",row[2])
    if ch==5:
        an=input("Enter AccNo:")
        row=acc.search(an)
        if row==None:
            print("AccNo not Found..")
        else:
            print("Name:",row[1])
            print("Balance:",row[2])
            rows=acc.gettrans(an)
            print("-"*37)
            for row in rows:
                print("|",row[1],"|",row[3],"|",str(row[4]).rjust(8),"|")
            print("-"*37)
    if ch==6:
        an=input("Enter AccNo:")
        row=acc.search(an)
        if row==None:
            print("AccNo not Found..")
        else:
            print("Name:",row[1])
            print("Balance:",row[2])
            res=input("Are you sure ? (y/n):")
            if res=="y":
                acc.close(an)
                print("A/C closed successfully..")
    if ch==7:
        rows=acc.listacc()
        print("-"*42)
        for row in rows:
              print("|",row[0],"|",row[1].ljust(20),"|",str(row[2]).rjust(8),"|")
        print("-"*42)
    if ch==8:
        r=input("Enter IRate:")
        n=input("Enter Period:")
        acc.interest(r,n)
        print("Calculated and added..")
del acc





