'''
آموزشگاه کامپیوتر راهکار تبریز
04135574231 - 09146585123
www.rahkar01.ir
پیشنهاد میکنیم سورس برنامه را خودتان چند بار صفر تا صد پیاده سازی کنید
تا برای آزمون عملی به مشکل نخورید
از حفظ کردن برنامه خودداری کنید و با مطالعه ویدئو و یادداشت برداری
برای خودتان منطق برنامه را درک کنید
درصورت نیاز به هرگونه توضیح تکمیلی یا آموزش بهتر با ما تماس بگیرید
'''

from tkinter import *
import sqlite3
global kod
conn = sqlite3.connect('market.db')
curs = conn.cursor()
curs.execute('create table if not exists market (id integer primary key autoincrement, namkala text, kharid integer, forosh integer , tedad integer)')

def add():
    enam=e1.get();      ekharid = e2.get();     eforosh=e3.get();       etedad=e4.get()
    curs.execute('insert into market (namkala, kharid, forosh, tedad) values (?, ? , ? , ?)' , (enam, ekharid, eforosh, etedad))
    conn.commit()
    e1.delete(0,END);       e2.delete(0,END);       e3.delete(0,END);       e4.delete(0,END);       e1.focus_set()
    show()

def show():
    mylist.delete(0,END)
    curs.execute('select * from market')
    info = curs.fetchall()
    for i in info:
        mylist.insert(END, i)

def search():
    shart=[];       megdar=[]
    enam=e1.get();      ekharid=e2.get();       eforosh=e3.get();       etedad=e4.get()
    mylist.delete(0,END)
    if enam:
        shart.append('namkala=?');      megdar.append(enam)
    if ekharid:
        shart.append('kharid=?');       megdar.append(ekharid)
    if eforosh:
        shart.append('forosh=?');       megdar.append(eforosh)
    if etedad:
        shart.append('tedad=?');        megdar.append(etedad)
    text='select * from market where ' + ' and '.join(shart)

    if enam or eforosh or ekharid or etedad:
        curs.execute(text , megdar)
    else:
        curs.execute('select * from market')
    
    info = curs.fetchall()
    for i in info:
        mylist.insert(END, i)


def entekhab(event):
    global kod
    satrno = mylist.curselection()
    matn = mylist.get(satrno)
    kod=matn[0]; 
    e1.delete(0,END);       e2.delete(0,END);       e3.delete(0,END);       e4.delete(0,END)
    e1.insert(0,matn[1]);       e2.insert(0, matn[2]);      e3.insert(0,matn[3]);       e4.insert(0, matn[4])


def hazf():
    curs.execute('delete from market where id=?' , (kod,))
    conn.commit()
    show()

def virayesh():
    enam=e1.get();      ekharid = e2.get();     eforosh=e3.get();       etedad=e4.get()
    curs.execute('update market set namkala=? , kharid=? , forosh=? , tedad=? where id=?' , (enam , ekharid , eforosh , etedad , kod))
    conn.commit()
    show()

def close():
    conn.close()
    win.destroy()

win = Tk()
win.title('مدیریت فروشگاه')
win.geometry('600x300')


l1 = Label(win , text='نام کالا').place(x=30 , y=20)
l2 = Label(win , text='قیمت خرید').place(x=300 , y=20)
l3 = Label(win , text='قیمت فروش').place(x=30 , y=50)
l4 = Label(win , text='تعداد').place(x=300 , y=50)

e1 = Entry(win);        e1.place(x=110, y=20)
e2 = Entry(win);        e2.place(x=370,y=20)
e3 = Entry(win);        e3.place(x=110, y=50)
e4 = Entry(win);        e4.place(x=370,y=50)

mylist = Listbox(win);      mylist.place(x=20 , y=80 , width=300)
show()
mylist.bind('<<ListboxSelect>>' , entekhab)

b1=Button(win , text='اضافه کردن' , command=add).place(x=400 , y=80 ,width=100)
b2=Button(win , text='جستجو' , command=search).place(x=400 , y=110 ,width=100)
b3=Button(win , text='حذف' ,command=hazf ).place(x=400 , y=140 ,width=100)
b4=Button(win , text='ویرایش' , command=virayesh).place(x=400 , y=170 ,width=100)
b5=Button(win , text='بستن' , command=close).place(x=400 , y=200 ,width=100)

mainloop()