import { jsxs, jsx } from "react/jsx-runtime";
import { Link } from "@tanstack/react-router";
import { useState, useMemo } from "react";
import { Plus, Flame, Swords, Gamepad2, Mailbox, Handshake, Megaphone, Search, PackageOpen, Package, Eye } from "lucide-react";
import { u as useProducts } from "./useProducts-DKx9jW9k.js";
import { s as supabase } from "./client-Dz5aErML.js";
import "@supabase/supabase-js";
const CATEGORIES = [{
  v: "Free Fire",
  label: "Free Fire",
  icon: Flame
}, {
  v: "Mobile Legends",
  label: "Mobile Legends",
  icon: Swords
}, {
  v: "Rental",
  label: "Rental",
  icon: Gamepad2
}, {
  v: "JasaPost",
  label: "JasaPost",
  icon: Mailbox
}, {
  v: "Partner Resmi Bang Pergam",
  label: "Partner Resmi",
  icon: Handshake
}, {
  v: "Paid Promote Bang Pergam",
  label: "Paid Promote",
  icon: Megaphone
}];
const formatIDR = (n) => new Intl.NumberFormat("id-ID", {
  style: "currency",
  currency: "IDR",
  maximumFractionDigits: 0
}).format(n);
function AdminDashboard() {
  const {
    products,
    loading,
    refetch
  } = useProducts();
  const [category, setCategory] = useState("Free Fire");
  const [deletingId, setDeletingId] = useState(null);
  const [searchQuery, setSearchQuery] = useState("");
  const filtered = useMemo(() => {
    let res = products.filter((p) => p.game === category);
    if (searchQuery.trim()) {
      const q = searchQuery.toLowerCase();
      res = res.filter((p) => p.name.toLowerCase().includes(q) || p.description && p.description.toLowerCase().includes(q));
    }
    return res;
  }, [products, category, searchQuery]);
  const counts = useMemo(() => {
    const map = {};
    products.forEach((p) => {
      map[p.game] = (map[p.game] ?? 0) + 1;
    });
    return map;
  }, [products]);
  const handleDelete = async (p) => {
    if (!confirm(`Hapus produk "${p.name}"? Tindakan ini tidak bisa dibatalkan.`)) return;
    setDeletingId(p.id);
    const {
      error
    } = await supabase.from("products").delete().eq("id", p.id);
    setDeletingId(null);
    if (error) {
      alert("Gagal menghapus: " + error.message);
    } else {
      refetch();
    }
  };
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-6 mb-8", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col sm:flex-row sm:items-center justify-between gap-4", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("h1", { className: "font-display text-3xl font-bold", children: "Kelola Produk" }),
        /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground mt-1", children: "Tambah, ubah, atau hapus produk. Perubahan langsung muncul di website." })
      ] }),
      /* @__PURE__ */ jsxs(Link, { to: "/adminpergam/products/new", className: "inline-flex items-center justify-center gap-2 px-5 py-2.5 rounded-full text-sm font-bold bg-primary text-primary-foreground border border-primary/20 glow-purple-sm hover:opacity-90 transition shrink-0 self-start sm:self-auto", children: [
        /* @__PURE__ */ jsx(Plus, { className: "h-4 w-4" }),
        /* @__PURE__ */ jsx("span", { children: "Tambah Produk" })
      ] })
    ] }) }),
    /* @__PURE__ */ jsx("div", { className: "flex flex-nowrap overflow-x-auto pb-4 gap-2 mb-6 -mx-4 px-4 sm:mx-0 sm:px-0 sm:flex-wrap sm:overflow-visible no-scrollbar", children: CATEGORIES.map((c) => /* @__PURE__ */ jsxs("button", { onClick: () => setCategory(c.v), className: `group inline-flex items-center gap-2 px-4 py-2.5 rounded-full text-sm font-medium border transition shrink-0 ${category === c.v ? "bg-primary text-primary-foreground border-primary glow-purple-sm" : "bg-card/60 border-border text-muted-foreground hover:text-foreground hover:border-primary/30"}`, children: [
      /* @__PURE__ */ jsx(c.icon, { className: "h-4 w-4 opacity-80 transition-transform group-hover:-translate-y-0.5 group-hover:scale-105" }),
      /* @__PURE__ */ jsx("span", { children: c.label }),
      /* @__PURE__ */ jsxs("span", { className: "text-[10px] opacity-70", children: [
        "(",
        counts[c.v] ?? 0,
        ")"
      ] })
    ] }, c.v)) }),
    /* @__PURE__ */ jsxs("div", { className: "mb-6 max-w-md relative", children: [
      /* @__PURE__ */ jsx("input", { type: "text", value: searchQuery, onChange: (e) => setSearchQuery(e.target.value), placeholder: "Cari nama produk...", className: "w-full rounded-2xl border border-border bg-card/60 pl-11 pr-4 py-2.5 text-sm outline-none transition focus:border-primary" }),
      /* @__PURE__ */ jsx(Search, { className: "absolute left-4 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground opacity-75" })
    ] }),
    loading ? /* @__PURE__ */ jsx("div", { className: "text-center py-16 text-muted-foreground text-sm", children: "Memuat..." }) : filtered.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "card-surface rounded-2xl p-16 text-center", children: [
      /* @__PURE__ */ jsx(PackageOpen, { className: "mx-auto mb-3 h-12 w-12 text-primary/70", strokeWidth: 1.7 }),
      /* @__PURE__ */ jsx("h3", { className: "font-display font-semibold text-lg", children: "Belum ada produk" }),
      /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-sm mt-1", children: 'Klik "Tambah Produk" untuk membuat produk pertama.' })
    ] }) : /* @__PURE__ */ jsxs("div", { className: "card-surface rounded-2xl overflow-hidden border border-border shadow-sm", children: [
      /* @__PURE__ */ jsx("div", { className: "max-h-[31rem] overflow-auto scrollbar-none", children: /* @__PURE__ */ jsxs("table", { className: "w-full", children: [
        /* @__PURE__ */ jsx("thead", { className: "sticky top-0 z-10 bg-muted/95 backdrop-blur text-left text-[10px] uppercase tracking-widest text-muted-foreground font-bold", children: /* @__PURE__ */ jsxs("tr", { children: [
          /* @__PURE__ */ jsx("th", { className: "px-4 py-4 md:px-6", children: "Produk" }),
          /* @__PURE__ */ jsx("th", { className: "px-6 py-4 hidden md:table-cell", children: "Kategori" }),
          /* @__PURE__ */ jsx("th", { className: "px-4 py-4 md:px-6 text-center whitespace-nowrap", children: "Harga" }),
          /* @__PURE__ */ jsx("th", { className: "px-6 py-4 text-center hidden md:table-cell", children: "Status" }),
          /* @__PURE__ */ jsx("th", { className: "px-4 py-4 md:px-6 text-right whitespace-nowrap", children: "Aksi" })
        ] }) }),
        /* @__PURE__ */ jsx("tbody", { className: "divide-y divide-border/50", children: filtered.map((p) => /* @__PURE__ */ jsxs("tr", { className: "group hover:bg-primary/5 transition-colors", children: [
          /* @__PURE__ */ jsx("td", { className: "px-4 py-4 md:px-6", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3 md:gap-4", children: [
            p.image ? /* @__PURE__ */ jsx("div", { className: "relative h-12 w-12 md:h-14 md:w-14 shrink-0 overflow-hidden rounded-xl border border-border shadow-sm", children: /* @__PURE__ */ jsx("img", { src: p.image, alt: "", className: "h-full w-full object-cover transition-transform group-hover:scale-110" }) }) : /* @__PURE__ */ jsx("div", { className: "grid h-12 w-12 md:h-14 md:w-14 shrink-0 place-items-center rounded-xl bg-muted border border-border", children: /* @__PURE__ */ jsx(Package, { className: "h-5 w-5 md:h-6 md:w-6 text-muted-foreground opacity-50", strokeWidth: 1.5 }) }),
            /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
              /* @__PURE__ */ jsx("div", { className: "font-bold text-xs md:text-sm text-foreground truncate", children: p.name }),
              /* @__PURE__ */ jsx("div", { className: "mt-0.5 text-[10px] md:text-xs text-muted-foreground line-clamp-1 max-w-[120px] md:max-w-[200px]", children: p.description || "Tidak ada deskripsi" })
            ] })
          ] }) }),
          /* @__PURE__ */ jsx("td", { className: "px-6 py-4 hidden md:table-cell", children: /* @__PURE__ */ jsx("span", { className: "inline-flex items-center rounded-full bg-primary/10 px-2.5 py-0.5 text-[10px] font-bold text-primary border border-primary/20", children: p.game }) }),
          /* @__PURE__ */ jsxs("td", { className: "px-4 py-4 md:px-6 text-center", children: [
            /* @__PURE__ */ jsx("div", { className: "text-xs md:text-sm font-semibold text-foreground", children: p.rentalPackages?.length ? `${formatIDR(p.rentalPackages[0].price)}` : p.price > 0 ? formatIDR(p.price) : "—" }),
            p.rentalPackages?.length ? /* @__PURE__ */ jsx("div", { className: "text-[9px] md:text-[10px] text-muted-foreground", children: "Mulai dari" }) : null
          ] }),
          /* @__PURE__ */ jsx("td", { className: "px-6 py-4 text-center hidden md:table-cell", children: p.status === "Ready" ? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-1.5 text-[10px] font-bold text-success", children: [
            /* @__PURE__ */ jsx("div", { className: "h-1.5 w-1.5 rounded-full bg-success animate-pulse" }),
            "Ready"
          ] }) : /* @__PURE__ */ jsx("span", { className: "text-[10px] font-bold text-muted-foreground uppercase tracking-wider", children: "N/A" }) }),
          /* @__PURE__ */ jsx("td", { className: "px-4 py-4 md:px-6 text-right", children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-1.5 md:gap-2", children: [
            /* @__PURE__ */ jsxs(Link, { to: "/adminpergam/products/$id", params: {
              id: p.id
            }, className: "inline-flex h-8 w-8 items-center justify-center rounded-lg border border-border bg-card text-muted-foreground transition hover:border-primary/50 hover:bg-primary/5 hover:text-primary md:w-auto md:px-3", title: "Edit / Detail", children: [
              /* @__PURE__ */ jsx(Eye, { className: "h-4 w-4" }),
              /* @__PURE__ */ jsx("span", { className: "hidden md:inline ml-1.5 text-xs font-bold", children: "Edit" })
            ] }),
            /* @__PURE__ */ jsxs("button", { onClick: () => handleDelete(p), disabled: deletingId === p.id, className: "inline-flex h-8 w-8 items-center justify-center rounded-lg border border-destructive/20 bg-destructive/5 text-destructive transition hover:border-destructive hover:bg-destructive/10 disabled:opacity-50 md:w-auto md:px-3", title: "Hapus", children: [
              /* @__PURE__ */ jsx("span", { className: "hidden md:inline text-xs font-bold mr-1.5", children: deletingId === p.id ? "..." : "Hapus" }),
              /* @__PURE__ */ jsx(PackageOpen, { className: "h-4 w-4 md:hidden" }),
              /* @__PURE__ */ jsx("span", { className: "hidden md:inline" })
            ] })
          ] }) })
        ] }, p.id)) })
      ] }) }),
      filtered.length > 5 && /* @__PURE__ */ jsxs("div", { className: "border-t border-border px-4 py-3 text-xs text-muted-foreground md:px-6", children: [
        "Menampilkan 5 produk pertama, scroll ke bawah untuk melihat ",
        filtered.length - 5,
        " produk lainnya."
      ] })
    ] })
  ] });
}
export {
  AdminDashboard as component
};
