240 lines
11 KiB
TypeScript
240 lines
11 KiB
TypeScript
import React, { useState, useMemo } from 'react';
|
|
import { Pagination } from '../common/Pagination';
|
|
import { ArrowUpDown, ArrowDown, ArrowUp } from 'lucide-react';
|
|
|
|
interface SubscriptionsTabProps {
|
|
subscriptions: any[];
|
|
filterCustomerName: string;
|
|
filterModuleName: string;
|
|
currentPage: number;
|
|
perPage: number;
|
|
onFilterCustomerChange: (v: string) => void;
|
|
onFilterModuleChange: (v: string) => void;
|
|
onPageChange: (p: number) => void;
|
|
onPerPageChange: (pp: number) => void;
|
|
}
|
|
|
|
export const SubscriptionsTab: React.FC<SubscriptionsTabProps> = ({
|
|
subscriptions,
|
|
filterCustomerName,
|
|
filterModuleName,
|
|
currentPage,
|
|
perPage,
|
|
onFilterCustomerChange,
|
|
onFilterModuleChange,
|
|
onPageChange,
|
|
onPerPageChange,
|
|
}) => {
|
|
const [sortOrder, setSortOrder] = useState<'desc' | 'asc'>('desc');
|
|
|
|
const processedSubs = useMemo(() => {
|
|
let result = subscriptions.filter((sub: any) => {
|
|
const custName = (sub.user_name || '').toLowerCase();
|
|
const custEmail = (sub.user_email || '').toLowerCase();
|
|
const modName = (sub.plan_name || '').toLowerCase();
|
|
const modCode = (sub.plan_code || '').toLowerCase();
|
|
|
|
const queryCust = filterCustomerName.toLowerCase();
|
|
const queryMod = filterModuleName.toLowerCase();
|
|
|
|
const matchCust = !queryCust || custName.includes(queryCust) || custEmail.includes(queryCust);
|
|
const matchMod = !queryMod || modName.includes(queryMod) || modCode.includes(queryMod);
|
|
|
|
return matchCust && matchMod;
|
|
});
|
|
|
|
result.sort((a: any, b: any) => {
|
|
const timeA = a.started_at ? new Date(a.started_at).getTime() : 0;
|
|
const timeB = b.started_at ? new Date(b.started_at).getTime() : 0;
|
|
return sortOrder === 'desc' ? timeB - timeA : timeA - timeB;
|
|
});
|
|
|
|
return result;
|
|
}, [subscriptions, filterCustomerName, filterModuleName, sortOrder]);
|
|
|
|
const totalFiltered = processedSubs.length;
|
|
const totalPages = Math.max(1, Math.ceil(totalFiltered / perPage));
|
|
const safePage = Math.min(currentPage, totalPages);
|
|
const startIndex = (safePage - 1) * perPage;
|
|
const paginatedSubs = processedSubs.slice(startIndex, startIndex + perPage);
|
|
|
|
return (
|
|
<div className="space-y-5">
|
|
{/* Search Filters & Sort Controls */}
|
|
<div className="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 space-y-3">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-[10px] uppercase font-bold text-slate-500 mb-1.5">
|
|
Filter by Customer (Name / Email)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
placeholder="Search customer name or email..."
|
|
value={filterCustomerName}
|
|
onChange={(e) => {
|
|
onFilterCustomerChange(e.target.value);
|
|
onPageChange(1);
|
|
}}
|
|
className="w-full bg-white border border-slate-200 rounded-xl px-3.5 py-2 text-xs text-slate-800 font-bold focus:outline-none focus:border-indigo-500 transition-all placeholder:text-slate-400 shadow-sm"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[10px] uppercase font-bold text-slate-500 mb-1.5">
|
|
Filter by Module (Name / Code)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
placeholder="Search plan name or code..."
|
|
value={filterModuleName}
|
|
onChange={(e) => {
|
|
onFilterModuleChange(e.target.value);
|
|
onPageChange(1);
|
|
}}
|
|
className="w-full bg-white border border-slate-200 rounded-xl px-3.5 py-2 text-xs text-slate-800 font-bold focus:outline-none focus:border-indigo-500 transition-all placeholder:text-slate-400 shadow-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Sort & Count Bar */}
|
|
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-2 pt-2 border-t border-slate-200/60">
|
|
<button
|
|
type="button"
|
|
onClick={() => setSortOrder(prev => prev === 'desc' ? 'asc' : 'desc')}
|
|
className="flex items-center gap-1.5 px-3 py-1.5 rounded-xl text-xs font-bold bg-white border border-slate-200 text-slate-700 hover:bg-slate-100 hover:border-slate-300 transition-colors shadow-2xs cursor-pointer"
|
|
title={`Toggle sort order (current: ${sortOrder === 'desc' ? 'Latest first' : 'Oldest first'})`}
|
|
>
|
|
<ArrowUpDown className="h-3.5 w-3.5 text-indigo-600" />
|
|
<span>Sort: {sortOrder === 'desc' ? 'Newest Activated First' : 'Oldest Activated First'}</span>
|
|
{sortOrder === 'desc' ? (
|
|
<ArrowDown className="h-3 w-3 text-slate-400" />
|
|
) : (
|
|
<ArrowUp className="h-3 w-3 text-slate-400" />
|
|
)}
|
|
</button>
|
|
|
|
<span className="text-xs font-bold text-slate-500">
|
|
Showing <span className="text-slate-800">{totalFiltered}</span> of{' '}
|
|
<span className="text-slate-800">{subscriptions.length}</span> subscriptions
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{totalFiltered === 0 ? (
|
|
<p className="text-sm text-slate-400 italic text-center py-10 font-semibold">
|
|
No subscriptions match the active filters.
|
|
</p>
|
|
) : (
|
|
<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-xs min-w-[700px]">
|
|
<thead>
|
|
<tr className="border-b border-slate-200 bg-slate-50/80 text-[10px] uppercase font-bold text-slate-400 tracking-wider">
|
|
<th className="text-left py-3 px-3">Subscriber</th>
|
|
<th className="text-left py-3 px-3">Phone</th>
|
|
<th className="text-left py-3 px-3">Access Plan</th>
|
|
<th className="text-left py-3 px-3">Status</th>
|
|
<th className="text-left py-3 px-3">Activated</th>
|
|
<th className="text-left py-3 px-3">Expires</th>
|
|
<th className="text-left py-3 px-3">Time Left</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{paginatedSubs.map((sub: any, i: number) => {
|
|
const daysLeft = sub.days_left;
|
|
return (
|
|
<tr key={sub.id || i} className="border-b border-slate-100 hover:bg-slate-50 transition-colors">
|
|
<td className="py-3 px-3">
|
|
<p className="font-bold text-slate-800">{sub.user_name || 'Subscriber'}</p>
|
|
<p className="text-[10px] text-slate-400 font-mono mt-0.5">{sub.user_email}</p>
|
|
{sub.user_external_ref && (
|
|
<p className="text-[9px] text-slate-400 font-mono mt-0.5">
|
|
Cust:{' '}
|
|
<span className="bg-slate-100 text-slate-600 px-1 py-0.2 rounded font-bold">
|
|
{sub.user_external_ref}
|
|
</span>
|
|
</p>
|
|
)}
|
|
</td>
|
|
<td className="py-3 px-3 text-slate-600 font-semibold">{sub.user_phone || '—'}</td>
|
|
<td className="py-3 px-3">
|
|
<p className="font-bold text-slate-800">{sub.plan_name}</p>
|
|
<p className="text-[9px] text-indigo-500 font-bold uppercase tracking-wider mt-0.5">
|
|
{sub.plan_code}
|
|
</p>
|
|
{sub.payment_reference && (
|
|
<p className="text-[9px] text-slate-400 font-mono mt-0.5">
|
|
Order:{' '}
|
|
<span className="bg-slate-100 text-slate-600 px-1 py-0.2 rounded font-bold">
|
|
{sub.payment_reference}
|
|
</span>
|
|
</p>
|
|
)}
|
|
</td>
|
|
<td className="py-3 px-3">
|
|
<span
|
|
className={`px-2 py-0.5 rounded-full text-[10px] font-black uppercase ${
|
|
sub.status === 'active'
|
|
? 'bg-emerald-50 text-emerald-700 border border-emerald-200'
|
|
: sub.status === 'refunded'
|
|
? 'bg-rose-50 text-rose-700 border border-rose-200'
|
|
: 'bg-slate-100 text-slate-600 border border-slate-200'
|
|
}`}
|
|
>
|
|
{sub.status}
|
|
</span>
|
|
</td>
|
|
<td className="py-3 px-3 text-slate-600 font-semibold">
|
|
{sub.started_at
|
|
? new Date(sub.started_at).toLocaleDateString(undefined, { dateStyle: 'medium' })
|
|
: '—'}
|
|
</td>
|
|
<td className="py-3 px-3 text-slate-600 font-semibold">
|
|
{sub.expires_at
|
|
? new Date(sub.expires_at).toLocaleDateString(undefined, { dateStyle: 'medium' })
|
|
: sub.status === 'active'
|
|
? 'Lifetime'
|
|
: '—'}
|
|
</td>
|
|
<td className="py-3 px-3">
|
|
{sub.status === 'active' ? (
|
|
!sub.expires_at ? (
|
|
<span className="text-[10px] font-bold text-emerald-600">Lifetime</span>
|
|
) : daysLeft !== undefined && daysLeft !== null ? (
|
|
<span
|
|
className={`text-[10px] font-bold px-2 py-0.5 rounded-full ${
|
|
daysLeft > 30
|
|
? 'bg-emerald-50 text-emerald-700'
|
|
: daysLeft > 7
|
|
? 'bg-amber-50 text-amber-700'
|
|
: 'bg-rose-50 text-rose-700'
|
|
}`}
|
|
>
|
|
{daysLeft}d left
|
|
</span>
|
|
) : (
|
|
<span className="text-[10px] text-slate-400">Active</span>
|
|
)
|
|
) : (
|
|
<span className="text-[10px] text-slate-400">Expired</span>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<Pagination
|
|
currentPage={safePage}
|
|
totalItems={totalFiltered}
|
|
itemsPerPage={perPage}
|
|
onPageChange={onPageChange}
|
|
onItemsPerPageChange={onPerPageChange}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|