129 lines
4.9 KiB
Python
129 lines
4.9 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox, filedialog
|
|
from database import connect
|
|
import os
|
|
import shutil
|
|
|
|
# GANTI NAMA CLASS JADI AdminPage
|
|
class AdminPage(tk.Frame):
|
|
def __init__(self, parent, controller):
|
|
super().__init__(parent)
|
|
self.controller = controller
|
|
self.selected_image_path = None
|
|
self.data_menu = []
|
|
|
|
# --- Header ---
|
|
top = tk.Frame(self, bg="#333")
|
|
top.pack(fill="x")
|
|
tk.Label(top, text="ADMIN DASHBOARD", font=("Arial", 16, "bold"), fg="white", bg="#333").pack(side="left", padx=10, pady=10)
|
|
tk.Button(top, text="Logout", bg="#ff6b6b", command=lambda: controller.show_frame("LoginPage")).pack(side="right", padx=10)
|
|
|
|
# --- Layout Kiri (Form) & Kanan (Tabel) ---
|
|
main_content = tk.Frame(self)
|
|
main_content.pack(fill="both", expand=True, padx=10, pady=10)
|
|
|
|
# --- KIRI ---
|
|
left_frame = tk.Frame(main_content)
|
|
left_frame.pack(side="left", fill="y", padx=10)
|
|
|
|
tk.Label(left_frame, text="Nama Menu:").pack(anchor="w")
|
|
self.entry_nama = tk.Entry(left_frame, width=30)
|
|
self.entry_nama.pack(pady=5)
|
|
|
|
tk.Label(left_frame, text="Harga:").pack(anchor="w")
|
|
self.entry_harga = tk.Entry(left_frame, width=30)
|
|
self.entry_harga.pack(pady=5)
|
|
|
|
tk.Label(left_frame, text="Stok Awal:").pack(anchor="w")
|
|
self.entry_stok = tk.Entry(left_frame, width=30)
|
|
self.entry_stok.insert(0, "100") # Default stok
|
|
self.entry_stok.pack(pady=5)
|
|
|
|
tk.Label(left_frame, text="Gambar:").pack(anchor="w")
|
|
self.btn_img = tk.Button(left_frame, text="Pilih Gambar", command=self.browse_image)
|
|
self.btn_img.pack(pady=5, anchor="w")
|
|
self.lbl_img_path = tk.Label(left_frame, text="Belum ada gambar", fg="gray", font=("Arial", 8))
|
|
self.lbl_img_path.pack(anchor="w")
|
|
|
|
btn_box = tk.Frame(left_frame, pady=20)
|
|
btn_box.pack()
|
|
tk.Button(btn_box, text="TAMBAH", bg="#4CAF50", fg="white", command=self.add_menu).grid(row=0, column=0, padx=5)
|
|
tk.Button(btn_box, text="HAPUS", bg="#f44336", fg="white", command=self.delete_menu).grid(row=0, column=1, padx=5)
|
|
|
|
# --- KANAN ---
|
|
right_frame = tk.Frame(main_content)
|
|
right_frame.pack(side="right", fill="both", expand=True)
|
|
|
|
tk.Label(right_frame, text="Daftar Menu:", font=("Arial", 10, "bold")).pack(anchor="w")
|
|
|
|
self.list_menu = tk.Listbox(right_frame)
|
|
self.list_menu.pack(fill="both", expand=True)
|
|
|
|
scrollbar = tk.Scrollbar(self.list_menu)
|
|
scrollbar.pack(side="right", fill="y")
|
|
self.list_menu.config(yscrollcommand=scrollbar.set)
|
|
scrollbar.config(command=self.list_menu.yview)
|
|
|
|
def update_data(self):
|
|
self.list_menu.delete(0, tk.END)
|
|
self.data_menu = []
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
cur.execute("SELECT id, nama, harga, stok FROM menu")
|
|
rows = cur.fetchall()
|
|
db.close()
|
|
|
|
for row in rows:
|
|
self.data_menu.append(row)
|
|
self.list_menu.insert(tk.END, f"{row[1]} - Rp {int(row[2])} (Stok: {row[3]})")
|
|
|
|
def browse_image(self):
|
|
filename = filedialog.askopenfilename(title="Pilih Gambar", filetypes=[("Images", "*.png;*.jpg;*.jpeg")])
|
|
if filename:
|
|
self.selected_image_path = filename
|
|
self.lbl_img_path.config(text=os.path.basename(filename), fg="black")
|
|
|
|
def add_menu(self):
|
|
nama = self.entry_nama.get()
|
|
harga = self.entry_harga.get()
|
|
stok = self.entry_stok.get()
|
|
|
|
if not nama or not harga:
|
|
messagebox.showwarning("Warning", "Nama dan Harga wajib diisi!")
|
|
return
|
|
|
|
final_image_path = "default.png"
|
|
if self.selected_image_path:
|
|
if not os.path.exists("img"): os.makedirs("img")
|
|
destinasi = os.path.join("img", os.path.basename(self.selected_image_path))
|
|
try:
|
|
shutil.copy(self.selected_image_path, destinasi)
|
|
final_image_path = destinasi
|
|
except:
|
|
pass
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
try:
|
|
cur.execute("INSERT INTO menu (nama, harga, stok, gambar) VALUES (?, ?, ?, ?)",
|
|
(nama, harga, stok, final_image_path))
|
|
db.commit()
|
|
messagebox.showinfo("Sukses", "Menu berhasil ditambahkan!")
|
|
self.update_data()
|
|
except Exception as e:
|
|
messagebox.showerror("Error", str(e))
|
|
finally:
|
|
db.close()
|
|
|
|
def delete_menu(self):
|
|
idx = self.list_menu.curselection()
|
|
if not idx: return
|
|
|
|
selected = self.data_menu[idx[0]]
|
|
if messagebox.askyesno("Hapus", f"Hapus {selected[1]}?"):
|
|
db = connect()
|
|
db.cursor().execute("DELETE FROM menu WHERE id=?", (selected[0],))
|
|
db.commit()
|
|
db.close()
|
|
self.update_data() |