| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- 'use client';
- import { useState, useMemo } from 'react';
- import {
- Edit3,
- QrCode,
- Trash2,
- Coins,
- Search,
- LayoutGrid,
- List,
- Link as LinkIcon,
- Copy,
- Check,
- ToggleLeft,
- ToggleRight,
- Loader2,
- Plus,
- Layers,
- CreditCard
- } from 'lucide-react';
- import { Provider } from '@/types';
- import { getPaymentProviderIcon } from './paymentIcons';
- interface ProviderListProps {
- providers: Provider[];
- loading: boolean;
- onEdit: (provider: Provider) => void;
- onManageQr: (provider: Provider) => void;
- onDelete: (provider: Provider) => void;
- onToggleStatus?: (provider: Provider) => Promise<void>;
- onCreateNew?: () => void;
- }
- function ProviderLogo({ name, icon, size = 'md' }: { name: string; icon?: string; size?: 'sm' | 'md' | 'lg' }) {
- const [failed, setFailed] = useState(false);
- const resolvedIcon = getPaymentProviderIcon(name, icon);
- const containerClasses = {
- sm: 'w-7 h-7 border border-black p-0.5',
- md: 'w-9 h-9 border-2 border-black p-1 shadow-[1px_1px_0px_#000]',
- lg: 'w-10 h-10 border-2 border-black p-1 shadow-[2px_2px_0px_#000]'
- }[size];
- return (
- <div className={`${containerClasses} bg-white flex items-center justify-center shrink-0 overflow-hidden`}>
- {resolvedIcon && !failed ? (
- <img
- src={resolvedIcon}
- alt={name}
- className="w-full h-full object-contain"
- onError={() => setFailed(true)}
- />
- ) : (
- <div className="w-full h-full bg-neutral-100 flex items-center justify-center font-black text-black text-xs uppercase">
- {name ? name.slice(0, 2) : <CreditCard size={14} />}
- </div>
- )}
- </div>
- );
- }
- export default function ProviderList({
- providers,
- loading,
- onEdit,
- onManageQr,
- onDelete,
- onToggleStatus,
- onCreateNew
- }: ProviderListProps) {
- const [viewMode, setViewMode] = useState<'grid' | 'table'>('grid');
- const [searchQuery, setSearchQuery] = useState('');
- const [statusFilter, setStatusFilter] = useState<'all' | 'active' | 'disabled'>('all');
- const [channelFilter, setChannelFilter] = useState<'all' | 'qr_static' | 'online_link'>('all');
- const [copiedCode, setCopiedCode] = useState<string | null>(null);
- const [togglingId, setTogglingId] = useState<number | null>(null);
- const handleCopyCode = (code: string) => {
- navigator.clipboard.writeText(code);
- setCopiedCode(code);
- setTimeout(() => setCopiedCode(null), 2000);
- };
- const handleToggle = async (provider: Provider) => {
- if (!onToggleStatus) return;
- setTogglingId(provider.id);
- try {
- await onToggleStatus(provider);
- } finally {
- setTogglingId(null);
- }
- };
- const filteredProviders = useMemo(() => {
- return providers.filter(p => {
- const q = searchQuery.toLowerCase().trim();
- const matchSearch = !q ||
- p.name.toLowerCase().includes(q) ||
- (p.title && p.title.toLowerCase().includes(q)) ||
- (p.currency && p.currency.toLowerCase().includes(q));
- const isEnabled = Boolean(p.enabled);
- const matchStatus =
- statusFilter === 'all' ||
- (statusFilter === 'active' && isEnabled) ||
- (statusFilter === 'disabled' && !isEnabled);
- const matchChannel =
- channelFilter === 'all' ||
- p.channel === channelFilter;
- return matchSearch && matchStatus && matchChannel;
- });
- }, [providers, searchQuery, statusFilter, channelFilter]);
- const renderChannelBadge = (channel: string) => {
- if (channel === 'qr_static') {
- return (
- <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-neutral-100 text-[10px] font-bold uppercase text-black">
- <QrCode size={11} />
- 静态收款码
- </span>
- );
- }
- return (
- <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-amber-100 text-[10px] font-bold uppercase text-black">
- <LinkIcon size={11} />
- 在线直连网关
- </span>
- );
- };
- const renderCurrencyBadge = (currency: string) => (
- <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-white text-[10px] font-black text-black">
- <Coins size={11} />
- {currency || 'CNY'}
- </span>
- );
- const renderFeeBadge = (p: Provider) => {
- const cfg = p.config;
- if (!cfg || cfg.fee_rate === undefined) return null;
- const rate = Number(cfg.fee_rate);
- const fixed = Number(cfg.fee_fixed || 0);
- const isFree = rate === 0 && fixed === 0;
- return isFree ? (
- <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-emerald-100 text-[10px] font-bold text-emerald-900">
- 免手续费
- </span>
- ) : (
- <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-amber-100 text-[10px] font-bold text-amber-950">
- 手续费: {cfg.fee_label || `${(rate * 100).toFixed(1)}%`}
- </span>
- );
- };
- return (
- <div className="space-y-4 font-mono">
- {/* 顶部工具栏 */}
- <div className="bg-white p-4 border-2 border-black shadow-[4px_4px_0px_#000] flex flex-col md:flex-row md:items-center justify-between gap-3">
-
- {/* 搜索框 */}
- <div className="relative flex-1 max-w-md">
- <Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-black pointer-events-none" />
- <input
- type="text"
- placeholder="搜索渠道名称 / 标识 / 币种..."
- value={searchQuery}
- onChange={e => setSearchQuery(e.target.value)}
- className="w-full pl-9 pr-8 py-2 text-xs bg-neutral-50 border-2 border-black focus:bg-white outline-none shadow-[2px_2px_0px_#000] text-black font-mono"
- />
- {searchQuery && (
- <button
- onClick={() => setSearchQuery('')}
- className="absolute right-2.5 top-1/2 -translate-y-1/2 text-xs text-black hover:text-rose-600 font-bold"
- >
- ✕
- </button>
- )}
- </div>
- {/* 筛选与视图切换 */}
- <div className="flex items-center gap-2 flex-wrap">
- <select
- value={channelFilter}
- onChange={e => setChannelFilter(e.target.value as any)}
- className="text-xs font-bold bg-neutral-50 border-2 border-black px-2.5 py-1.5 text-black outline-none shadow-[2px_2px_0px_#000]"
- >
- <option value="all">[ 全部通道类型 ]</option>
- <option value="qr_static">静态二维码 (QR)</option>
- <option value="online_link">在线直连 (Link)</option>
- </select>
- <select
- value={statusFilter}
- onChange={e => setStatusFilter(e.target.value as any)}
- className="text-xs font-bold bg-neutral-50 border-2 border-black px-2.5 py-1.5 text-black outline-none shadow-[2px_2px_0px_#000]"
- >
- <option value="all">[ 全部状态 ]</option>
- <option value="active">已启用</option>
- <option value="disabled">已禁用</option>
- </select>
- <div className="flex items-center border-2 border-black bg-neutral-100 shadow-[2px_2px_0px_#000]">
- <button
- onClick={() => setViewMode('grid')}
- className={`p-1.5 transition ${viewMode === 'grid' ? 'bg-amber-300 text-black font-bold' : 'text-neutral-500 hover:text-black'}`}
- title="卡片视图"
- >
- <LayoutGrid size={14} />
- </button>
- <button
- onClick={() => setViewMode('table')}
- className={`p-1.5 transition ${viewMode === 'table' ? 'bg-amber-300 text-black font-bold' : 'text-neutral-500 hover:text-black'}`}
- title="表格视图"
- >
- <List size={14} />
- </button>
- </div>
- </div>
- </div>
- {/* 内容区域 */}
- {loading ? (
- <div className="bg-white p-12 text-center border-2 border-black shadow-[4px_4px_0px_#000]">
- <Loader2 className="animate-spin text-black mx-auto mb-2" size={24} />
- <p className="text-xs font-black uppercase">[ FETCHING PAYMENT GATEWAYS... ]</p>
- </div>
- ) : filteredProviders.length === 0 ? (
- <div className="bg-white p-12 text-center border-2 border-black shadow-[4px_4px_0px_#000] space-y-3">
- <div className="w-10 h-10 bg-neutral-100 border-2 border-black flex items-center justify-center mx-auto shadow-[2px_2px_0px_#000] text-black">
- <Layers size={18} />
- </div>
- <h3 className="text-sm font-black text-black uppercase">[ NO GATEWAYS FOUND ]</h3>
- <p className="text-xs text-neutral-500 font-sans">未检索到匹配的支付渠道配置</p>
- </div>
- ) : viewMode === 'grid' ? (
- <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
- {filteredProviders.map((p) => {
- const isEnabled = Boolean(p.enabled);
- const isToggling = togglingId === p.id;
- return (
- <div
- key={p.id}
- className={`bg-white border-2 border-black shadow-[4px_4px_0px_#000] flex flex-col justify-between transition ${
- isEnabled ? '' : 'bg-neutral-100 opacity-80'
- }`}
- >
- {/* 卡片头部 */}
- <div className="p-4 space-y-3">
- <div className="flex items-start justify-between gap-2">
- <div className="flex items-center gap-2.5 min-w-0">
- <ProviderLogo name={p.name} icon={p.icon} size="lg" />
- <div className="min-w-0">
- <h4 className="text-sm font-black text-black truncate font-sans" title={p.title || p.name}>
- {p.title || p.name}
- </h4>
- <div className="flex items-center gap-1 text-[10px] text-neutral-500 font-mono mt-0.5">
- <span>{p.name}</span>
- <button
- onClick={() => handleCopyCode(p.name)}
- className="hover:text-black"
- title="复制标识代码"
- >
- {copiedCode === p.name ? <Check size={10} className="text-black" /> : <Copy size={10} />}
- </button>
- </div>
- </div>
- </div>
- <button
- onClick={() => handleToggle(p)}
- disabled={isToggling || !onToggleStatus}
- className={`p-1 border border-black text-xs font-bold ${
- isEnabled ? 'bg-amber-300 text-black' : 'bg-neutral-200 text-neutral-500'
- }`}
- title={isEnabled ? '点击禁用' : '点击启用'}
- >
- {isToggling ? <Loader2 size={14} className="animate-spin" /> : isEnabled ? 'ON' : 'OFF'}
- </button>
- </div>
- <div className="pt-2 border-t border-dashed border-neutral-300 flex items-center gap-1.5 flex-wrap">
- {renderChannelBadge(p.channel)}
- {renderCurrencyBadge(p.currency)}
- {renderFeeBadge(p)}
- </div>
- </div>
- {/* 卡片底部操作栏 */}
- <div className="px-4 py-2.5 bg-neutral-100 border-t-2 border-black flex items-center justify-between gap-2 text-xs">
- <div>
- {p.channel === 'qr_static' ? (
- <button
- onClick={() => onManageQr(p)}
- className="px-2.5 py-1 bg-white hover:bg-amber-300 text-black border border-black font-bold text-[11px] shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 flex items-center gap-1 uppercase"
- >
- <QrCode size={12} />
- <span>[ 码池 ]</span>
- </button>
- ) : (
- <span className="text-[10px] text-neutral-500 font-bold uppercase">
- ONLINE GATEWAY
- </span>
- )}
- </div>
- <div className="flex items-center gap-1.5">
- <button
- onClick={() => onEdit(p)}
- className="px-2 py-1 bg-white hover:bg-neutral-200 text-black border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
- title="编辑配置"
- >
- [ 编辑 ]
- </button>
- <button
- onClick={() => onDelete(p)}
- className="px-2 py-1 bg-rose-100 hover:bg-rose-200 text-rose-800 border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
- title="删除渠道"
- >
- [ 删除 ]
- </button>
- </div>
- </div>
- </div>
- );
- })}
- </div>
- ) : (
- /* 表格视图 */
- <div className="bg-white border-2 border-black shadow-[4px_4px_0px_#000] overflow-hidden">
- <div className="overflow-x-auto">
- <table className="min-w-full divide-y-2 divide-black text-left">
- <thead className="bg-neutral-100 text-xs font-black text-black uppercase">
- <tr>
- <th className="px-4 py-3">[ 服务商 / 标识 ]</th>
- <th className="px-4 py-3">[ 通道类型 ]</th>
- <th className="px-4 py-3">[ 结算货币 ]</th>
- <th className="px-4 py-3">[ 交易手续费 ]</th>
- <th className="px-4 py-3">[ 状态 ]</th>
- <th className="px-4 py-3 text-right">[ 操作 ]</th>
- </tr>
- </thead>
- <tbody className="divide-y divide-black text-xs">
- {filteredProviders.map((p) => {
- const isEnabled = Boolean(p.enabled);
- const isToggling = togglingId === p.id;
- return (
- <tr key={p.id} className="hover:bg-amber-50/70">
- <td className="px-4 py-3">
- <div className="flex items-center gap-2.5">
- <ProviderLogo name={p.name} icon={p.icon} size="sm" />
- <div>
- <div className="font-bold text-black font-sans">{p.title || p.name}</div>
- <div className="text-[10px] font-mono text-neutral-500">{p.name}</div>
- </div>
- </div>
- </td>
- <td className="px-4 py-3 whitespace-nowrap">
- {renderChannelBadge(p.channel)}
- </td>
- <td className="px-4 py-3 whitespace-nowrap">
- {renderCurrencyBadge(p.currency)}
- </td>
- <td className="px-4 py-3 whitespace-nowrap">
- {renderFeeBadge(p)}
- </td>
- <td className="px-4 py-3 whitespace-nowrap">
- <button
- onClick={() => handleToggle(p)}
- disabled={isToggling || !onToggleStatus}
- className={`px-2 py-0.5 border border-black text-[10px] font-black ${
- isEnabled ? 'bg-amber-300 text-black' : 'bg-neutral-200 text-neutral-500'
- }`}
- >
- {isToggling ? <Loader2 size={12} className="animate-spin" /> : isEnabled ? '已启用' : '已禁用'}
- </button>
- </td>
- <td className="px-4 py-3 text-right whitespace-nowrap">
- <div className="inline-flex items-center gap-1.5">
- {p.channel === 'qr_static' && (
- <button
- onClick={() => onManageQr(p)}
- className="px-2 py-1 bg-white hover:bg-amber-300 text-black border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- 码池
- </button>
- )}
- <button
- onClick={() => onEdit(p)}
- className="px-2 py-1 bg-white hover:bg-neutral-200 text-black border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- 编辑
- </button>
- <button
- onClick={() => onDelete(p)}
- className="px-2 py-1 bg-rose-100 hover:bg-rose-200 text-rose-800 border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- 删除
- </button>
- </div>
- </td>
- </tr>
- );
- })}
- </tbody>
- </table>
- </div>
- </div>
- )}
- </div>
- );
- }
|