class BuyBooks(SeeBooks):
    def __init__(self):

        super().__init__()
        self.see=SeeBooks()
        self.db=MyDb()
        #self.app = Guis()



    def buy_gui(self):
        self.buy_dis = Tk()
        self.buy_dis.title('BUY BOOKS')
        self.buy_dis.geometry('1920x1080')
        self.buy_dis.configure(bg='dark orange3')

        self.see = SeeBooks()

        self.lbl111 = Label(self.buy_dis, bg='dark orange1', width=600, height=4,
                            font=('Ariel', 15, 'bold'))
        self.lbl111.place(x=1, y=20)

        self.lbl113 = Label(self.buy_dis, bg='dark orange1', width=60, height=12)
        self.lbl113.place(x=60, y=250)


        self.lbl116 = Label(self.buy_dis, bg='dark orange1', width=60, height=14)
        self.lbl116.place(x=60, y=460)

        self.lbl115 = Label(self.buy_dis, bg='dark orange1', width=68, height=4)
        self.lbl115.place(x=1, y=160)

        self.lbl113 = Label(self.buy_dis, bg='dark orange1', width=40, height=4)
        self.lbl113.place(x=600, y=160)

        self.lbl118 = Label(self.buy_dis, bg='dark orange1', width=600, height=4)
        self.lbl118.place(x=600, y=600)

        self.lbl112 = Label(self.buy_dis, text='ORIENTAL BOOK HOUSE', bg='dark orange1', fg='black',
                            font=('Ariel', 19, 'bold'))
        self.lbl112.place(x=560, y=30)

        self.lbl112 = Label(self.buy_dis, text='Bagbazar, Kathmandu', bg='dark orange1', fg='black',
                            font=('Ariel', 13, 'bold'))
        self.lbl112.place(x=620, y=75)
        self.sort_lbl = Label(self.buy_dis, text='Search and Sort Books', bg='dark orange1', font=('Ariel', 16, 'bold'))
        self.sort_lbl.place(x=110, y=180)

        self.sortby_lbl = Label(self.buy_dis, text='Search by :', font=('Ariel', 13, 'bold'))
        self.sortby_lbl.place(x=130, y=270)

        self.cat = ttk.Combobox(self.buy_dis, text="Category",font=('Ariel', 13),width=16)
        self.cat.place(x=230, y=270)
        self.cat.set('----SELECT FIRST----')
        # opening above saved file to show it on combobox
        # access category from database here---
        # self.cat['values'] =
        # self.cat.grid()
        sortby = ['Name', 'Writer', 'Publisher', 'Catagory']

        self.cat['values'] = sortby

        self.selectbut = Button(self.buy_dis, text='Select', command=self.select,font=('Ariel', 11, 'bold'))
        self.selectbut.place(x=340, y=300)

        self.lbl33=Label(self.buy_dis,text='Results:',bg='dark orange1', font=('Ariel', 16, 'bold'))
        self.lbl33.place(x=650, y=180)

        self.add_tree = ttk.Treeview(self.buy_dis, column=('n', 'w', 'p', 'm', 'c'))
        self.add_tree.place(x=600, y=250)
        self.add_tree['show'] = 'headings'
        self.add_tree.column('n', width=200)
        self.add_tree.column('w', width=100)
        self.add_tree.column('p', width=100)
        self.add_tree.column('m', width=100)
        self.add_tree.column('c', width=100)
        self.add_tree.heading('n', text='Book')
        self.add_tree.heading('w', text='Writer')
        self.add_tree.heading('p', text='Price')
        self.add_tree.heading('m', text='Publisher')
        self.add_tree.heading('c', text='Catagory')

        self.start_tree()


        self.lbl22 = Label(self.buy_dis, text='*Selected', bg='dark orange1', font=('Ariel', 10, 'bold'))
        self.lbl22.place(x=130, y=470)

        self.get1 = Label(self.buy_dis, text='Book :', font=('Ariel', 13, 'bold'), width=9)
        self.get1.place(x=130, y=500)

        self.get1 = Label(self.buy_dis, text='Price :', font=('Ariel', 13, 'bold'), width=9)
        self.get1.place(x=130, y=540)

        self.get1 = Label(self.buy_dis, text='Dis % :', font=('Ariel', 13, 'bold'), width=9)
        self.get1.place(x=130, y=580)

        self.nament = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
        self.nament.place(x=230, y=500)

        self.priceent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
        self.priceent.place(x=230, y=540)

        self.disent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
        self.disent.place(x=230, y=580)

        self.addbill = Button(self.buy_dis, text="Calculate", command=self.bills, font=('Ariel', 11, 'bold'))
        self.addbill.place(x=390, y=620)


        self.back=Button(self.buy_dis, text="Back", command=self.backbut, bg='green', font=('Ariel', 9, 'bold'))
        self.back.place(x=1290,y=620)

        self.buy_dis.mainloop()


    def select(self):
        self.ab = AddBooks()

        user_chosen = self.cat.get()

        if user_chosen == '':
            messagebox.showerror('Error', 'Select first')


        elif user_chosen == 'Name':
            self.search_ent = Entry(self.buy_dis, text='Keyword', font=('Ariel', 13, 'bold'), width=18)
            self.search_ent.place(x=230, y=350)

            self.selectbut = Button(self.buy_dis, text='Search', command=self.tree_byname,
                                    font=('Ariel', 11, 'bold'))
            self.selectbut.place(x=335, y=380)

            self.search_lbl = Label(self.buy_dis, text='Keyword :', font=('Ariel', 13, 'bold'))
            self.search_lbl.place(x=130, y=350)

            # self.lbl22=Label (self.buy_dis, text='*Selected',bg='dark orange1', font=('Ariel', 10, 'bold'))
            # self.lbl22.place(x=130, y=470)
            #
            # self.get1=Label(self.buy_dis, text='Book :',font=('Ariel', 13, 'bold'),width=9)
            # self.get1.place(x=130, y=500)
            #
            # self.get1 = Label(self.buy_dis, text='Price :',font=('Ariel', 13, 'bold'),width=9)
            # self.get1.place(x=130, y=540)
            #
            # self.get1 = Label(self.buy_dis, text='Dis % :',font=('Ariel', 13, 'bold'),width=9)
            # self.get1.place(x=130, y=580)
            #
            # self.nament=Entry(self.buy_dis,font=('Ariel', 13, 'bold'), width=18)
            # self.nament.place(x=230, y=500)
            #
            #
            # self.priceent = Entry(self.buy_dis,font=('Ariel', 13, 'bold'), width=18)
            # self.priceent.place(x=230, y=540)
            #
            #
            # self.disent = Entry(self.buy_dis,font=('Ariel', 13, 'bold'), width=18)
            # self.disent.place(x=230, y=580)
            #
            # self.addbill= Button(self.buy_dis, text="Calculate", command=self.bills, font=('Ariel', 11, 'bold'))
            # self.addbill.place(x=390, y=620)




        elif user_chosen == 'Writer':

            self.search_ent = Entry(self.buy_dis, text='Keyword', font=('Ariel', 13, 'bold'), width=18)
            self.search_ent.place(x=230, y=350)

            self.selectbut = Button(self.buy_dis, text='Search', command=self.tree_bywriter,
                                    font=('Ariel', 11, 'bold'))
            self.selectbut.place(x=335, y=380)

            self.search_lbl = Label(self.buy_dis, text='Keyword :', font=('Ariel', 13, 'bold'))
            self.search_lbl.place(x=130, y=350)

            # self.lbl22 = Label(self.buy_dis, text='*Selected', bg='dark orange1', font=('Ariel', 10, 'bold'))
            # self.lbl22.place(x=130, y=470)
            #
            # self.get1 = Label(self.buy_dis, text='Book :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=500)
            #
            # self.get1 = Label(self.buy_dis, text='Price :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=540)
            #
            # self.get1 = Label(self.buy_dis, text='Dis % :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=580)
            #
            # self.nament = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.nament.place(x=230, y=500)
            #
            # self.priceent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.priceent.place(x=230, y=540)
            #
            # self.disent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.disent.place(x=230, y=580)
            #
            # self.addbill = Button(self.buy_dis, text="Calculate", command=self.bills, font=('Ariel', 11, 'bold'))
            # self.addbill.place(x=390, y=620)

        elif user_chosen == 'Publisher':

            self.search_ent = Entry(self.buy_dis, text='Keyword', font=('Ariel', 13, 'bold'), width=18)
            self.search_ent.place(x=230, y=350)

            self.selectbut = Button(self.buy_dis, text='Search', command=self.tree_bypublisher,
                                    font=('Ariel', 11, 'bold'))
            self.selectbut.place(x=335, y=380)

            self.search_lbl = Label(self.buy_dis, text='Keyword :', font=('Ariel', 13, 'bold'))
            self.search_lbl.place(x=130, y=350)

            # self.lbl22 = Label(self.buy_dis, text='*Selected', bg='dark orange1', font=('Ariel', 10, 'bold'))
            # self.lbl22.place(x=130, y=470)
            #
            # self.get1 = Label(self.buy_dis, text='Book :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=500)
            #
            # self.get1 = Label(self.buy_dis, text='Price :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=540)
            #
            # self.get1 = Label(self.buy_dis, text='Dis % :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=580)
            #
            # self.nament = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.nament.place(x=230, y=500)
            #
            # self.priceent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.priceent.place(x=230, y=540)
            #
            # self.disent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.disent.place(x=230, y=580)
            #
            # self.addbill = Button(self.buy_dis, text="Calculate", command=self.bills, font=('Ariel', 11, 'bold'))
            # self.addbill.place(x=390, y=620)

        elif user_chosen == 'Catagory':

            values = self.ab.combo()

            self.search_ent = ttk.Combobox(self.buy_dis, text='Keyword', font=('Ariel', 13, 'bold'), width=16,values=values)
            self.search_ent.place(x=230, y=350)

            self.selectbut = Button(self.buy_dis, text='Search', command=self.tree_bycatagory,
                                    font=('Ariel', 11, 'bold'))
            self.selectbut.place(x=335, y=380)

            self.search_lbl = Label(self.buy_dis, text='Keyword :', font=('Ariel', 13, 'bold'))
            self.search_lbl.place(x=130, y=350)

            # self.lbl22 = Label(self.buy_dis, text='*Selected', bg='dark orange1', font=('Ariel', 10, 'bold'))
            # self.lbl22.place(x=130, y=470)
            #
            # self.get1 = Label(self.buy_dis, text='Book :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=500)
            #
            # self.get1 = Label(self.buy_dis, text='Price :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=540)
            #
            # self.get1 = Label(self.buy_dis, text='Dis % :', font=('Ariel', 13, 'bold'), width=9)
            # self.get1.place(x=130, y=580)
            #
            # self.nament = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.nament.place(x=230, y=500)
            #
            # self.priceent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.priceent.place(x=230, y=540)
            #
            # self.disent = Entry(self.buy_dis, font=('Ariel', 13, 'bold'), width=18)
            # self.disent.place(x=230, y=580)
            #
            # self.addbill = Button(self.buy_dis, text="Calculate", command=self.bills, font=('Ariel', 11, 'bold'))
            # self.addbill.place(x=390, y=620)

        else:
            messagebox.showinfo("No Selection",'Select First!!')

    def start_tree(self):
        all_items= self.db.all_books()

        self.add_tree.delete(*self.add_tree.get_children())
        for i in all_items:
            self.add_tree.insert("", "end", text=i[0], value=(i[1], i[2], i[3], i[4], i[5]))
            self.add_tree.bind("<Double-1>", self.select_item)

    def tree_byname(self):
        name = self.search_ent.get()
        all_items= self.db.searchbyname(name)

        self.add_tree.delete(*self.add_tree.get_children())
        for i in all_items:
            self.add_tree.insert("", "end", text=i[0], value=(i[1], i[2], i[3], i[4], i[5]))
            self.add_tree.bind("<Double-1>", self.select_item)

    def tree_bywriter(self):
        writer = self.search_ent.get()
        all_items= self.db.searchbywriter(writer)

        self.add_tree.delete(*self.add_tree.get_children())
        for i in all_items:
            self.add_tree.insert("", "end", text=i[0], value=(i[1], i[2], i[3], i[4], i[5]))
            self.add_tree.bind("<Double-1>", self.select_item)


    def tree_bypublisher(self):
        publisher = self.search_ent.get()
        all_items= self.db.searchbypublisher(publisher)

        self.add_tree.delete(*self.add_tree.get_children())
        for i in all_items:
            self.add_tree.insert("", "end", text=i[0], value=(i[1], i[2], i[3], i[4], i[5]))
            self.add_tree.bind("<Double-1>", self.select_item)


    def tree_bycatagory(self):
        catagory = self.search_ent.get()
        all_items= self.db.searchbycatagory(catagory)

        self.add_tree.delete(*self.add_tree.get_children())
        for i in all_items:
            self.add_tree.insert("", "end", text=i[0], value=(i[1], i[2], i[3], i[4], i[5]))
            self.add_tree.bind("<Double-1>", self.select_item)

    def select_item(self, event):
        sel_row = self.add_tree.selection()[0]
        sel_item = self.add_tree.item(sel_row)
        self.update_index = self.add_tree.item(sel_row, 'text')
        selected_data = self.add_tree.item(sel_row, 'values')
        self.nament.delete(0, 'end')
        self.nament.insert(0, selected_data[0])
        self.priceent.delete(0, 'end')
        self.priceent.insert(0, selected_data[2])

    def bills(self):
        discount=self.disent.get()

        if discount == '':
            messagebox.showerror('Error','Enter discount percentage first!')

        else:
            money = float(self.priceent.get())
            discount = float(self.disent.get())

            self.addbilllbl = Label(self.buy_dis, text='TOTAL: ',font=('Ariel', 15, 'bold'),fg='dark green')
            self.addbilllbl.place(x=680, y=620)


            self.addbillent = Entry(self.buy_dis, width=10, bg='black',fg='white',font=('Ariel', 15, 'bold'))
            self.addbillent.place(x=770, y=620)

            dis= discount/100
            tt= money * dis

            global total
            total= money - tt
            print(total)

            self.addbillent.insert(0, total)

            self.bill_button= Button(self.buy_dis, bg='blue', fg='white', command=self.bill_gui, text='Add to Bills',font=('Ariel', 11, 'bold'))
            self.bill_button.place(x=1100,y=618)




    def bill_gui(self):

        self.bill_dis = Tk()
        self.bill_dis.title('BILLING')
        self.bill_dis.geometry('600x350')
        self.bill_dis.configure(bg='sky blue2')

        self.lbl_ = Label(self.bill_dis, text='ORIENTAL BOOK HOUSE', fg='maroon', bg='sky blue2',font=('Ariel', 15, 'bold'))
        self.lbl_.place(x=200, y=30)

        self._lbl = Label(self.bill_dis, text='Estimate Bill', bg='sky blue2', font=('Ariel', 12, 'bold'))
        self._lbl.place(x=270, y=70)

        self.date_lbl=Label(self.bill_dis, text='Date: ', bg='sky blue2')
        self.date_lbl.place(x=445, y=100)

        self.date_ent = Entry(self.bill_dis, width=10)
        self.date_ent.place(x=480, y=100)

        self.cust_name = Label(self.bill_dis, text="Customer's Name ", bg='sky blue2',font=('Ariel', 8, 'bold') )
        self.cust_name.place(x=30, y=140)

        self.cust_phone = Label(self.bill_dis, text="Customer's Phone ", bg='sky blue2', font=('Ariel', 8, 'bold'))
        self.cust_phone.place(x=150, y=140)

        self.booki = Label(self.bill_dis, text="Book ", bg='sky blue2', font=('Ariel', 8, 'bold'))
        self.booki.place(x=295, y=140)

        self.prici = Label(self.bill_dis, text="Price ", bg='sky blue2', font=('Ariel', 8, 'bold'))
        self.prici.place(x=390, y=140)

        self.disc = Label(self.bill_dis, text="discount %", bg='sky blue2', font=('Ariel', 8, 'bold'))
        self.disc.place(x=430, y=140)

        self.amt = Label(self.bill_dis, text="Tot. Amount", bg='sky blue2', font=('Ariel', 8, 'bold'))
        self.amt.place(x=500, y=140)

        self.cus_name=Entry(self.bill_dis, width=15)
        self.cus_name.place(x=30, y=170)

        self.cus_phn = Entry(self.bill_dis, width=15)
        self.cus_phn.place(x=150, y=170)

        self.book_ent = Entry(self.bill_dis)
        self.book_ent.place(x=260, y=170)

        self.pr_ent = Entry(self.bill_dis,width=6)
        self.pr_ent.place(x=390, y=170)

        self.dis_ent = Entry(self.bill_dis,width=6)
        self.dis_ent.place(x=440, y=170)

        self.tot_ent = Entry(self.bill_dis,width=10)
        self.tot_ent.place(x=500, y=170)

        com=['Bipin','Kishan','Badri','Hari']
        self.billby = ttk.Combobox(self.bill_dis, font=('Ariel', 9),width=8, values=com)
        self.billby.place(x=420, y=270)

        self.billby_lbl=Label(self.bill_dis,text="Bill by: ")
        self.billby_lbl.place(x=360,y=270)


        self.st_button = Button(self.bill_dis, bg='black', fg='white',text='Store',command=self.billdb, font=('Ariel', 10, 'bold'))
        self.st_button.place(x=520, y=270)

        money = float(self.priceent.get())
        bookss=self.nament.get()
        discount = float(self.disent.get())
        today = date.today()
        #billcutter= self.app.getname()

        self.book_ent.insert(0, bookss)
        self.pr_ent.insert(0, money)
        self.dis_ent.insert(0, discount)
        self.tot_ent.insert(0, total)
        self.date_ent.insert(0,today)
        #self.billby.insert(0,billcutter)


        self.bill_dis.mainloop()

    def billdb(self):
        cus_name = self.cus_name.get()
        print(cus_name)
        cus_phone = self.cus_phn.get()
        booko = self.book_ent.get()
        #bookid----methoddatabase
        price = self.pr_ent.get()
        bookido = self.bookid(booko, price)
        discount = self.dis_ent.get()
        total = self.tot_ent.get()
        date = self.date_ent.get()
        book = self.book_ent.get()
        billby=self.billby.get()

        if cus_name == '' or cus_phone == '' or price == '' or discount == '' or total== ''or date == '' or billby=='':
            messagebox.showerror('Error', 'Fill all the entries !!')
            return False

        else:
            self.databill(cus_name,cus_phone,bookido,price,discount, total,date,book,billby)
            messagebox.showinfo("Done", "Bill Added 😊")


    def databill(self,cus_name,cus_phone,bookid,price,discount, total,date,book,billby):
        self.db = MyDb()
        qry = "INSERT INTO bill (cus_name, cus_phone, book, price, discount, total,date,bookname,bill_by) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        values = (cus_name,cus_phone,bookid,price,discount, total,date,book,billby)
        return self.db.iud(qry, values)


    def bookid(self,book, price):
        self.db = MyDb()
        book = self.book_ent.get()
        price = self.pr_ent.get()
        print(book)
        print(price)
        qry = '''select id from booksdetail where name=%s and price=%s'''
        values = (book,price)
        get= self.db.get_data_i(qry, values)
        print(type(get))
        bipin =int(get[0])
        print(type(bipin))
        return bipin


    def backbut(self):
        self.buy_dis.destroy()



# a=BuyBooks()
# a.buy_gui()
class AddBooks:
    def __init__(self):
        self.db = MyDb()
        self.update_index = ""

    def add_gui(self):
        try:
            self.display = Tk()
            self.display.title('ADD BOOKS')
            self.display.geometry('1920x1080+0+0')
            self.display.configure(bg='springgreen2')

            self.lbl111 = Label(self.display,
                                bg='Seagreen1',
                                width=600,
                                height=4,
                                font=('Ariel', 15, 'bold'))
            self.lbl111.place(x=1, y=20)

            self.lbl113 = Label(self.display,
                                bg='Seagreen1',
                                width=60,
                                height=24)
            self.lbl113.place(x=60, y=250)

            self.lbl115 = Label(self.display,
                                bg='Seagreen1',
                                width=68,
                                height=4)
            self.lbl115.place(x=1, y=160)

            self.lbl113 = Label(self.display,
                                bg='Seagreen1',
                                width=40,
                                height=4)
            self.lbl113.place(x=550, y=160)

            self.lbl112 = Label(self.display,
                                text='ORIENTAL BOOK HOUSE',
                                bg='Seagreen1',
                                fg='black',
                                font=('Ariel', 19, 'bold'))
            self.lbl112.place(x=560, y=30)

            self.lbl112 = Label(self.display,
                                text='Bagbazar, Kathmandu',
                                bg='Seagreen1',
                                fg='black',
                                font=('Ariel', 12, 'bold'))
            self.lbl112.place(x=620, y=75)

            self.top1_lbl = Label(self.display,
                                  bg='Seagreen1',
                                  text='Book Registry',
                                  font=('Ariel', 15, 'bold'))
            self.top1_lbl.place(x=190, y=180)

            self.add_name = Label(self.display,
                                  bg='Seagreen1',
                                  text='Book Name',
                                  font=('Ariel', 10, 'bold'))
            self.add_name.place(x=140, y=270)

            self.ent01 = Entry(self.display, font=('Ariel', 10))
            self.ent01.place(x=240, y=270)

            self.add_writer = Label(self.display,
                                    bg='Seagreen1',
                                    text='Writer',
                                    font=('Ariel', 10, 'bold'))
            self.add_writer.place(x=140, y=310)

            self.wn_ent = Entry(self.display, font=('Ariel', 10))
            self.wn_ent.place(x=240, y=310)

            self.add_pub = Label(self.display,
                                 bg='Seagreen1',
                                 text='Publisher',
                                 font=('Ariel', 10, 'bold'))
            self.add_pub.place(x=140, y=350)

            self.pub_ent = Entry(self.display, font=('Ariel', 10))
            self.pub_ent.place(x=240, y=350)

            self.add_price = Label(self.display,
                                   bg='Seagreen1',
                                   text='Price',
                                   font=('Ariel', 10, 'bold'))
            self.add_price.place(x=140, y=390)

            self.price_ent = Entry(self.display, font=('Ariel', 10))
            self.price_ent.place(x=240, y=390)

            self.add_cat = Label(self.display,
                                 bg='Seagreen1',
                                 text='Category',
                                 font=('Ariel', 10, 'bold'))
            self.add_cat.place(x=140, y=430)

            self.cat = ttk.Combobox(self.display,
                                    text="Category",
                                    font=('Ariel', 10))
            self.cat.set('--choose category--')
            self.cat['values'] = self.combo()
            self.cat.place(x=240, y=430)

            self.reg_book = Label(self.display,
                                  bg='Seagreen1',
                                  text='Added Piece',
                                  font=('Ariel', 10, 'bold'))
            self.reg_book.place(x=140, y=470)

            self.pie_ent = Entry(self.display, font=('Ariel', 10))
            self.pie_ent.place(x=240, y=470)

            self.rem_book = Label(self.display,
                                  bg='Seagreen1',
                                  text='Remain Piece',
                                  font=('Ariel', 10, 'bold'))
            self.rem_book.place(x=140, y=510)

            self.rem_ent = Entry(self.display, font=('Ariel', 10))
            self.rem_ent.place(x=240, y=510)

            self.ad_book = Button(self.display,
                                  text='Add',
                                  command=self.addbooks_hand,
                                  bg='black',
                                  fg='white',
                                  width=6,
                                  font=('Ariel', 11, 'bold'))
            self.ad_book.place(x=140, y=570)

            self.up_book = Button(self.display,
                                  text='Update',
                                  bg='black',
                                  fg='white',
                                  command=self.update_item,
                                  font=('Ariel', 11, 'bold'))
            self.up_book.place(x=240, y=570)

            self.del_book = Button(self.display,
                                   text='Delete',
                                   bg='black',
                                   fg='white',
                                   command=self.delete_item,
                                   font=('Ariel', 11, 'bold'))
            self.del_book.place(x=340, y=570)

            self.top_lbl = Label(self.display,
                                 bg='Seagreen1',
                                 text='Recently added books',
                                 font=('Ariel', 15, 'bold'))
            self.top_lbl.place(x=570, y=180)

            self.add_tree = ttk.Treeview(self.display,
                                         column=('n', 'w', 'p', 'm', 'c', 'e',
                                                 'r'),
                                         height=17)
            self.add_tree.place(x=550, y=250)
            self.add_tree['show'] = 'headings'
            self.add_tree.column('n', width=200)
            self.add_tree.column('w', width=100)
            self.add_tree.column('p', width=100)
            self.add_tree.column('m', width=100)
            self.add_tree.column('c', width=100)
            self.add_tree.column('e', width=70)
            self.add_tree.column('r', width=70)
            self.add_tree.heading('n', text='Book')
            self.add_tree.heading('w', text='Writer')
            self.add_tree.heading('p', text='Price')
            self.add_tree.heading('m', text='Publisher')
            self.add_tree.heading('c', text='Catagory')
            self.add_tree.heading('e', text='Reg-piece')
            self.add_tree.heading('r', text='Rem-piece')

            self.show_items_in_tree()

            self.ser_book = Button(self.display,
                                   bg='yellow',
                                   text='Search',
                                   font=('Ariel', 10, 'bold'),
                                   command=self.searchbook)
            self.ser_book.place(x=1270, y=85)

            self.search_book = Entry(self.display,
                                     text='Search',
                                     font=('Ariel', 10))
            self.search_book.place(x=1110, y=90)

            self.display.mainloop()
        except Exception as e:
            print(e)

    def addbooks_hand(self):
        try:
            name = self.ent01.get()
            print(name)
            writer = self.wn_ent.get()
            print(writer)
            price = self.price_ent.get()
            print(price)
            publisher = self.pub_ent.get()
            print(publisher)
            reg = self.pie_ent.get()
            rem = self.rem_ent.get()

            catagory = self.cat.get()

            if name == '' or writer == '' or price == '' or publisher == '' or catagory == '' or reg == '' or rem == '':
                messagebox.showerror('Error', 'Fill all the entries !!')
                return False
            elif not price.isdigit():
                messagebox.showerror("Error", "Invalid Price")
                return False

            else:
                self.add_books1(name, writer, price, publisher, catagory, reg,
                                rem)
                messagebox.showinfo("BOOKS", "BOOK Added 😊")
                self.show_items_in_tree()

        except Exception as e:
            print(e)

    def add_books1(self, name, writer, price, publisher, catagory, reg, rem):
        if name == '' or writer == '' or price == '' or publisher == '' or catagory == '' or reg == '' or rem == '':
            return False
        elif not price.isdigit():
            return False
        else:
            qry = "INSERT INTO booksdetail (name, writer, price, publisher,catagory,ent_book, rem_book) VALUES (%s,%s,%s,%s,%s,%s,%s)"
            values = (name, writer, price, publisher, catagory, reg, rem)
            return self.db.iud(qry, values)

    def combo(self):
        qry = '''SELECT catagory FROM catq'''
        combox = self.db.get_data(qry)
        return combox

    def show_items_in_tree(self):
        all_items = self.show_books()
        self.add_tree.delete(*self.add_tree.get_children())
        for i in all_items:
            self.add_tree.insert("",
                                 "end",
                                 text=i[0],
                                 value=(i[1], i[2], i[3], i[4], i[5], i[6],
                                        i[7]))
        self.add_tree.bind("<Double-1>", self.select_item)

    def show_books(self):
        qry = "SELECT * FROM booksdetail"
        all_books = self.db.get_data(qry)
        return all_books

    def select_item(self, event):
        sel_row = self.add_tree.selection()[0]
        sel_item = self.add_tree.item(sel_row)
        self.update_index = self.add_tree.item(sel_row, 'text')
        selected_data = self.add_tree.item(sel_row, 'values')
        self.ent01.delete(0, 'end')
        self.ent01.insert(0, selected_data[0])
        self.wn_ent.delete(0, 'end')
        self.wn_ent.insert(0, selected_data[1])
        self.price_ent.delete(0, 'end')
        self.price_ent.insert(0, selected_data[2])
        self.cat.delete(0, 'end')
        self.cat.insert(0, selected_data[4])
        self.pub_ent.delete(0, 'end')
        self.pub_ent.insert(0, selected_data[3])
        self.pie_ent.delete(0, 'end')
        self.pie_ent.insert(0, selected_data[5])
        self.rem_ent.delete(0, 'end')
        self.rem_ent.insert(0, selected_data[6])

    def update_books(self, index, name, writer, price, publisher, catagory,
                     reg, rem):
        try:
            qry = "UPDATE booksdetail SET name = %s, writer = %s, price = %s, publisher=%s, catagory=%s, ent_book =%s, rem_book=%s WHERE id = %s"
            values = (name, writer, price, publisher, catagory, reg, rem,
                      index)
            self.db.iud(qry, values)
            return True
        except Exception as e:
            print(e)
            return False

    def update_item(self):
        name = self.ent01.get()
        writer = self.wn_ent.get()
        price = self.price_ent.get()
        publisher = self.pub_ent.get()
        catagory = self.cat.get()
        reg = self.pie_ent.get()
        rem = self.rem_ent.get()

        if self.update_index == "":
            messagebox.showerror("Error", "Select Item first")
        elif not name == '' or writer == '' or price == '' or publisher == '' or catagory == '' or reg == '' or rem == '':

            if self.update_books(int(self.update_index), name, writer, price,
                                 publisher, catagory, reg, rem):
                messagebox.showinfo("Item", "Item Updated")
                self.show_items_in_tree()
                print(self.update_index)
            else:
                messagebox.showerror("Error", "Item can not be Updated")

    def delete_books(self, id):
        try:
            qry = "DELETE FROM booksdetail WHERE id = %s"
            values = (id)
            self.db.iud(qry, values)
            return True
        except Exception as e:
            print(e)
            return False

    def delete_item(self):

        name = self.ent01.get()
        writer = self.wn_ent.get()
        price = self.price_ent.get()
        publisher = self.pub_ent.get()
        catagory = self.cat.get()
        reg = self.pie_ent.get()
        rem = self.rem_ent.get()

        if self.update_index == "":
            messagebox.showerror("Error", "Select Item first")
        elif not name == '' or writer == '' or price == '' or publisher == '' or catagory == '' or reg == '' or rem == '':

            self.delete_books(self.del_help(name, publisher))
            messagebox.showinfo("Success", "Delete success")
            self.show_items_in_tree()

    def del_help(self, name, publisher):
        name = self.ent01.get()
        publisher = self.pub_ent.get()

        qry = '''select id from booksdetail where name= %s and publisher=%s'''
        values = (name, publisher)
        gett = self.db.get_data_i(qry, values)
        print(gett)
        return gett

    def searchbook(self):
        self.db = MyDb()
        searchbook = self.search_book.get()

        if searchbook == '':
            messagebox.showerror('Empty', 'Insert some keyword please!')
        else:
            qry = "SELECT * FROM booksdetail WHERE name LIKE '" + searchbook + "%'"
            values = (searchbook)
            result = self.db.get_data_p(qry, values)

            self.add_tree.delete(*self.add_tree.get_children())
            for i in result:
                self.add_tree.insert("",
                                     "end",
                                     text=i[0],
                                     value=(i[1], i[2], i[3], i[4], i[5], i[6],
                                            i[7]))
                self.add_tree.bind("<Double-1>", self.select_item)