%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/ |
| Current File : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/PaketPage.tsx |
import { useEffect, useState } from "react";
import Button from "@/components/ui/Button";
import Card, { CardBody, CardHeader } from "@/components/ui/Card";
import Badge from "@/components/ui/Badge";
import Modal from "@/components/ui/Modal";
import { usePackages } from "@/hooks/usePackages";
import { useWallet } from "@/hooks/useWallet";
import {
formatCredit,
formatDateTime,
formatIDR,
formatPackageDuration,
parseCredit,
} from "@/lib/format";
import type { PackageInfo } from "@/types/api";
const MIN_CLAIM_BALANCE = 10000;
function quotaText(v: number | null): string {
return v === null ? "Unlimited" : v.toLocaleString("id-ID");
}
function daysLeft(expiresAt: string): number {
const ms = new Date(expiresAt).getTime() - Date.now();
return Math.max(0, Math.ceil(ms / 86_400_000));
}
/* ─── Ikon SVG inline ─────────────────────────────────────────── */
function IconGift({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<path
d="M12 6v13m0-13c0-1.657-1.343-3-3-3s-3 1.343-3 3c0 1.657 3 3 3 3h3m0-3c0-1.657 1.343-3 3-3s3 1.343 3 3c0 1.657-3 3-3 3h-3M5 11h14v8H5v-8z"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M5 15h14M5 8h14"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
/>
</svg>
);
}
function IconCheckCircle({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.6" />
<path
d="m8.5 12.2 2.4 2.4 4.6-5"
stroke="currentColor"
strokeWidth="1.8"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function IconBolt({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<path
d="M13 3 5 13.5h6L11 21l8-10.5h-6L13 3z"
stroke="currentColor"
strokeWidth="1.6"
strokeLinejoin="round"
/>
</svg>
);
}
function IconToken({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<circle cx="12" cy="12" r="8.5" stroke="currentColor" strokeWidth="1.6" />
<path
d="M12 7v5l3.2 2"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function IconClock({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<circle cx="12" cy="12" r="8.5" stroke="currentColor" strokeWidth="1.6" />
<path
d="M12 7.5V12l3 2"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function IconWallet({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<path
d="M4 7.5A2.5 2.5 0 0 1 6.5 5H18a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H6.5A2.5 2.5 0 0 1 4 16.5v-9z"
stroke="currentColor"
strokeWidth="1.6"
strokeLinejoin="round"
/>
<path
d="M15 12.5h3.5a1.5 1.5 0 0 1 0 3H15a1.5 1.5 0 0 1 0-3z"
fill="currentColor"
/>
</svg>
);
}
function IconShieldCheck({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<path
d="M12 3.5 5 6v5.5c0 4.2 2.9 7.6 7 9 4.1-1.4 7-4.8 7-9V6l-7-2.5z"
stroke="currentColor"
strokeWidth="1.6"
strokeLinejoin="round"
/>
<path
d="m9 11.8 2.2 2.2 3.8-4.2"
stroke="currentColor"
strokeWidth="1.7"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
function IconPackage({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<path
d="M4 8.5 12 4l8 4.5v7L12 20l-8-4.5v-7z"
stroke="currentColor"
strokeWidth="1.6"
strokeLinejoin="round"
/>
<path
d="m4 8.5 8 4.5 8-4.5M12 13v7"
stroke="currentColor"
strokeWidth="1.6"
strokeLinejoin="round"
/>
</svg>
);
}
function IconAlert({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" className={className} aria-hidden>
<circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.6" />
<path
d="M12 8v5m0 3h.01"
stroke="currentColor"
strokeWidth="1.8"
strokeLinecap="round"
/>
</svg>
);
}
/* ─── Progress pemakaian kuota ────────────────────────────────── */
function QuotaProgress({
label,
total,
remaining,
}: {
label: string;
total: number | null;
remaining: number | null;
}) {
if (total === null) {
return (
<div className="flex items-center justify-between gap-3 text-xs">
<span className="text-slate-400">{label}</span>
<span className="font-semibold text-white">Unlimited</span>
</div>
);
}
const used = Math.max(0, total - (remaining ?? 0));
const pct = total > 0 ? Math.min(100, (used / total) * 100) : 0;
return (
<div>
<div className="flex items-center justify-between gap-3 text-xs">
<span className="text-slate-400">{label}</span>
<span className="tabular-nums text-slate-400">
Terpakai{" "}
<span className="font-semibold text-white">
{used.toLocaleString("id-ID")}
</span>{" "}
dari {total.toLocaleString("id-ID")}
</span>
</div>
<div className="mt-1.5 h-1.5 overflow-hidden rounded-full bg-white/[0.06]">
<div
className="h-full rounded-full bg-gradient-to-r from-emerald-400 to-teal-400"
style={{ width: `${pct}%` }}
/>
</div>
<p className="mt-1 text-right text-[10px] tabular-nums text-slate-500">
{pct.toFixed(0)}% terpakai
</p>
</div>
);
}
/* ─── Kartu paket ─────────────────────────────────────────────── */
function QuotaStat({
icon,
label,
value,
sub,
}: {
icon: React.ReactNode;
label: string;
value: string;
sub?: string;
}) {
return (
<div className="flex items-center gap-2.5 rounded-xl border border-white/[0.06] bg-white/[0.02] px-3 py-2.5">
<span className="shrink-0 text-slate-500">{icon}</span>
<div className="min-w-0">
<p className="text-[10px] uppercase tracking-wide text-slate-500">
{label}
</p>
<p className="truncate text-sm font-semibold tabular-nums text-white">
{value}
</p>
{sub && <p className="text-[10px] text-slate-500">{sub}</p>}
</div>
</div>
);
}
type PackageCardProps = {
pkg: PackageInfo;
hasActive: boolean;
claimed: boolean;
onBuy: (pkg: PackageInfo) => void;
onClaim: (pkg: PackageInfo) => void;
};
function PackageCard({
pkg,
hasActive,
claimed,
onBuy,
onClaim,
}: PackageCardProps) {
const isClaim = Number(pkg.claim_free) === 1;
const [copiedModel, setCopiedModel] = useState(false);
const handleCopyModel = async () => {
const id = "mercury-3";
try {
await navigator.clipboard.writeText(id);
setCopiedModel(true);
window.setTimeout(() => setCopiedModel(false), 1500);
} catch {
// Fallback: select manual bila clipboard tidak tersedia
const ta = document.createElement("textarea");
ta.value = id;
document.body.appendChild(ta);
ta.select();
try {
document.execCommand("copy");
setCopiedModel(true);
window.setTimeout(() => setCopiedModel(false), 1500);
} finally {
document.body.removeChild(ta);
}
}
};
/* ── Paket klaim gratis: kartu khusus dengan gradient & ribbon ── */
if (isClaim) {
return (
<Card className="relative flex flex-col overflow-hidden">
{/* Glow dekoratif */}
<div className="pointer-events-none absolute inset-0 bg-[radial-gradient(ellipse_80%_60%_at_50%_-10%,rgba(251,191,36,0.14),transparent)]" />
<div className="absolute inset-x-0 top-0 h-1 bg-gradient-to-r from-amber-400 via-orange-400 to-emerald-400" />
{/* Ribbon GRATIS */}
<div className="absolute right-0 top-4 rounded-l-full bg-gradient-to-r from-amber-400 to-orange-400 px-3 py-1 text-[10px] font-bold uppercase tracking-wider text-ink-950 shadow-lg shadow-amber-500/20">
Gratis
</div>
<CardBody className="relative flex flex-1 flex-col">
<div className="flex items-start gap-3 pr-16">
<span className="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-gradient-to-br from-amber-400/20 to-emerald-400/20 text-amber-300 ring-1 ring-amber-400/25">
<IconGift />
</span>
<div className="min-w-0">
<p className="text-base font-semibold text-white">{pkg.name}</p>
{pkg.description && (
<p className="mt-0.5 text-xs leading-relaxed text-slate-400">
{pkg.description}
</p>
)}
<div className="mt-2 flex flex-wrap items-center gap-1.5">
<span className="inline-flex w-fit items-center gap-1.5 rounded-full border border-amber-400/30 bg-amber-500/10 px-2.5 py-0.5 text-[10px] font-bold tracking-wide text-amber-200">
<svg
className="h-3 w-3"
fill="currentColor"
viewBox="0 0 24 24"
aria-hidden
>
<path d="M12 2l2.4 7.2L22 12l-7.6 2.8L12 22l-2.4-7.2L2 12l7.6-2.8L12 2z" />
</svg>
Disarankan pakai model mercury-3
</span>
<button
type="button"
onClick={handleCopyModel}
aria-label="Salin ID model mercury-3"
className="inline-flex items-center gap-1 rounded-full border border-white/[0.08] bg-white/[0.03] px-2.5 py-0.5 text-[10px] font-semibold text-slate-300 transition hover:bg-white/[0.07] hover:text-white"
>
{copiedModel ? (
<>
<svg
className="h-3 w-3 text-emerald-400"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2.2}
aria-hidden
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="m4.5 12.75 6 6 9-13.5"
/>
</svg>
Tersalin
</>
) : (
<>
<svg
className="h-3 w-3"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={1.8}
aria-hidden
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75"
/>
</svg>
Salin
</>
)}
</button>
</div>
</div>
</div>
<div className="mt-4 flex items-end justify-between gap-3">
<div>
<p className="text-2xl font-extrabold tracking-tight text-transparent bg-clip-text bg-gradient-to-r from-amber-300 to-emerald-300">
Gratis
</p>
<p className="text-[11px] text-slate-500">
{formatPackageDuration(pkg)}
</p>
</div>
<span className="flex items-center gap-1.5 text-[11px] text-slate-500">
<IconClock className="h-3.5 w-3.5" />
{formatPackageDuration(pkg)}
</span>
</div>
<div className="mt-3 grid grid-cols-2 gap-2.5">
<QuotaStat
icon={<IconBolt className="h-4.5 w-4.5" />}
label="Request"
value={quotaText(pkg.request_quota)}
/>
<QuotaStat
icon={<IconToken className="h-4.5 w-4.5" />}
label="Token"
value={quotaText(pkg.token_quota)}
/>
</div>
<div className="mt-4 flex-1" />
{claimed ? (
<Button variant="ghost" className="w-full" disabled>
<IconCheckCircle className="h-4 w-4 text-emerald-400" />
Sudah diklaim
</Button>
) : hasActive ? (
<Button variant="ghost" className="w-full" disabled>
Tunggu paket aktif berakhir
</Button>
) : (
<Button
className="w-full bg-gradient-to-r from-amber-500 to-orange-500 text-white shadow-lg shadow-amber-500/20 hover:from-amber-400 hover:to-orange-400"
onClick={() => onClaim(pkg)}
>
<IconGift className="h-4 w-4" />
Klaim Gratis
</Button>
)}
</CardBody>
</Card>
);
}
/* ── Paket berbayar: kartu standar premium ── */
const isActivePkg = hasActive;
return (
<Card className="relative flex flex-col overflow-hidden">
<div className="absolute inset-x-0 top-0 h-1 bg-gradient-to-r from-blue-500 via-blue-600 to-indigo-500" />
<CardBody className="flex flex-1 flex-col">
<div className="flex items-start gap-3">
<span className="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-blue-500/10 text-blue-300 ring-1 ring-blue-400/20">
<IconPackage />
</span>
<div className="min-w-0">
<p className="text-base font-semibold text-white">{pkg.name}</p>
{pkg.description && (
<p className="mt-0.5 text-xs leading-relaxed text-slate-400">
{pkg.description}
</p>
)}
</div>
</div>
<div className="mt-4 flex items-end justify-between gap-3">
<div>
<p className="text-2xl font-extrabold tracking-tight tabular-nums text-white">
{formatIDR(Number(pkg.price))}
</p>
<p className="text-[11px] text-slate-500">
{formatPackageDuration(pkg)}
</p>
</div>
<span className="flex items-center gap-1.5 text-[11px] text-slate-500">
<IconClock className="h-3.5 w-3.5" />
{formatPackageDuration(pkg)}
</span>
</div>
<div className="mt-3 grid grid-cols-2 gap-2.5">
<QuotaStat
icon={<IconBolt className="h-4.5 w-4.5" />}
label="Request"
value={quotaText(pkg.request_quota)}
/>
<QuotaStat
icon={<IconToken className="h-4.5 w-4.5" />}
label="Token"
value={quotaText(pkg.token_quota)}
/>
</div>
<div className="mt-4 flex-1" />
<Button
className="w-full"
disabled={isActivePkg}
onClick={() => onBuy(pkg)}
>
{isActivePkg ? "Sudah ada paket aktif" : "Beli Paket"}
</Button>
</CardBody>
</Card>
);
}
/* ─── Halaman ─────────────────────────────────────────────────── */
export default function PaketPage() {
const { catalog, active, purchases, loading, error, buy, claim } =
usePackages();
const { data: wallet, refresh: refreshWallet } = useWallet();
const [busy, setBusy] = useState<number | null>(null);
const [notice, setNotice] = useState<{
id: number;
type: "success" | "error";
text: string;
} | null>(null);
const [claimTarget, setClaimTarget] = useState<PackageInfo | null>(null);
const [buyTarget, setBuyTarget] = useState<PackageInfo | null>(null);
// Auto-dismiss notifikasi setelah 5 detik.
useEffect(() => {
if (!notice) return;
const t = setTimeout(() => setNotice(null), 5000);
return () => clearTimeout(t);
}, [notice]);
const showNotice = (type: "success" | "error", text: string) =>
setNotice({ id: Date.now(), type, text });
const balance = wallet ? parseCredit(wallet.wallet.balance) : 0;
const hasActive = Boolean(active);
// Paket klaim gratis hanya boleh diklaim sekali — cek riwayat
// (klaim selalu tercatat dengan harga 0).
const claimedIds = new Set(
purchases
.filter((p) => Number(p.package_price) === 0)
.map((p) => p.package_id),
);
const handleBuy = async (pkg: PackageInfo) => {
setBusy(pkg.id);
setNotice(null);
try {
const res = await buy(pkg.id);
await refreshWallet();
setBuyTarget(null);
showNotice(
"success",
`Paket "${res.user_package.package_name}" aktif! Berakhir ${formatDateTime(
res.user_package.expires_at,
)}.`,
);
} catch (e) {
showNotice(
"error",
e instanceof Error ? e.message : "Gagal membeli paket",
);
} finally {
setBusy(null);
}
};
const handleClaim = async (pkg: PackageInfo) => {
setBusy(pkg.id);
setNotice(null);
try {
const res = await claim(pkg.id);
await refreshWallet();
setClaimTarget(null);
showNotice(
"success",
`Paket gratis "${res.user_package.package_name}" berhasil diklaim! Aktif sampai ${formatDateTime(
res.user_package.expires_at,
)}.`,
);
} catch (e) {
showNotice(
"error",
e instanceof Error ? e.message : "Gagal mengklaim paket",
);
} finally {
setBusy(null);
}
};
return (
<div className="space-y-6">
<div className="flex flex-wrap items-center justify-between gap-4">
<div>
<h2 className="text-xl font-bold tracking-tight text-white sm:text-2xl">
Paket
</h2>
<p className="mt-1 text-sm text-slate-500">
Pilih paket kuota, atau klaim paket gratis langsung dari kartu.
</p>
</div>
<div className="panel flex items-center gap-3 px-4 py-2.5">
<IconWallet className="h-4.5 w-4.5 text-slate-400" />
<span className="text-xs text-slate-500">Saldo wallet</span>
<span className="text-base font-bold tabular-nums text-white">
{wallet ? formatCredit(wallet.wallet.balance) : "…"}
</span>
</div>
</div>
{notice && (
<div
key={notice.id}
role="alert"
className={`flex items-start gap-3 rounded-xl border px-4 py-3 text-sm ${
notice.type === "success"
? "border-emerald-500/25 bg-emerald-500/10 text-emerald-300"
: "border-red-500/25 bg-red-500/10 text-red-300"
}`}
>
{notice.type === "success" ? (
<IconCheckCircle className="mt-0.5 h-4.5 w-4.5 shrink-0" />
) : (
<IconAlert className="mt-0.5 h-4.5 w-4.5 shrink-0" />
)}
<span className="flex-1 leading-relaxed">{notice.text}</span>
<button
type="button"
onClick={() => setNotice(null)}
aria-label="Tutup notifikasi"
className="shrink-0 rounded-lg p-1 text-current opacity-60 transition hover:bg-white/[0.06] hover:opacity-100"
>
<svg
className="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
)}
{/* Paket aktif */}
{active && (
<Card className="relative overflow-hidden">
<div className="absolute inset-x-0 top-0 h-1 bg-gradient-to-r from-emerald-400 to-teal-400" />
<CardHeader
title="Paket Aktif"
subtitle={`${active.name} · berakhir ${formatDateTime(active.expires_at)}`}
action={<Badge tone="success">Aktif</Badge>}
/>
<CardBody>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
<div className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4">
<div className="flex items-center gap-2 text-xs font-medium text-slate-400">
<IconBolt className="h-4 w-4 text-slate-500" />
Request
</div>
<div className="mt-2.5">
<QuotaProgress
label="Sisa request"
total={active.request_quota}
remaining={active.request_remaining}
/>
</div>
</div>
<div className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4">
<div className="flex items-center gap-2 text-xs font-medium text-slate-400">
<IconToken className="h-4 w-4 text-slate-500" />
Token
</div>
<div className="mt-2.5">
<QuotaProgress
label="Sisa token"
total={active.token_quota}
remaining={active.token_remaining}
/>
</div>
</div>
<div className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4">
<div className="flex items-center gap-2 text-xs font-medium text-slate-400">
<IconClock className="h-4 w-4 text-slate-500" />
Masa Aktif
</div>
<p className="mt-2 text-2xl font-bold tabular-nums text-white">
{daysLeft(active.expires_at)} hari
</p>
<p className="mt-0.5 text-[11px] text-slate-500">
berakhir {formatDateTime(active.expires_at)}
</p>
</div>
<div className="rounded-xl border border-emerald-500/20 bg-emerald-500/[0.06] p-4">
<div className="flex items-center gap-2 text-xs font-medium text-emerald-300">
<IconCheckCircle className="h-4 w-4" />
Status
</div>
<div className="mt-2.5 space-y-1.5 text-xs">
<div className="flex items-center justify-between gap-2">
<span className="text-slate-400">Paket</span>
<span className="truncate font-medium text-white">
{active.name}
</span>
</div>
<div className="flex items-center justify-between gap-2">
<span className="text-slate-400">Diklaim</span>
<span className="tabular-nums text-slate-300">
{Number(active.package_price) === 0
? "Ya"
: formatIDR(Number(active.package_price))}
</span>
</div>
</div>
</div>
</div>
</CardBody>
</Card>
)}
{/* Katalog */}
<div>
<h3 className="mb-3 text-sm font-semibold text-white">
Katalog Paket
</h3>
{loading ? (
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
{[...Array(3)].map((_, i) => (
<div
key={i}
className="h-60 animate-pulse rounded-2xl border border-white/[0.04] bg-white/[0.02]"
/>
))}
</div>
) : error ? (
<Card>
<CardBody>
<p className="text-sm text-red-300">{error}</p>
</CardBody>
</Card>
) : catalog.length === 0 ? (
<Card>
<CardBody>
<p className="text-sm text-slate-500">
Belum ada paket tersedia.
</p>
</CardBody>
</Card>
) : (
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
{catalog.map((pkg) => (
<PackageCard
key={pkg.id}
pkg={pkg}
hasActive={hasActive}
claimed={claimedIds.has(pkg.id)}
onBuy={(p) => setBuyTarget(p)}
onClaim={(p) => setClaimTarget(p)}
/>
))}
</div>
)}
</div>
{/* Riwayat pembelian & klaim */}
<Card>
<CardHeader title="Riwayat" subtitle="Paket yang dibeli atau diklaim" />
<CardBody className="p-0">
{purchases.length === 0 ? (
<div className="px-5 py-10 text-center text-sm text-slate-500">
Belum ada pembelian/klaim paket.
</div>
) : (
<>
{/* Mobile: kartu */}
<div className="divide-y divide-white/[0.04] md:hidden">
{purchases.map((p) => (
<div key={p.id} className="p-4">
<div className="flex items-start justify-between gap-2">
<div className="min-w-0">
<p className="font-medium text-white">{p.name}</p>
<p className="mt-0.5 text-xs text-slate-500">
{formatPackageDuration(p)} ·{" "}
{Number(p.package_price) === 0
? "Klaim gratis"
: formatIDR(Number(p.package_price))}
</p>
</div>
{p.status === "active" ? (
<Badge tone="success">Aktif</Badge>
) : p.status === "exhausted" ? (
<Badge tone="warning">Kuota Habis</Badge>
) : (
<Badge tone="default">Berakhir</Badge>
)}
</div>
<div className="mt-2.5 grid grid-cols-2 gap-2 text-xs">
<div className="rounded-lg border border-white/[0.06] bg-white/[0.02] px-2.5 py-1.5">
<p className="text-[10px] text-slate-500">
Sisa Request
</p>
<p className="font-medium tabular-nums text-slate-300">
{quotaText(p.request_remaining)}
</p>
</div>
<div className="rounded-lg border border-white/[0.06] bg-white/[0.02] px-2.5 py-1.5">
<p className="text-[10px] text-slate-500">
Sisa Token
</p>
<p className="font-medium tabular-nums text-slate-300">
{quotaText(p.token_remaining)}
</p>
</div>
</div>
<p className="mt-2 text-[10px] tabular-nums text-slate-600">
{formatDateTime(p.purchased_at)}
</p>
</div>
))}
</div>
{/* Desktop: tabel */}
<div className="hidden overflow-x-auto md:block">
<table className="w-full min-w-[640px] text-left text-sm">
<thead>
<tr className="border-b border-white/[0.06] bg-white/[0.02] text-[11px] uppercase tracking-wider text-slate-500">
<th className="px-5 py-3 font-semibold">Paket</th>
<th className="px-4 py-3 text-right font-semibold">
Harga
</th>
<th className="px-4 py-3 text-right font-semibold">
Sisa Request
</th>
<th className="px-4 py-3 text-right font-semibold">
Sisa Token
</th>
<th className="px-4 py-3 font-semibold">Status</th>
<th className="px-5 py-3 text-right font-semibold">
Dibeli
</th>
</tr>
</thead>
<tbody>
{purchases.map((p) => (
<tr
key={p.id}
className="border-b border-white/[0.04] transition-colors last:border-0 hover:bg-white/[0.02]"
>
<td className="px-5 py-3.5">
<p className="font-medium text-white">{p.name}</p>
<p className="text-[11px] text-slate-500">
{formatPackageDuration(p)}
</p>
</td>
<td className="px-4 py-3.5 text-right font-medium tabular-nums text-slate-300">
{Number(p.package_price) === 0
? "Gratis"
: formatIDR(Number(p.package_price))}
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-400">
{quotaText(p.request_remaining)}
</td>
<td className="px-4 py-3.5 text-right tabular-nums text-slate-400">
{quotaText(p.token_remaining)}
</td>
<td className="px-4 py-3.5">
{p.status === "active" ? (
<Badge tone="success">Aktif</Badge>
) : p.status === "exhausted" ? (
<Badge tone="warning">Kuota Habis</Badge>
) : (
<Badge tone="default">Berakhir</Badge>
)}
</td>
<td className="px-5 py-3.5 text-right text-xs tabular-nums text-slate-500">
{formatDateTime(p.purchased_at)}
</td>
</tr>
))}
</tbody>
</table>
</div>
</>
)}
</CardBody>
</Card>
{/* Modal konfirmasi pembelian */}
<Modal
open={Boolean(buyTarget)}
onClose={() => setBuyTarget(null)}
title="Konfirmasi Pembelian"
subtitle={buyTarget ? buyTarget.name : ""}
>
{buyTarget && (
<div className="space-y-4">
{/* Ringkasan paket */}
<div className="flex items-center gap-3 rounded-xl border border-white/[0.06] bg-white/[0.02] p-3">
<span className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-blue-500/10 text-blue-300 ring-1 ring-blue-400/20">
<IconPackage className="h-5 w-5" />
</span>
<div className="min-w-0">
<p className="text-sm font-semibold text-white">
{buyTarget.name}
</p>
<p className="text-xs text-slate-500">
{formatPackageDuration(buyTarget)} ·{" "}
{quotaText(buyTarget.request_quota)} request ·{" "}
{quotaText(buyTarget.token_quota)} token
</p>
</div>
<p className="ml-auto shrink-0 text-sm font-bold tabular-nums text-white">
{formatIDR(Number(buyTarget.price))}
</p>
</div>
{/* Peringatan potongan saldo */}
<div className="rounded-xl border border-amber-400/15 bg-amber-500/[0.06] p-3.5">
<p className="flex items-center gap-2 text-xs font-semibold text-amber-200">
<IconWallet className="h-4 w-4" />
Akan memotong saldo wallet
</p>
<div className="mt-2 space-y-1.5 text-xs text-slate-400">
<div className="flex items-center justify-between gap-2">
<span>Saldo saat ini</span>
<span className="font-medium tabular-nums text-slate-300">
{wallet ? formatCredit(wallet.wallet.balance) : "…"}
</span>
</div>
<div className="flex items-center justify-between gap-2">
<span>Dipotong</span>
<span className="font-semibold tabular-nums text-red-300">
−{formatIDR(Number(buyTarget.price))}
</span>
</div>
<div className="flex items-center justify-between gap-2 border-t border-white/[0.06] pt-1.5">
<span>Sisa setelah pembelian</span>
<span className="font-semibold tabular-nums text-emerald-300">
{balance >= Number(buyTarget.price)
? formatIDR(balance - Number(buyTarget.price))
: "—"}
</span>
</div>
</div>
{balance < Number(buyTarget.price) && (
<p className="mt-2.5 rounded-lg bg-red-500/10 px-2.5 py-1.5 text-[11px] font-medium text-red-300">
Saldo tidak cukup — isi saldo dulu sebelum membeli.
</p>
)}
</div>
<div className="flex gap-3">
<Button
variant="ghost"
className="flex-1"
onClick={() => setBuyTarget(null)}
>
Batal
</Button>
<Button
className="flex-1"
isLoading={busy === buyTarget.id}
disabled={balance < Number(buyTarget.price)}
onClick={() => handleBuy(buyTarget)}
>
<IconPackage className="h-4 w-4" />
Konfirmasi Pembelian
</Button>
</div>
</div>
)}
</Modal>
{/* Modal konfirmasi klaim gratis */}
<Modal
open={Boolean(claimTarget)}
onClose={() => setClaimTarget(null)}
title="Klaim Paket Gratis"
subtitle={claimTarget ? claimTarget.name : ""}
>
{claimTarget && (
<div className="space-y-4">
{/* Ringkasan paket */}
<div className="flex items-center gap-3 rounded-xl border border-white/[0.06] bg-white/[0.02] p-3">
<span className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-gradient-to-br from-amber-400/20 to-emerald-400/20 text-amber-300 ring-1 ring-amber-400/25">
<IconGift className="h-5 w-5" />
</span>
<div className="min-w-0">
<p className="text-sm font-semibold text-white">
{claimTarget.name}
</p>
<p className="text-xs text-slate-500">
{formatPackageDuration(claimTarget)} ·{" "}
{quotaText(claimTarget.request_quota)} request ·{" "}
{quotaText(claimTarget.token_quota)} token
</p>
</div>
</div>
{/* Syarat */}
<div className="rounded-xl border border-amber-400/15 bg-amber-500/[0.06] p-3.5">
<p className="flex items-center gap-2 text-xs font-semibold text-amber-200">
<IconShieldCheck className="h-4 w-4" />
Syarat klaim
</p>
<ul className="mt-2 space-y-1.5 text-xs leading-relaxed text-slate-400">
<li className="flex items-start gap-2">
<span className="mt-0.5 h-1 w-1 shrink-0 rounded-full bg-amber-400" />
Saldo wallet minimal {formatIDR(MIN_CLAIM_BALANCE)} (tanpa
potongan saldo).
</li>
</ul>
{balance < MIN_CLAIM_BALANCE && (
<p className="mt-2.5 rounded-lg bg-red-500/10 px-2.5 py-1.5 text-[11px] font-medium text-red-300">
Saldo belum cukup — isi saldo minimal{" "}
{formatIDR(MIN_CLAIM_BALANCE)}.
</p>
)}
</div>
<div className="flex gap-3">
<Button
variant="ghost"
className="flex-1"
onClick={() => setClaimTarget(null)}
>
Batal
</Button>
<Button
className="flex-1 bg-gradient-to-r from-amber-500 to-orange-500 text-white shadow-lg shadow-amber-500/20 hover:from-amber-400 hover:to-orange-400"
isLoading={busy === claimTarget.id}
disabled={balance < MIN_CLAIM_BALANCE}
onClick={() => handleClaim(claimTarget)}
>
<IconGift className="h-4 w-4" />
Konfirmasi Klaim
</Button>
</div>
</div>
)}
</Modal>
</div>
);
}