acupedia-web-ui/src/components/coupons/CouponsTab.tsx

350 lines
18 KiB
TypeScript

import React, { useState } from 'react';
import { Ticket, Plus, Check, Copy, Edit, Trash2 } from 'lucide-react';
import { Pagination } from '../common/Pagination';
interface CouponsTabProps {
coupons: any[];
filterStatus: 'all' | 'active' | 'scheduled' | 'expired';
searchQuery: string;
currentPage: number;
perPage: number;
onFilterStatusChange: (status: 'all' | 'active' | 'scheduled' | 'expired') => void;
onSearchChange: (query: string) => void;
onPageChange: (page: number) => void;
onPerPageChange: (perPage: number) => void;
onOpenCreateCoupon: () => void;
onOpenEditCoupon: (coupon: any) => void;
onToggleStatus: (coupon: any) => void;
onDeleteCoupon: (coupon: any) => void;
}
export const CouponsTab: React.FC<CouponsTabProps> = ({
coupons,
filterStatus,
searchQuery,
currentPage,
perPage,
onFilterStatusChange,
onSearchChange,
onPageChange,
onPerPageChange,
onOpenCreateCoupon,
onOpenEditCoupon,
onToggleStatus,
onDeleteCoupon,
}) => {
const [copiedCode, setCopiedCode] = useState<string | null>(null);
const handleCopyCode = (code: string) => {
navigator.clipboard.writeText(code);
setCopiedCode(code);
setTimeout(() => setCopiedCode(null), 2000);
};
const filteredCoupons = coupons.filter((c) => {
if (filterStatus !== 'all' && c.schedule_status !== filterStatus) return false;
if (searchQuery.trim()) {
const q = searchQuery.toLowerCase();
return (
c.code.toLowerCase().includes(q) ||
(c.description && c.description.toLowerCase().includes(q))
);
}
return true;
});
const totalFiltered = filteredCoupons.length;
const totalPages = Math.max(1, Math.ceil(totalFiltered / perPage));
const safePage = Math.min(currentPage, totalPages);
const startIndex = (safePage - 1) * perPage;
const paginatedCoupons = filteredCoupons.slice(startIndex, startIndex + perPage);
return (
<div className="space-y-5">
{/* Top KPI Cards */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
<div className="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 text-center">
<p className="text-2xl font-black text-slate-800">{coupons.length}</p>
<p className="text-[10px] font-bold text-slate-400 uppercase tracking-wider mt-1">Total Promo Codes</p>
</div>
<div className="bg-emerald-50/60 border border-emerald-200/60 rounded-2xl p-4 text-center">
<div className="flex items-center justify-center gap-1.5">
<div className="h-2 w-2 rounded-full bg-emerald-500 animate-pulse" />
<p className="text-2xl font-black text-emerald-700">
{coupons.filter((c) => c.schedule_status === 'active').length}
</p>
</div>
<p className="text-[10px] font-bold text-emerald-600 uppercase tracking-wider mt-1">Active Now</p>
</div>
<div className="bg-amber-50/60 border border-amber-200/60 rounded-2xl p-4 text-center">
<p className="text-2xl font-black text-amber-700">
{coupons.filter((c) => c.schedule_status === 'scheduled').length}
</p>
<p className="text-[10px] font-bold text-amber-600 uppercase tracking-wider mt-1">Scheduled / Upcoming</p>
</div>
<div className="bg-indigo-50/60 border border-indigo-200/60 rounded-2xl p-4 text-center">
<p className="text-2xl font-black text-indigo-700">
{coupons.reduce((acc, c) => acc + (c.usage_count || 0), 0)}
</p>
<p className="text-[10px] font-bold text-indigo-600 uppercase tracking-wider mt-1">Total Redemptions</p>
</div>
</div>
{/* Filter & Action Controls */}
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 bg-slate-50/80 border border-slate-200/60 rounded-2xl p-4">
{/* Filter Pills */}
<div className="flex flex-wrap items-center gap-1.5">
{(['all', 'active', 'scheduled', 'expired'] as const).map((status) => (
<button
key={status}
type="button"
onClick={() => {
onFilterStatusChange(status);
onPageChange(1);
}}
className={`px-3 py-1.5 rounded-xl text-xs font-bold capitalize transition-all cursor-pointer ${
filterStatus === status
? 'bg-indigo-600 text-white shadow-sm'
: 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-100 shadow-sm'
}`}
>
{status === 'all'
? `All (${coupons.length})`
: `${status} (${coupons.filter((c) => c.schedule_status === status).length})`}
</button>
))}
</div>
<div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-2.5 sm:gap-3 w-full sm:w-auto">
<input
type="text"
placeholder="Search code or description..."
value={searchQuery}
onChange={(e) => {
onSearchChange(e.target.value);
onPageChange(1);
}}
className="bg-white border border-slate-200 rounded-xl px-3 py-1.5 text-xs text-slate-800 placeholder:text-slate-400 focus:outline-none focus:border-indigo-500 w-full sm:w-56 shadow-sm"
/>
<button
type="button"
onClick={onOpenCreateCoupon}
className="inline-flex items-center justify-center gap-1.5 px-4 py-2 rounded-xl text-xs font-bold bg-indigo-600 hover:bg-indigo-500 text-white shadow-md shadow-indigo-600/20 transition-all cursor-pointer whitespace-nowrap"
>
<Plus className="h-3.5 w-3.5" /> Create Coupon
</button>
</div>
</div>
{/* Coupons Table */}
{filteredCoupons.length === 0 ? (
<div className="text-center py-16 bg-slate-50/50 rounded-2xl border border-slate-200/60">
<Ticket className="h-10 w-10 text-slate-300 mx-auto mb-2" />
<p className="text-sm font-bold text-slate-600">No promotional coupons found</p>
<p className="text-xs text-slate-400 mt-0.5">Click "+ Create Coupon" above to launch a new discount code.</p>
</div>
) : (
<div className="border border-slate-200 rounded-2xl overflow-hidden shadow-xs p-4 bg-white">
<div className="overflow-x-auto">
<table className="w-full text-left text-xs min-w-[720px]">
<thead className="bg-slate-50 border-b border-slate-200 text-slate-500 uppercase tracking-wider text-[10px] font-extrabold">
<tr>
<th className="py-3 px-4">Coupon Code</th>
<th className="py-3 px-4">Discount Value</th>
<th className="py-3 px-4">Schedule / Status</th>
<th className="py-3 px-4">Redemptions</th>
<th className="py-3 px-4">Scope & Conditions</th>
<th className="py-3 px-4 text-center">Active</th>
<th className="py-3 px-4 text-right">Actions</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{paginatedCoupons.map((c) => {
const isPercent = c.discount_type === 'percentage';
const hasCap = isPercent && c.max_discount_limit > 0;
const usageLimit = c.usage_limit || 0;
const usageCount = c.usage_count || 0;
const hasSpecific =
(c.applicable_plan_ids && c.applicable_plan_ids.length > 0) ||
(c.applicable_module_ids && c.applicable_module_ids.length > 0);
return (
<tr key={c.id} className="hover:bg-slate-50/70 transition-colors">
{/* Code */}
<td className="py-3.5 px-4 whitespace-nowrap">
<div className="flex items-center gap-1.5">
<span className="font-mono font-black text-indigo-700 bg-indigo-50 border border-indigo-200/80 px-2.5 py-1 rounded-lg text-xs tracking-wider">
{c.code}
</span>
<button
type="button"
onClick={() => handleCopyCode(c.code)}
title="Copy promo code"
className="text-slate-400 hover:text-indigo-600 p-1 transition-colors cursor-pointer"
>
{copiedCode === c.code ? (
<Check className="h-3.5 w-3.5 text-emerald-500" />
) : (
<Copy className="h-3.5 w-3.5" />
)}
</button>
</div>
{c.description && (
<p className="text-[10px] text-slate-400 font-semibold mt-1 max-w-[200px] truncate">
{c.description}
</p>
)}
</td>
{/* Value */}
<td className="py-3.5 px-4 whitespace-nowrap">
<div className="inline-flex items-center gap-1 font-extrabold text-slate-800 text-xs">
{isPercent ? (
`${c.value}% OFF`
) : (
<div className="flex items-center gap-1.5 flex-wrap">
<span>₹{c.value_inr ?? c.value}</span>
{c.value_usd !== undefined && c.value_usd !== null && (
<span className="text-slate-400 font-medium">/ ${c.value_usd}</span>
)}
{c.value_eur !== undefined && c.value_eur !== null && (
<span className="text-slate-400 font-medium">/ €{c.value_eur}</span>
)}
<span className="text-[10px] font-bold text-slate-500">FLAT</span>
</div>
)}
</div>
{hasCap && (
<p className="text-[10px] text-slate-400 font-semibold mt-0.5">
Cap: ₹{c.max_discount_inr ?? c.max_discount_limit}
{c.max_discount_usd !== undefined && c.max_discount_usd !== null && ` / $${c.max_discount_usd}`}
{c.max_discount_eur !== undefined && c.max_discount_eur !== null && ` / €${c.max_discount_eur}`}
</p>
)}
<span className="block text-[9px] font-bold text-slate-400 capitalize mt-0.5">
Category: {c.coupon_category || 'General'}
</span>
</td>
{/* Schedule */}
<td className="py-3.5 px-4 whitespace-nowrap">
<span
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-extrabold tracking-wider uppercase border ${
c.schedule_status === 'active'
? 'bg-emerald-50 text-emerald-700 border-emerald-200'
: c.schedule_status === 'scheduled'
? 'bg-amber-50 text-amber-700 border-amber-200'
: c.schedule_status === 'expired'
? 'bg-rose-50 text-rose-700 border-rose-200'
: 'bg-slate-100 text-slate-500 border-slate-200'
}`}
>
{c.schedule_status === 'active' && (
<span className="h-1.5 w-1.5 rounded-full bg-emerald-500 animate-pulse" />
)}
{c.schedule_status}
</span>
<p className="text-[10px] text-slate-400 font-medium mt-1">
{c.start_date?.split('T')[0] || '—'} → {c.end_date?.split('T')[0] || '—'}
</p>
</td>
{/* Usage */}
<td className="py-3.5 px-4 whitespace-nowrap">
<p className="font-bold text-slate-700 text-xs">
{usageCount} {usageLimit > 0 ? `/ ${usageLimit}` : 'uses'}
</p>
{usageLimit > 0 ? (
<div className="w-24 bg-slate-100 rounded-full h-1.5 mt-1.5 overflow-hidden">
<div
className="bg-indigo-600 h-1.5 rounded-full transition-all"
style={{ width: `${Math.min(100, (usageCount / usageLimit) * 100)}%` }}
/>
</div>
) : (
<span className="text-[9px] text-slate-400 font-semibold">Unlimited</span>
)}
</td>
{/* Scope & Conditions */}
<td className="py-3.5 px-4">
<div className="flex flex-wrap gap-1">
<span className="text-[9px] font-extrabold px-1.5 py-0.5 rounded bg-slate-100 text-slate-600 border border-slate-200">
{hasSpecific
? c.applicable_plan_ids && c.applicable_plan_ids.length > 0 && (!c.applicable_module_ids || c.applicable_module_ids.length === 0)
? `Restricted to ${c.plan_names?.length || 1} Plan${(c.plan_names?.length || 1) > 1 ? 's' : ''}`
: 'Targeted Items'
: 'Storewide'}
</span>
{(c.min_purchase_amount > 0 || (c.min_purchase_inr && c.min_purchase_inr > 0)) && (
<span className="text-[9px] font-bold px-1.5 py-0.5 rounded bg-blue-50 text-blue-700 border border-blue-200">
Min: ₹{c.min_purchase_inr ?? c.min_purchase_amount}
{c.min_purchase_usd !== undefined && c.min_purchase_usd !== null && c.min_purchase_usd > 0 && ` / $${c.min_purchase_usd}`}
{c.min_purchase_eur !== undefined && c.min_purchase_eur !== null && c.min_purchase_eur > 0 && ` / €${c.min_purchase_eur}`}
</span>
)}
{c.is_first_purchase_only && (
<span className="text-[9px] font-bold px-1.5 py-0.5 rounded bg-purple-50 text-purple-700 border border-purple-200">
1st Order Only
</span>
)}
</div>
</td>
{/* Active Switch */}
<td className="py-3.5 px-4 text-center whitespace-nowrap">
<button
type="button"
onClick={() => onToggleStatus(c)}
className={`relative inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${
c.is_active ? 'bg-indigo-600' : 'bg-slate-200'
}`}
>
<span
className={`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${
c.is_active ? 'translate-x-4' : 'translate-x-0'
}`}
/>
</button>
</td>
{/* Actions */}
<td className="py-3.5 px-4 text-right whitespace-nowrap">
<div className="flex items-center justify-end gap-1.5">
<button
type="button"
onClick={() => onOpenEditCoupon(c)}
className="inline-flex items-center gap-1 px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 hover:bg-indigo-50 hover:text-indigo-600 text-slate-600 transition-colors border border-slate-200 cursor-pointer shadow-sm"
>
<Edit className="h-3 w-3" /> Edit
</button>
<button
type="button"
onClick={() => onDeleteCoupon(c)}
className="inline-flex items-center p-1 rounded-lg text-slate-400 hover:bg-rose-50 hover:text-rose-600 transition-colors border border-slate-200 cursor-pointer shadow-sm"
title="Delete coupon"
>
<Trash2 className="h-3.5 w-3.5" />
</button>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
<Pagination
currentPage={safePage}
totalItems={totalFiltered}
itemsPerPage={perPage}
onPageChange={onPageChange}
onItemsPerPageChange={onPerPageChange}
/>
</div>
)}
</div>
);
};