225 lines
6.5 KiB
Python
225 lines
6.5 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox
|
|
from database import connect
|
|
|
|
class AdminMenu:
|
|
def __init__(self, root):
|
|
self.root = root
|
|
self.frame = tk.Frame(root)
|
|
self.frame.pack(fill="both", expand=True)
|
|
|
|
# =====================
|
|
# JUDUL
|
|
# =====================
|
|
tk.Label(
|
|
self.frame,
|
|
text="ADMIN - KELOLA MENU",
|
|
font=("Arial", 18, "bold")
|
|
).pack(pady=15)
|
|
|
|
# =====================
|
|
# FORM INPUT
|
|
# =====================
|
|
form = tk.Frame(self.frame)
|
|
form.pack(pady=10)
|
|
|
|
tk.Label(form, text="Nama Menu", width=15, anchor="w")\
|
|
.grid(row=0, column=0, pady=5)
|
|
self.nama = tk.Entry(form, width=30)
|
|
self.nama.grid(row=0, column=1)
|
|
|
|
tk.Label(form, text="Kategori", width=15, anchor="w")\
|
|
.grid(row=1, column=0, pady=5)
|
|
self.kategori = tk.StringVar()
|
|
tk.OptionMenu(
|
|
form,
|
|
self.kategori,
|
|
"Makanan",
|
|
"Minuman",
|
|
"Dessert"
|
|
).grid(row=1, column=1, sticky="w")
|
|
self.kategori.set("Makanan")
|
|
|
|
tk.Label(form, text="Harga", width=15, anchor="w")\
|
|
.grid(row=2, column=0, pady=5)
|
|
self.harga = tk.Entry(form, width=30)
|
|
self.harga.grid(row=2, column=1)
|
|
|
|
tk.Label(form, text="Path Gambar", width=15, anchor="w")\
|
|
.grid(row=3, column=0, pady=5)
|
|
self.gambar = tk.Entry(form, width=30)
|
|
self.gambar.grid(row=3, column=1)
|
|
|
|
# =====================
|
|
# BUTTON CRUD
|
|
# =====================
|
|
btn_frame = tk.Frame(self.frame)
|
|
btn_frame.pack(pady=10)
|
|
|
|
tk.Button(btn_frame, text="Tambah", width=10,
|
|
bg="#d1e7dd", command=self.tambah).grid(row=0, column=0, padx=5)
|
|
|
|
tk.Button(btn_frame, text="Update", width=10,
|
|
bg="#cfe2ff", command=self.update).grid(row=0, column=1, padx=5)
|
|
|
|
tk.Button(btn_frame, text="Hapus", width=10,
|
|
bg="#f8d7da", command=self.hapus).grid(row=0, column=2, padx=5)
|
|
|
|
tk.Button(btn_frame, text="Refresh", width=10,
|
|
command=self.load_menu).grid(row=0, column=3, padx=5)
|
|
|
|
tk.Button(btn_frame, text="Logout", width=10,
|
|
command=self.logout).grid(row=0, column=4, padx=5)
|
|
|
|
# =====================
|
|
# FILTER
|
|
# =====================
|
|
filter_frame = tk.Frame(self.frame)
|
|
filter_frame.pack(pady=5)
|
|
|
|
tk.Label(filter_frame, text="Filter Kategori:").pack(side=tk.LEFT)
|
|
self.filter_kategori = tk.StringVar()
|
|
tk.OptionMenu(
|
|
filter_frame,
|
|
self.filter_kategori,
|
|
"Semua",
|
|
"Makanan",
|
|
"Minuman",
|
|
"Dessert",
|
|
command=self.load_menu
|
|
).pack(side=tk.LEFT)
|
|
self.filter_kategori.set("Semua")
|
|
|
|
# =====================
|
|
# LIST MENU
|
|
# =====================
|
|
self.listbox = tk.Listbox(self.frame, width=80, height=10)
|
|
self.listbox.pack(pady=10)
|
|
self.listbox.bind("<<ListboxSelect>>", self.pilih_menu)
|
|
|
|
self.menu_data = []
|
|
self.load_menu()
|
|
|
|
# =====================
|
|
# LOAD MENU + FILTER
|
|
# =====================
|
|
def load_menu(self, *_):
|
|
self.listbox.delete(0, tk.END)
|
|
db = connect()
|
|
cur = db.cursor()
|
|
|
|
if self.filter_kategori.get() == "Semua":
|
|
cur.execute("SELECT * FROM menu")
|
|
else:
|
|
cur.execute(
|
|
"SELECT * FROM menu WHERE kategori=?",
|
|
(self.filter_kategori.get(),)
|
|
)
|
|
|
|
self.menu_data = cur.fetchall()
|
|
db.close()
|
|
|
|
for m in self.menu_data:
|
|
self.listbox.insert(
|
|
tk.END,
|
|
f"{m[0]:<3} | {m[1]:<20} | {m[2]:<10} | Rp {m[3]:,.0f}"
|
|
)
|
|
|
|
# =====================
|
|
# PILIH MENU
|
|
# =====================
|
|
def pilih_menu(self, event):
|
|
idx = self.listbox.curselection()
|
|
if not idx:
|
|
return
|
|
|
|
menu = self.menu_data[idx[0]]
|
|
self.clear_form()
|
|
|
|
self.nama.insert(0, menu[1])
|
|
self.kategori.set(menu[2])
|
|
self.harga.insert(0, menu[3])
|
|
self.gambar.insert(0, menu[4])
|
|
|
|
# =====================
|
|
# CRUD
|
|
# =====================
|
|
def tambah(self):
|
|
if not self.nama.get() or not self.harga.get():
|
|
messagebox.showwarning("Error", "Data belum lengkap")
|
|
return
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute(
|
|
"INSERT INTO menu(nama,kategori,harga,gambar) VALUES (?,?,?,?)",
|
|
(
|
|
self.nama.get(),
|
|
self.kategori.get(),
|
|
float(self.harga.get()),
|
|
self.gambar.get()
|
|
)
|
|
)
|
|
db.commit()
|
|
db.close()
|
|
|
|
messagebox.showinfo("Sukses", "Menu berhasil ditambahkan")
|
|
self.load_menu()
|
|
self.clear_form()
|
|
|
|
def update(self):
|
|
idx = self.listbox.curselection()
|
|
if not idx:
|
|
messagebox.showwarning("Pilih", "Pilih menu dulu")
|
|
return
|
|
|
|
menu_id = self.menu_data[idx[0]][0]
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute(
|
|
"UPDATE menu SET nama=?, kategori=?, harga=?, gambar=? WHERE id=?",
|
|
(
|
|
self.nama.get(),
|
|
self.kategori.get(),
|
|
float(self.harga.get()),
|
|
self.gambar.get(),
|
|
menu_id
|
|
)
|
|
)
|
|
db.commit()
|
|
db.close()
|
|
|
|
messagebox.showinfo("Sukses", "Menu berhasil diupdate")
|
|
self.load_menu()
|
|
|
|
def hapus(self):
|
|
idx = self.listbox.curselection()
|
|
if not idx:
|
|
messagebox.showwarning("Pilih", "Pilih menu dulu")
|
|
return
|
|
|
|
menu_id = self.menu_data[idx[0]][0]
|
|
|
|
if messagebox.askyesno("Konfirmasi", "Yakin hapus menu ini?"):
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute("DELETE FROM menu WHERE id=?", (menu_id,))
|
|
db.commit()
|
|
db.close()
|
|
|
|
messagebox.showinfo("Sukses", "Menu berhasil dihapus")
|
|
self.load_menu()
|
|
self.clear_form()
|
|
|
|
def clear_form(self):
|
|
self.nama.delete(0, tk.END)
|
|
self.harga.delete(0, tk.END)
|
|
self.gambar.delete(0, tk.END)
|
|
self.kategori.set("Makanan")
|
|
|
|
def logout(self):
|
|
self.frame.destroy()
|
|
from main import LoginScreen
|
|
LoginScreen(self.root)
|