173 lines
5.1 KiB
Python
173 lines
5.1 KiB
Python
import tkinter as tk
|
|
from database import connect
|
|
from tkinter import messagebox
|
|
|
|
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 (RAPI)
|
|
# =====================
|
|
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="Harga", width=15, anchor="w")\
|
|
.grid(row=1, column=0, pady=5)
|
|
self.harga = tk.Entry(form, width=30)
|
|
self.harga.grid(row=1, column=1)
|
|
|
|
tk.Label(form, text="Path Gambar", width=15, anchor="w")\
|
|
.grid(row=2, column=0, pady=5)
|
|
self.gambar = tk.Entry(form, width=30)
|
|
self.gambar.grid(row=2, column=1)
|
|
|
|
# =====================
|
|
# BUTTON CRUD
|
|
# =====================
|
|
btn_frame = tk.Frame(self.frame)
|
|
btn_frame.pack(pady=10)
|
|
|
|
tk.Button(btn_frame, text="Tambah", bg="#d1e7dd",
|
|
width=10, command=self.tambah).grid(row=0, column=0, padx=5)
|
|
|
|
tk.Button(btn_frame, text="Update", bg="#cfe2ff",
|
|
width=10, command=self.update).grid(row=0, column=1, padx=5)
|
|
|
|
tk.Button(btn_frame, text="Hapus", bg="#f8d7da",
|
|
width=10, 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)
|
|
|
|
# =====================
|
|
# LIST MENU
|
|
# =====================
|
|
tk.Label(self.frame, text="Daftar Menu", font=("Arial", 12, "bold"))\
|
|
.pack(pady=5)
|
|
|
|
self.listbox = tk.Listbox(self.frame, width=60, height=10)
|
|
self.listbox.pack(pady=5)
|
|
self.listbox.bind("<<ListboxSelect>>", self.pilih_menu)
|
|
|
|
self.menu_data = []
|
|
self.load_menu()
|
|
|
|
# =====================
|
|
# DATABASE FUNCTION
|
|
# =====================
|
|
def load_menu(self):
|
|
self.listbox.delete(0, tk.END)
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute("SELECT * FROM menu")
|
|
self.menu_data = cur.fetchall()
|
|
db.close()
|
|
|
|
for menu in self.menu_data:
|
|
self.listbox.insert(
|
|
tk.END,
|
|
f"{menu[0]} | {menu[1]} | Rp {menu[2]:,.0f}"
|
|
)
|
|
|
|
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.harga.insert(0, menu[2])
|
|
self.gambar.insert(0, menu[3])
|
|
|
|
# =====================
|
|
# CRUD
|
|
# =====================
|
|
def tambah(self):
|
|
if not self.nama.get() or not self.harga.get():
|
|
messagebox.showwarning("Error", "Nama dan Harga wajib diisi")
|
|
return
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute(
|
|
"INSERT INTO menu(nama,harga,gambar) VALUES (?,?,?)",
|
|
(self.nama.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 yang ingin diupdate")
|
|
return
|
|
|
|
menu_id = self.menu_data[idx[0]][0]
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute(
|
|
"UPDATE menu SET nama=?, harga=?, gambar=? WHERE id=?",
|
|
(self.nama.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 yang ingin dihapus")
|
|
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)
|
|
|
|
def logout(self):
|
|
self.frame.destroy()
|
|
from main import LoginScreen
|
|
LoginScreen(self.root)
|