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 = ({ coupons, filterStatus, searchQuery, currentPage, perPage, onFilterStatusChange, onSearchChange, onPageChange, onPerPageChange, onOpenCreateCoupon, onOpenEditCoupon, onToggleStatus, onDeleteCoupon, }) => { const [copiedCode, setCopiedCode] = useState(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 (
{/* Top KPI Cards */}

{coupons.length}

Total Promo Codes

{coupons.filter((c) => c.schedule_status === 'active').length}

Active Now

{coupons.filter((c) => c.schedule_status === 'scheduled').length}

Scheduled / Upcoming

{coupons.reduce((acc, c) => acc + (c.usage_count || 0), 0)}

Total Redemptions

{/* Filter & Action Controls */}
{/* Filter Pills */}
{(['all', 'active', 'scheduled', 'expired'] as const).map((status) => ( ))}
{ 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" />
{/* Coupons Table */} {filteredCoupons.length === 0 ? (

No promotional coupons found

Click "+ Create Coupon" above to launch a new discount code.

) : (
{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 ( {/* Code */} {/* Value */} {/* Schedule */} {/* Usage */} {/* Scope & Conditions */} {/* Active Switch */} {/* Actions */} ); })}
Coupon Code Discount Value Schedule / Status Redemptions Scope & Conditions Active Actions
{c.code}
{c.description && (

{c.description}

)}
{isPercent ? ( `${c.value}% OFF` ) : (
₹{c.value_inr ?? c.value} {c.value_usd !== undefined && c.value_usd !== null && ( / ${c.value_usd} )} {c.value_eur !== undefined && c.value_eur !== null && ( / €{c.value_eur} )} FLAT
)}
{hasCap && (

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}`}

)} Category: {c.coupon_category || 'General'}
{c.schedule_status === 'active' && ( )} {c.schedule_status}

{c.start_date?.split('T')[0] || '—'} → {c.end_date?.split('T')[0] || '—'}

{usageCount} {usageLimit > 0 ? `/ ${usageLimit}` : 'uses'}

{usageLimit > 0 ? (
) : ( Unlimited )}
{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'} {(c.min_purchase_amount > 0 || (c.min_purchase_inr && c.min_purchase_inr > 0)) && ( 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}`} )} {c.is_first_purchase_only && ( 1st Order Only )}
)}
); };