// ============================================
// admin.jsx — Dashboard (v2 redesign)
// ============================================
function AdminPanel({
    quotations,
    invoices,
    onViewQuotation,
    onViewInvoice,
    onNewQuotation,
    onNewInvoice,
    onRefresh,
    companyLogo,
}) {
    const [activeTab, setActiveTab] = useState("quotations")
    const [refreshing, setRefreshing] = useState(false)
    const [search, setSearch] = useState("")
    const [statusFilter, setStatusFilter] = useState("all")
    const [monthOffset, setMonthOffset] = useState(0) // 0 = bulan semasa

    const handleRefresh = async () => {
        setRefreshing(true)
        await onRefresh()
        setRefreshing(false)
    }

    const handleLogout = () => {
        sessionStorage.removeItem("cs_authenticated")
        window.location.reload()
    }

    const switchTab = (tab) => {
        setActiveTab(tab)
        setStatusFilter("all")
    }

    // ---------- Monthly stats ----------
    const now = new Date()
    const viewMonth = new Date(now.getFullYear(), now.getMonth() + monthOffset, 1)
    const monthNames = [
        "Januari", "Februari", "Mac", "April", "Mei", "Jun",
        "Julai", "Ogos", "September", "Oktober", "November", "Disember",
    ]
    const monthLabel = `${monthNames[viewMonth.getMonth()]} ${viewMonth.getFullYear()}`

    const inMonth = (dateStr, ref) => {
        if (!dateStr) return false
        const d = new Date(dateStr)
        return (
            !isNaN(d.getTime()) &&
            d.getMonth() === ref.getMonth() &&
            d.getFullYear() === ref.getFullYear()
        )
    }

    const sumTotal = (list) => list.reduce((s, x) => s + (x.total || 0), 0)

    // Duit masuk bulanan: payments + deposit paid + baki invoice paid tanpa rekod
    const moneyInMonth = (ref) => {
        let total = 0
        let count = 0
        invoices.forEach((inv) => {
            let got = 0
            let paymentsSum = 0
            ;(inv.payments || []).forEach((p) => {
                paymentsSum += p.amount || 0
                if (inMonth(p.date, ref)) got += p.amount || 0
            })
            let depositPaid = 0
            if (inv.deposit && inv.deposit.status === "paid") {
                depositPaid = inv.deposit.amount || 0
                const dDate = inv.deposit.paidDate || inv.deposit.date
                if (inMonth(dDate, ref)) got += depositPaid
            }
            // Invoice ditanda paid tapi bayaran tak direkod penuh —
            // baki dianggap masuk pada tarikh invoice
            if (inv.status === "paid") {
                const remainder =
                    (inv.total || 0) - paymentsSum - depositPaid
                if (remainder > 0 && inMonth(inv.date, ref)) got += remainder
            }
            if (got > 0) {
                total += got
                count++
            }
        })
        return { total, count }
    }

    const mMoneyIn = moneyInMonth(viewMonth)
    const mInvoiced = invoices.filter((i) => inMonth(i.date, viewMonth))

    const stats = {
        revenue: mMoneyIn,
        invoiced: { total: sumTotal(mInvoiced), count: mInvoiced.length },
    }

    // Trend 6 bulan (berakhir pada bulan dipilih)
    const trend = Array.from({ length: 6 }, (_, idx) => {
        const ref = new Date(
            viewMonth.getFullYear(),
            viewMonth.getMonth() - (5 - idx),
            1
        )
        return {
            label: monthNames[ref.getMonth()].slice(0, 3),
            total: moneyInMonth(ref).total,
            isSelected: idx === 5,
        }
    })
    const trendMax = Math.max(...trend.map((t) => t.total), 1)

    // Keseluruhan (caption kecil)
    const overallOutstanding = invoices.filter(
        (i) => i.status === "unpaid" || i.status === "overdue"
    )
    const overallPending = quotations.filter((q) => q.status === "sent")

    const formatRM = (amount) => {
        if (amount >= 1000) {
            return `RM ${(amount / 1000).toFixed(amount % 1000 === 0 ? 0 : 1)}k`
        }
        return `RM ${amount.toLocaleString()}`
    }

    // ---------- Status meta ----------
    const statusMeta = {
        draft: { dot: "#9CA3AF", bg: "#F3F4F6", text: "#6B7280", label: "Draft" },
        sent: { dot: "#F59E0B", bg: "#FEF3C7", text: "#B45309", label: "Pending" },
        accepted: { dot: "#10B981", bg: "#D1FAE5", text: "#047857", label: "Accepted" },
        rejected: { dot: "#EF4444", bg: "#FEE2E2", text: "#B91C1C", label: "Rejected" },
        unpaid: { dot: "#F59E0B", bg: "#FEF3C7", text: "#B45309", label: "Unpaid" },
        paid: { dot: "#10B981", bg: "#D1FAE5", text: "#047857", label: "Paid" },
        overdue: { dot: "#EF4444", bg: "#FEE2E2", text: "#B91C1C", label: "Overdue" },
    }
    const meta = (s) => statusMeta[s] || statusMeta.draft

    // ---------- Filtering ----------
    const matchSearch = (item) => {
        if (!search.trim()) return true
        const hay = [item.id, item.client, item.company, item.project]
            .filter(Boolean)
            .join(" ")
            .toLowerCase()
        return hay.includes(search.trim().toLowerCase())
    }
    const matchStatus = (item) =>
        statusFilter === "all" || item.status === statusFilter

    const filteredQuotations = quotations.filter(
        (q) => matchSearch(q) && matchStatus(q)
    )
    const filteredInvoices = invoices.filter(
        (i) => matchSearch(i) && matchStatus(i)
    )

    const quotationChips = [
        { value: "all", label: "Semua" },
        { value: "sent", label: "Pending" },
        { value: "accepted", label: "Accepted" },
        { value: "draft", label: "Draft" },
        { value: "rejected", label: "Rejected" },
    ]
    const invoiceChips = [
        { value: "all", label: "Semua" },
        { value: "unpaid", label: "Unpaid" },
        { value: "paid", label: "Paid" },
        { value: "overdue", label: "Overdue" },
    ]
    const chips = activeTab === "quotations" ? quotationChips : invoiceChips

    const chipCount = (value) => {
        const list = activeTab === "quotations" ? quotations : invoices
        if (value === "all") return list.filter(matchSearch).length
        return list.filter((x) => x.status === value && matchSearch(x)).length
    }

    // ---------- List row ----------
    const ListRow = ({ item, isInvoice, onClick }) => {
        const m = meta(item.status)
        return (
            <div
                className="cs-card"
                onClick={onClick}
                style={{
                    backgroundColor: "#FFF",
                    borderRadius: 14,
                    cursor: "pointer",
                    border: "1px solid #E8EAEE",
                    boxShadow: "0 1px 3px rgba(15,23,42,.05)",
                    padding: "14px 16px",
                }}
            >
                <div
                    style={{
                        display: "flex",
                        justifyContent: "space-between",
                        alignItems: "center",
                        marginBottom: 8,
                        gap: 8,
                    }}
                >
                    <div
                        style={{
                            display: "flex",
                            alignItems: "center",
                            gap: 8,
                            minWidth: 0,
                        }}
                    >
                        <span
                            style={{
                                width: 8,
                                height: 8,
                                borderRadius: "50%",
                                backgroundColor: m.dot,
                                flexShrink: 0,
                            }}
                        />
                        <span
                            style={{
                                fontFamily: "monospace",
                                fontSize: 11,
                                fontWeight: 700,
                                color: "#475569",
                                backgroundColor: "#F1F5F9",
                                padding: "2px 8px",
                                borderRadius: 6,
                            }}
                        >
                            {item.id}
                        </span>
                        <span style={{ fontSize: 11, color: "#94A3B8" }}>
                            {isInvoice
                                ? item.due
                                    ? `Due ${formatDate(item.due)}`
                                    : ""
                                : item.date
                                  ? formatDate(item.date)
                                  : ""}
                        </span>
                    </div>
                    <span
                        style={{
                            fontSize: 11,
                            fontWeight: 600,
                            color: m.text,
                            backgroundColor: m.bg,
                            padding: "3px 10px",
                            borderRadius: 20,
                            whiteSpace: "nowrap",
                        }}
                    >
                        {m.label}
                    </span>
                </div>
                <div
                    style={{
                        display: "flex",
                        justifyContent: "space-between",
                        alignItems: "flex-end",
                        gap: 12,
                    }}
                >
                    <div style={{ minWidth: 0 }}>
                        <div
                            style={{
                                fontWeight: 700,
                                color: "#0F172A",
                                fontSize: 15,
                                marginBottom: 2,
                            }}
                        >
                            {item.client}
                        </div>
                        <div
                            style={{
                                fontSize: 12.5,
                                color: "#94A3B8",
                                lineHeight: 1.4,
                                overflow: "hidden",
                                display: "-webkit-box",
                                WebkitLineClamp: 1,
                                WebkitBoxOrient: "vertical",
                            }}
                        >
                            {item.project}
                        </div>
                    </div>
                    <div
                        style={{
                            fontWeight: 800,
                            color: "#0F172A",
                            fontSize: 16,
                            whiteSpace: "nowrap",
                        }}
                    >
                        RM {(item.total || 0).toLocaleString()}
                    </div>
                </div>
            </div>
        )
    }

    const list = activeTab === "quotations" ? filteredQuotations : filteredInvoices

    return (
        <div
            style={{
                minHeight: "100vh",
                width: "100%",
                backgroundColor: "#F4F5F7",
                fontFamily: "'Inter', system-ui, sans-serif",
                paddingBottom: 32,
            }}
        >
            {/* ===== Header (dark hero) ===== */}
            <div
                style={{
                    background:
                        "linear-gradient(135deg, #0F172A 0%, #1E293B 70%, #27303F 100%)",
                    borderBottom: "3px solid #F97316",
                }}
            >
                <div
                    style={{
                        maxWidth: 860,
                        margin: "0 auto",
                        padding: "18px 16px 64px",
                    }}
                >
                    <div
                        style={{
                            display: "flex",
                            justifyContent: "space-between",
                            alignItems: "center",
                            flexWrap: "wrap",
                            gap: 12,
                        }}
                    >
                        <div
                            style={{
                                display: "flex",
                                alignItems: "center",
                                gap: 12,
                            }}
                        >
                            <LogoDisplay
                                logo={companyLogo}
                                size={44}
                                bgColor="#FFF"
                                textColor="#F97316"
                                rounded={12}
                            />
                            <div>
                                <div
                                    style={{
                                        fontWeight: 800,
                                        color: "#FFF",
                                        fontSize: 17,
                                        letterSpacing: 0.2,
                                    }}
                                >
                                    {companyInfo.name}
                                </div>
                                <div
                                    style={{
                                        fontSize: 11.5,
                                        color: "#94A3B8",
                                        fontWeight: 500,
                                    }}
                                >
                                    Quotation & Invoice Dashboard
                                </div>
                            </div>
                        </div>
                        <div
                            style={{
                                display: "flex",
                                gap: 8,
                                alignItems: "center",
                            }}
                        >
                            <button
                                className="cs-btn"
                                onClick={handleRefresh}
                                disabled={refreshing}
                                title="Refresh data"
                                style={{
                                    padding: "9px 11px",
                                    borderRadius: 10,
                                    border: "1px solid rgba(255,255,255,0.18)",
                                    backgroundColor: "rgba(255,255,255,0.08)",
                                    color: "#E2E8F0",
                                    cursor: "pointer",
                                    opacity: refreshing ? 0.5 : 1,
                                    display: "inline-flex",
                                    alignItems: "center",
                                }}
                            >
                                <Icon name="refresh" size={16} />
                            </button>
                            <button
                                className="cs-btn"
                                onClick={onNewQuotation}
                                style={{
                                    padding: "9px 14px",
                                    borderRadius: 10,
                                    border: "none",
                                    background:
                                        "linear-gradient(135deg, #F97316, #EA580C)",
                                    color: "#FFF",
                                    fontWeight: 700,
                                    fontSize: 12.5,
                                    cursor: "pointer",
                                    boxShadow: "0 4px 12px rgba(249,115,22,.35)",
                                    display: "inline-flex",
                                    alignItems: "center",
                                    gap: 6,
                                }}
                            >
                                <Icon name="plus" size={15} />
                                Quotation
                            </button>
                            <button
                                className="cs-btn"
                                onClick={onNewInvoice}
                                style={{
                                    padding: "9px 14px",
                                    borderRadius: 10,
                                    border: "none",
                                    background:
                                        "linear-gradient(135deg, #3B82F6, #2563EB)",
                                    color: "#FFF",
                                    fontWeight: 700,
                                    fontSize: 12.5,
                                    cursor: "pointer",
                                    boxShadow: "0 4px 12px rgba(37,99,235,.35)",
                                    display: "inline-flex",
                                    alignItems: "center",
                                    gap: 6,
                                }}
                            >
                                <Icon name="plus" size={15} />
                                Invoice
                            </button>
                            <button
                                className="cs-btn"
                                onClick={handleLogout}
                                title="Logout"
                                style={{
                                    padding: "9px 11px",
                                    borderRadius: 10,
                                    border: "1px solid rgba(255,255,255,0.18)",
                                    backgroundColor: "transparent",
                                    color: "#94A3B8",
                                    cursor: "pointer",
                                    display: "inline-flex",
                                    alignItems: "center",
                                }}
                            >
                                <Icon name="logout" size={16} />
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <div style={{ maxWidth: 860, margin: "0 auto", padding: "0 16px" }}>
                {/* ===== Monthly overview (overlap hero) ===== */}
                <div
                    style={{
                        backgroundColor: "#FFF",
                        borderRadius: 16,
                        border: "1px solid #E8EAEE",
                        boxShadow: "0 4px 16px rgba(15,23,42,.06)",
                        marginTop: -44,
                        padding: "16px 18px 18px",
                    }}
                >
                    {/* Month navigator */}
                    <div
                        style={{
                            display: "flex",
                            justifyContent: "space-between",
                            alignItems: "center",
                            gap: 10,
                        }}
                    >
                        <button
                            className="cs-btn"
                            onClick={() => setMonthOffset(monthOffset - 1)}
                            style={{
                                width: 34,
                                height: 34,
                                borderRadius: 10,
                                border: "1px solid #E8EAEE",
                                backgroundColor: "#FFF",
                                color: "#475569",
                                cursor: "pointer",
                                display: "inline-flex",
                                alignItems: "center",
                                justifyContent: "center",
                            }}
                        >
                            <Icon name="chevron-left" size={18} />
                        </button>
                        <div style={{ textAlign: "center" }}>
                            <div
                                style={{
                                    fontSize: 10,
                                    fontWeight: 700,
                                    color: "#94A3B8",
                                    textTransform: "uppercase",
                                    letterSpacing: 1.2,
                                }}
                            >
                                Ringkasan Bulanan
                            </div>
                            <div
                                style={{
                                    fontSize: 16.5,
                                    fontWeight: 800,
                                    color: "#0F172A",
                                    marginTop: 2,
                                }}
                            >
                                {monthLabel}
                            </div>
                            {monthOffset !== 0 && (
                                <button
                                    className="cs-btn"
                                    onClick={() => setMonthOffset(0)}
                                    style={{
                                        marginTop: 4,
                                        padding: "3px 12px",
                                        borderRadius: 20,
                                        border: "1px solid #FED7AA",
                                        backgroundColor: "#FFF7ED",
                                        color: "#EA580C",
                                        fontSize: 11,
                                        fontWeight: 700,
                                        cursor: "pointer",
                                    }}
                                >
                                    Bulan Ini
                                </button>
                            )}
                        </div>
                        <button
                            className="cs-btn"
                            onClick={() => setMonthOffset(monthOffset + 1)}
                            disabled={monthOffset >= 0}
                            style={{
                                width: 34,
                                height: 34,
                                borderRadius: 10,
                                border: "1px solid #E8EAEE",
                                backgroundColor: "#FFF",
                                color: monthOffset >= 0 ? "#CBD5E1" : "#475569",
                                cursor:
                                    monthOffset >= 0
                                        ? "not-allowed"
                                        : "pointer",
                                display: "inline-flex",
                                alignItems: "center",
                                justifyContent: "center",
                            }}
                        >
                            <Icon name="chevron-right" size={18} />
                        </button>
                    </div>

                    {/* Monthly stat cards */}
                    <div
                        style={{
                            display: "grid",
                            gridTemplateColumns:
                                "repeat(auto-fit, minmax(150px, 1fr))",
                            gap: 10,
                            marginTop: 14,
                        }}
                    >
                        <div
                            style={{
                                backgroundColor: "#F0FDF4",
                                border: "1px solid #DCFCE7",
                                borderRadius: 12,
                                padding: "12px 14px",
                            }}
                        >
                            <div
                                style={{
                                    fontSize: 10.5,
                                    fontWeight: 700,
                                    color: "#64748B",
                                    textTransform: "uppercase",
                                    letterSpacing: 0.4,
                                    display: "flex",
                                    alignItems: "center",
                                    gap: 5,
                                }}
                            >
                                <Icon name="dollar" size={14} color="#059669" />
                                Duit Masuk
                            </div>
                            <div
                                style={{
                                    fontSize: 19,
                                    fontWeight: 800,
                                    color: "#059669",
                                    marginTop: 6,
                                }}
                            >
                                {formatRM(stats.revenue.total)}
                            </div>
                            <div
                                style={{
                                    fontSize: 11,
                                    color: "#94A3B8",
                                    marginTop: 2,
                                }}
                            >
                                {stats.revenue.count} invoice terima bayaran
                            </div>
                        </div>

                        <div
                            style={{
                                backgroundColor: "#EFF6FF",
                                border: "1px solid #DBEAFE",
                                borderRadius: 12,
                                padding: "12px 14px",
                            }}
                        >
                            <div
                                style={{
                                    fontSize: 10.5,
                                    fontWeight: 700,
                                    color: "#64748B",
                                    textTransform: "uppercase",
                                    letterSpacing: 0.4,
                                    display: "flex",
                                    alignItems: "center",
                                    gap: 5,
                                }}
                            >
                                <Icon name="file" size={14} color="#2563EB" />
                                Invoiced
                            </div>
                            <div
                                style={{
                                    fontSize: 19,
                                    fontWeight: 800,
                                    color: "#2563EB",
                                    marginTop: 6,
                                }}
                            >
                                {formatRM(stats.invoiced.total)}
                            </div>
                            <div
                                style={{
                                    fontSize: 11,
                                    color: "#94A3B8",
                                    marginTop: 2,
                                }}
                            >
                                {stats.invoiced.count} invoice dikeluarkan
                            </div>
                        </div>
                    </div>

                    {/* Revenue trend — 6 bulan */}
                    <div style={{ marginTop: 16 }}>
                        <div
                            style={{
                                fontSize: 10,
                                fontWeight: 700,
                                color: "#94A3B8",
                                textTransform: "uppercase",
                                letterSpacing: 1.2,
                                marginBottom: 10,
                            }}
                        >
                            Trend Duit Masuk · 6 Bulan
                        </div>
                        <div
                            style={{
                                display: "flex",
                                alignItems: "flex-end",
                                gap: 8,
                                height: 96,
                            }}
                        >
                            {trend.map((t, i) => {
                                const h = Math.max(
                                    Math.round((t.total / trendMax) * 64),
                                    t.total > 0 ? 4 : 2
                                )
                                return (
                                    <div
                                        key={i}
                                        title={`${t.label}: RM ${t.total.toLocaleString()}`}
                                        style={{
                                            flex: 1,
                                            display: "flex",
                                            flexDirection: "column",
                                            alignItems: "center",
                                            justifyContent: "flex-end",
                                            height: "100%",
                                            cursor: "default",
                                        }}
                                    >
                                        <div
                                            style={{
                                                fontSize: 10,
                                                fontWeight: t.isSelected
                                                    ? 700
                                                    : 500,
                                                color: t.isSelected
                                                    ? "#0F172A"
                                                    : "#94A3B8",
                                                marginBottom: 4,
                                                whiteSpace: "nowrap",
                                            }}
                                        >
                                            {t.total > 0
                                                ? formatRM(t.total).replace(
                                                      "RM ",
                                                      ""
                                                  )
                                                : ""}
                                        </div>
                                        <div
                                            style={{
                                                width: "100%",
                                                maxWidth: 42,
                                                height: h,
                                                backgroundColor: t.isSelected
                                                    ? "#EA580C"
                                                    : "#E2E8F0",
                                                borderRadius: "4px 4px 0 0",
                                            }}
                                        />
                                        <div
                                            style={{
                                                fontSize: 10.5,
                                                fontWeight: t.isSelected
                                                    ? 700
                                                    : 500,
                                                color: t.isSelected
                                                    ? "#EA580C"
                                                    : "#94A3B8",
                                                marginTop: 6,
                                            }}
                                        >
                                            {t.label}
                                        </div>
                                    </div>
                                )
                            })}
                        </div>
                    </div>

                    {/* Overall caption */}
                    <div
                        style={{
                            marginTop: 14,
                            paddingTop: 12,
                            borderTop: "1px solid #F1F5F9",
                            fontSize: 11.5,
                            color: "#64748B",
                            display: "flex",
                            gap: 16,
                            flexWrap: "wrap",
                        }}
                    >
                        <span>
                            Keseluruhan belum bayar:{" "}
                            <b style={{ color: "#D97706" }}>
                                {formatRM(sumTotal(overallOutstanding))}
                            </b>{" "}
                            ({overallOutstanding.length} invoice)
                        </span>
                        <span>
                            Quotation pending:{" "}
                            <b style={{ color: "#EA580C" }}>
                                {formatRM(sumTotal(overallPending))}
                            </b>{" "}
                            ({overallPending.length})
                        </span>
                    </div>
                </div>

                {/* ===== Tabs ===== */}
                <div style={{ marginTop: 20 }}>
                    <div
                        style={{
                            display: "flex",
                            gap: 4,
                            backgroundColor: "#E8EAEE",
                            padding: 4,
                            borderRadius: 12,
                        }}
                    >
                        {[
                            { key: "quotations", label: `Quotations (${quotations.length})` },
                            { key: "invoices", label: `Invoices (${invoices.length})` },
                        ].map((tab) => (
                            <button
                                key={tab.key}
                                onClick={() => switchTab(tab.key)}
                                style={{
                                    flex: 1,
                                    padding: "10px 16px",
                                    borderRadius: 9,
                                    border: "none",
                                    backgroundColor:
                                        activeTab === tab.key
                                            ? "#FFF"
                                            : "transparent",
                                    color:
                                        activeTab === tab.key
                                            ? "#0F172A"
                                            : "#64748B",
                                    fontWeight: 700,
                                    fontSize: 13.5,
                                    cursor: "pointer",
                                    boxShadow:
                                        activeTab === tab.key
                                            ? "0 1px 4px rgba(15,23,42,.12)"
                                            : "none",
                                }}
                            >
                                {tab.label}
                            </button>
                        ))}
                    </div>
                </div>

                {/* ===== Search + filter chips ===== */}
                <div style={{ marginTop: 14 }}>
                    <div style={{ position: "relative" }}>
                        <span
                            style={{
                                position: "absolute",
                                left: 14,
                                top: "50%",
                                transform: "translateY(-50%)",
                                color: "#94A3B8",
                                display: "flex",
                                pointerEvents: "none",
                            }}
                        >
                            <Icon name="search" size={16} />
                        </span>
                        <input
                            value={search}
                            onChange={(e) => setSearch(e.target.value)}
                            placeholder="Cari client, syarikat, projek atau ID…"
                            style={{
                                width: "100%",
                                padding: "12px 16px 12px 40px",
                                borderRadius: 12,
                                border: "1px solid #E8EAEE",
                                fontSize: 14,
                                outline: "none",
                                boxSizing: "border-box",
                                backgroundColor: "#FFF",
                                color: "#0F172A",
                            }}
                        />
                    </div>
                    <div
                        style={{
                            display: "flex",
                            gap: 6,
                            marginTop: 10,
                            flexWrap: "wrap",
                        }}
                    >
                        {chips.map((chip) => {
                            const active = statusFilter === chip.value
                            return (
                                <button
                                    key={chip.value}
                                    className="cs-btn"
                                    onClick={() => setStatusFilter(chip.value)}
                                    style={{
                                        padding: "6px 13px",
                                        borderRadius: 20,
                                        border: "1px solid",
                                        borderColor: active
                                            ? "#F97316"
                                            : "#E8EAEE",
                                        backgroundColor: active
                                            ? "#FFF7ED"
                                            : "#FFF",
                                        color: active ? "#EA580C" : "#64748B",
                                        fontSize: 12,
                                        fontWeight: active ? 700 : 500,
                                        cursor: "pointer",
                                    }}
                                >
                                    {chip.label}{" "}
                                    <span
                                        style={{
                                            opacity: 0.65,
                                            fontWeight: 600,
                                        }}
                                    >
                                        {chipCount(chip.value)}
                                    </span>
                                </button>
                            )
                        })}
                    </div>
                </div>

                {/* ===== List ===== */}
                <div
                    style={{
                        marginTop: 16,
                        display: "flex",
                        flexDirection: "column",
                        gap: 12,
                    }}
                >
                    {list.map((item) => (
                        <ListRow
                            key={item.id}
                            item={item}
                            isInvoice={activeTab === "invoices"}
                            onClick={() =>
                                activeTab === "invoices"
                                    ? onViewInvoice(item)
                                    : onViewQuotation(item)
                            }
                        />
                    ))}

                    {list.length === 0 && (
                        <div
                            style={{
                                textAlign: "center",
                                padding: "48px 20px",
                                color: "#94A3B8",
                                backgroundColor: "#FFF",
                                borderRadius: 14,
                                border: "1px dashed #E8EAEE",
                                fontSize: 13.5,
                            }}
                        >
                            {search || statusFilter !== "all"
                                ? "Tiada rekod padan dengan carian/filter."
                                : activeTab === "quotations"
                                  ? "Belum ada quotation."
                                  : "Belum ada invoice."}
                        </div>
                    )}
                </div>
            </div>
        </div>
    )
}

window.AdminPanel = AdminPanel
