import { useState, useEffect } from 'react'; import { Icons } from './Icons'; import { API } from '../services/api'; export default function AdminDashboard({ stock, orders, config, refreshData, notify, promptConfirm }: any) { const [tab, setTab] = useState<'orders' | 'stock' | 'settings'>('orders'); const [newAdminCode, setNewAdminCode] = useState(''); const [settingsForm, setSettingsForm] = useState(null); useEffect(() => { if (config && !settingsForm) { setSettingsForm({ finalDepositDate: config.finalDepositDate || '', cookDay: config.cookDay || '', payIdInfo: config.payIdInfo || '', maxTotalWeight: config.maxTotalWeight !== undefined && config.maxTotalWeight !== null ? config.maxTotalWeight : 100, maxItemWeight: config.maxItemWeight !== undefined && config.maxItemWeight !== null ? config.maxItemWeight : 50 }); } }, [config, settingsForm]); const handleSaveSettings = async () => { if (!settingsForm) return; await API.updateConfig(settingsForm); notify("Settings Saved"); setSettingsForm(null); refreshData(); }; const totalRevenue = orders.reduce((acc: number, order: any) => acc + (order.estimatedTotal || 0), 0); const totalKg = orders.reduce((acc: number, order: any) => acc + (order.totalWeight || 0), 0); const activeRequests = orders.length; const handleUpdatePassword = async () => { if (newAdminCode.length < 4) { notify("Code must be at least 4 characters"); return; } await API.updateConfig({ adminCode: newAdminCode }); setNewAdminCode(''); notify("Admin Password Updated"); refreshData(); }; const addNewStockItem = async () => { const newItem = { name: "New Meat Item", category: "Beef", price: 20, unit: "kg", available: false, description: "Description here", bulk1Threshold: 0, bulk1Price: 0, bulk2Threshold: 0, bulk2Price: 0 }; await API.updateStock(newItem); refreshData(); notify("New Item Added"); }; const updateStockItem = async (id: number, updates: any) => { const currentItem = stock.find((s: any) => s.id === id) || { id }; await API.updateStock({ ...currentItem, ...updates }); refreshData(); notify("Stock updated"); }; const deleteStockItem = (id: number) => { promptConfirm("Permanently delete this item from stock?", async () => { await API.deleteStock(id); refreshData(); notify("Item deleted"); }); }; const clearAllOrders = () => { promptConfirm("WARNING: This will delete ALL current order history. Proceed?", async () => { await API.clearOrders(); refreshData(); notify("All Orders Cleared"); }); }; const resetDatabase = () => { promptConfirm("Wipe all data and reset to defaults?", async () => { await API.resetDB(); refreshData(); notify("Database Reset to Defaults"); }); }; return (
{tab === 'stock' && (

Stock Configuration

Edit names, prices, and descriptions

{stock.map((item: any) => ( updateStockItem(item.id, data)} onDelete={() => deleteStockItem(item.id)} /> ))}
)} {tab === 'orders' && (

Est. Revenue

${totalRevenue.toFixed(2)}

Total Prep (kg)

{totalKg}kg

Requests

{activeRequests}

Incoming List

{orders.length === 0 ? (

No active orders. Time to smoke some meat!

) : (
{orders.map((order: any) => (
{order.orderNumber || `#${order.id}`}

{order.name}

{order.phone}

{order.items.map((item: any, idx: number) => (
{item.qty}{item.unit} {item.name} ${(item.lineTotal || 0).toFixed(2)}
))}

{new Date(order.timestamp).toLocaleString('en-AU')}

{order.totalWeight}kg Weight

Estimated Total

${order.estimatedTotal?.toFixed(2)}

30% Deposit: ${(order.estimatedTotal * 0.3).toFixed(2)}

))}
)}
)} {tab === 'settings' && (

Admin Settings

{settingsForm ? (

Scheduling & Deadlines

setSettingsForm({ ...settingsForm, finalDepositDate: e.target.value })} className="w-full bg-stone-900 border border-stone-700 rounded p-3 text-white focus:border-red-600 outline-none" />
setSettingsForm({ ...settingsForm, cookDay: e.target.value })} className="w-full bg-stone-900 border border-stone-700 rounded p-3 text-white focus:border-red-600 outline-none" />
setSettingsForm({ ...settingsForm, payIdInfo: e.target.value })} className="w-full bg-stone-900 border border-stone-700 rounded p-3 text-white focus:border-red-600 outline-none" />

Order Limits

setSettingsForm({ ...settingsForm, maxTotalWeight: parseFloat(e.target.value) || 0 })} className="w-full bg-stone-900 border border-stone-700 rounded p-3 text-white focus:border-red-600 outline-none" />
setSettingsForm({ ...settingsForm, maxItemWeight: parseFloat(e.target.value) || 0 })} className="w-full bg-stone-900 border border-stone-700 rounded p-3 text-white focus:border-red-600 outline-none" />
) : (
Loading settings...
)}
setNewAdminCode(e.target.value)} className="w-full bg-stone-900 border border-stone-700 rounded p-3 text-white mb-4 focus:border-red-600 outline-none" />

Danger Zone

)}
); } function AdminStockItem({ item, onUpdate, onDelete }: any) { const [isEditing, setIsEditing] = useState(false); const [editData, setEditData] = useState({}); useEffect(() => { if (!isEditing) { setEditData({ name: item.name || "", category: item.category || "", description: item.description || "", price: item.price || 0, unit: item.unit || "kg", bulk1Threshold: item.bulk1Threshold || 0, bulk1Price: item.bulk1Price || 0, bulk2Threshold: item.bulk2Threshold || 0, bulk2Price: item.bulk2Price || 0 }); } }, [item, isEditing]); const handleSave = () => { onUpdate({ ...editData, price: parseFloat(editData.price), bulk1Threshold: parseFloat(editData.bulk1Threshold), bulk1Price: parseFloat(editData.bulk1Price), bulk2Threshold: parseFloat(editData.bulk2Threshold), bulk2Price: parseFloat(editData.bulk2Price) }); setIsEditing(false); }; return (
{!isEditing ? (

{item.name}

{item.description}

) : (
setEditData({ ...editData, name: e.target.value })} className="bg-black text-white border border-stone-700 rounded px-2 py-1 font-bold w-full" placeholder="Item Name" />