class SearchWindow:
    def __init__(self):
        self.db_conn = DBInteraction()

        self.root = Tk()
        self.root.wm_title("Search Database")
        self.root.geometry("650x400")

        self.search_label = Label(self.root, font="Georgia 14 bold", text="Search:")
        self.search_label.pack()

        self.search_box = Entry(self.root, font="Georgia 14")
        self.search_box['width'] = 20
        self.search_box.pack()

        self.search_btn = Button(self.root, text="Search", command=self.search_db)
        self.search_btn.pack()

        spacer = Label(self.root, text="\n\n", font="Georgia 5")
        spacer.pack()

        id_label = Label(self.root, text="ID", font="Georgia 14 bold")
        id_label.place(x=65, y=80)
        name_label = Label(self.root, text="Name", font="Georgia 14 bold")
        name_label.place(x=220, y=80)
        age_label = Label(self.root, text="Age", font="Georgia 14 bold")
        age_label.place(x=390, y=80)
        gender_label = Label(self.root, text="Gender", font="Georgia 14 bold")
        gender_label.place(x=525, y=80)

        self.references = []
        self.root.mainloop()

        self.db_conn.shutdown()

    def search_db(self):
        # ============== DELETING THE OLD RESULTS ==============
        for widget in self.references:
            widget.pack_forget()

        # ================= GETTING NEW RESULTS ================
        name = str(self.search_box.get())
        db_results = (self.db_conn.search_data(name))

        for i in db_results:
            temp = Label(self.root,
                         text=str(
                            str(i[0]) + "\t\t" + str(i[1]) + "\t"
                            "\t" + str(i[2]) + "\t\t" + str(i[3])
                            ),
                         font="Georgia 12"
                         )
            temp.pack()
            self.references.append(temp)
allergies_box.place(x=70, y=310)

diseases_conditions = Frame(root, relief=GROOVE, bd=5)
diseases_conditions.place(x=650, y=260, width=550, height=200)
diseases_conditions_heading = Label(root, font="Georgia 14 bold", text="Diseases/Conditions")
diseases_conditions_heading.place(x=660, y=270)
diseases_conditions_box = Text(root, font="Georgia 12", width=50, height=7)
diseases_conditions_box.place(x=670, y=310)

med_history = Frame(root, relief=GROOVE, bd=5)
med_history.place(x=50, y=470, width=550, height=200)
med_history_heading = Label(root, font="Georgia 14 bold", text="Medical/Treatment History")
med_history_heading.place(x=60, y=480)
med_history_box = Text(root, font="Georgia 12", width=50, height=7)
med_history_box.place(x=70, y=520)

add_comments = Frame(root, relief=GROOVE, bd=5)
add_comments.place(x=650, y=470, width=550, height=200)
add_comments_heading = Label(root, font="Georgia 14 bold", text="Additional Comments")
add_comments_heading.place(x=660, y=480)
add_comments_box = Text(root, font="Georgia 12", width=50, height=7)
add_comments_box.place(x=670, y=520)

# ===================================================================================================
# ================================= CLOSING ALL CONNECTIONS AND FILES ===============================
# ===================================================================================================

root.mainloop()
db.shutdown()
f.close()