126 lines
5.0 KiB
Python
126 lines
5.0 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox, filedialog
|
|
from database import connect
|
|
import os
|
|
import shutil
|
|
|
|
class AdminPage(tk.Frame): # Ganti nama class jadi AdminPage
|
|
def __init__(self, parent, controller):
|
|
super().__init__(parent)
|
|
self.controller = controller
|
|
self.selected_image_path = None
|
|
|
|
# --- 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: self.controller.show_frame("LoginPage")).pack(side="right", padx=10)
|
|
|
|
# --- Form Input (Kiri) ---
|
|
left_frame = tk.Frame(self, padx=10, pady=10)
|
|
left_frame.pack(side="left", fill="y")
|
|
|
|
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.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")
|
|
|
|
# Tombol Aksi
|
|
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)
|
|
|
|
# --- Tabel Menu (Kanan) ---
|
|
right_frame = tk.Frame(self, padx=10, pady=10)
|
|
right_frame.pack(side="right", fill="both", expand=True)
|
|
|
|
tk.Label(right_frame, text="Daftar Menu Database:", 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=[("Image Files", "*.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")
|
|
nama_file_asli = os.path.basename(self.selected_image_path)
|
|
destinasi = os.path.join("img", nama_file_asli)
|
|
shutil.copy(self.selected_image_path, destinasi)
|
|
final_image_path = destinasi
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
try:
|
|
cur.execute("INSERT INTO menu (nama, harga, stok, gambar) VALUES (?, ?, ?, ?)",
|
|
(nama, harga, stok if stok else 0, final_image_path))
|
|
db.commit()
|
|
messagebox.showinfo("Sukses", "Menu berhasil ditambahkan!")
|
|
self.entry_nama.delete(0, tk.END)
|
|
self.entry_harga.delete(0, tk.END)
|
|
self.entry_stok.delete(0, tk.END)
|
|
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:
|
|
messagebox.showwarning("Pilih", "Pilih menu dulu!")
|
|
return
|
|
|
|
id_menu = self.data_menu[idx[0]][0]
|
|
if messagebox.askyesno("Hapus", "Yakin hapus menu ini?"):
|
|
db = connect()
|
|
db.cursor().execute("DELETE FROM menu WHERE id=?", (id_menu,))
|
|
db.commit()
|
|
db.close()
|
|
self.update_data() |