"use client"; import { useEffect, useState } from "react"; import { Plus, Pencil, Trash2, X, Package, Eye, } from "lucide-react"; import { api } from "@/lib/api"; export default function BrandsPanel() { const empty = { name: "", logo_url: "", description: "", }; const [brands, setBrands] = useState([]); const [show, setShow] = useState(false); const [editing, setEditing] = useState(null); const [form, setForm] = useState(empty); const [logoFile, setLogoFile] = useState(null); const [viewOpen, setViewOpen] = useState(false); const [brandProducts, setBrandProducts] = useState([]); const [brandName, setBrandName] = useState(""); async function loadBrands() { try { const data = await api("/brands"); setBrands(data); } catch (err) { console.error(err); } } useEffect(() => { loadBrands(); }, []); function openAdd() { setEditing(null); setForm(empty); setShow(true); } async function viewProducts(brand: any) { try { const data = await api(`/brands/${brand.id}`); setBrandName(data.name); setBrandProducts(data.products || []); setViewOpen(true); } catch (err) { console.error(err); } } function openEdit(brand: any) { setEditing(brand); setForm({ name: brand.name ?? "", logo_url: brand.logo_url ?? "", description: brand.description ?? "", }); setShow(true); } async function uploadLogo() { if (!logoFile) return form.logo_url; const data = new FormData(); data.append("file", logoFile); const res = await fetch("http://localhost:8000/brands/upload-logo", { method: "POST", body: data, }); const json = await res.json(); return json.url; } async function save() { const logo_url = await uploadLogo(); const body = { ...form, logo_url, }; if (editing) { await api(`/brands/${editing.id}`, { method: "PUT", body: JSON.stringify(body), }); } else { await api("/brands", { method: "POST", body: JSON.stringify(body), }); } setShow(false); loadBrands(); } async function remove(id: string) { if (!confirm("Delete this brand?")) return; await api(`/brands/${id}`, { method: "DELETE", }); loadBrands(); } return (
{/* Header */}

Brands

Manage all organization brands.

{/* Table */}
{brands.length === 0 && ( )} {brands.map((brand) => ( ))}
Brand Description Products Actions
No brands found.
{brand.logo_url ? ( {brand.name} ) : ( )}
{brand.name}
{brand.description || "-"} {brand.product_count}
{/* view popup */} {viewOpen && (
{/* Header */}

{brandName} Products

Total Products : {brandProducts.length}

{/* Body */}
{brandProducts.length === 0 ? (
No Products Found
) : ( {brandProducts.map((p: any) => ( {/* Product Image */} {/* Product */} {/* Brand */} {/* SKU */} {/* QR Count */} {/* Status */} ))}
Image Product SKU QRs Status
{p.image_url ? ( {p.name} ) : (
)}
{p.name}
{p.sku || "-"} {p.qr_count} {p.status}
)}
{/* Footer */}
)} {/* Modal */} {show && (

{editing ? "Edit Brand" : "Add Brand"}

setForm({ ...form, name: e.target.value }) } className="w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-brand-500 focus:outline-none" />
{ if (e.target.files?.length) { setLogoFile(e.target.files[0]); } }} className="w-full rounded-lg border border-slate-300 px-3 py-2" />