acupedia-web-ui/src/components/plans/PlansTab.tsx

330 lines
16 KiB
TypeScript

import React, { useState } from 'react';
import { Plus, Tag, Users, Edit, Sparkles } from 'lucide-react';
import { Pagination } from '../common/Pagination';
interface PlansTabProps {
plans: any[];
searchQuery: string;
typeFilter: string;
currentPage: number;
perPage: number;
onSearchChange: (q: string) => void;
onTypeFilterChange: (f: string) => void;
onPageChange: (p: number) => void;
onPerPageChange: (pp: number) => void;
onOpenCreatePlan: () => void;
onTogglePlanStatus: (plan: any) => void;
onOpenEditPrice: (plan: any) => void;
}
export const PlansTab: React.FC<PlansTabProps> = ({
plans,
searchQuery,
typeFilter,
currentPage,
perPage,
onSearchChange,
onTypeFilterChange,
onPageChange,
onPerPageChange,
onOpenCreatePlan,
onTogglePlanStatus,
onOpenEditPrice,
}) => {
const [expandedModules, setExpandedModules] = useState<Record<string, boolean>>({});
const toggleExpandModules = (planKey: string) => {
setExpandedModules((prev) => ({ ...prev, [planKey]: !prev[planKey] }));
};
const filteredPlans = plans.filter((plan) => {
const q = searchQuery.toLowerCase().trim();
const matchesSearch =
!q ||
plan.base_code?.toLowerCase().includes(q) ||
plan.name?.toLowerCase().includes(q) ||
plan.description?.toLowerCase().includes(q) ||
(plan.modules &&
plan.modules.some(
(m: any) => m.name?.toLowerCase().includes(q) || m.slug?.toLowerCase().includes(q)
));
if (!matchesSearch) return false;
if (typeFilter === 'bundle') return plan.plan_type === 'bundle';
if (typeFilter === 'one_time') return plan.plan_type !== 'bundle';
if (typeFilter === 'active') return plan.is_active === true;
if (typeFilter === 'inactive') return plan.is_active === false;
return true;
});
const totalFiltered = filteredPlans.length;
const totalPages = Math.max(1, Math.ceil(totalFiltered / perPage));
const safePage = Math.min(currentPage, totalPages);
const startIndex = (safePage - 1) * perPage;
const paginatedPlans = filteredPlans.slice(startIndex, startIndex + perPage);
return (
<div className="space-y-5">
{/* Header & Filter Bar */}
<div className="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 space-y-3.5">
<div className="flex flex-col sm:flex-row gap-3 items-center justify-between">
<div className="w-full sm:w-80 relative">
<input
type="text"
placeholder="Search plan code, name, or module..."
value={searchQuery}
onChange={(e) => {
onSearchChange(e.target.value);
onPageChange(1);
}}
className="w-full bg-white border border-slate-200 rounded-xl pl-9 pr-4 py-2 text-xs text-slate-800 font-semibold focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 shadow-sm"
/>
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 text-xs">🔍</span>
</div>
<div className="flex items-center gap-2 w-full sm:w-auto justify-end">
<button
type="button"
onClick={onOpenCreatePlan}
className="flex items-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 Custom Plan
</button>
</div>
</div>
{/* Filter Pills */}
<div className="flex items-center gap-1.5 flex-wrap border-t border-slate-200/60 pt-3">
<span className="text-[10px] uppercase font-bold text-slate-400 mr-1">Filter:</span>
{[
{ key: 'all', label: `All Plans (${plans.length})` },
{ key: 'bundle', label: `Bundles (${plans.filter((p) => p.plan_type === 'bundle').length})` },
{ key: 'one_time', label: `Single Modules (${plans.filter((p) => p.plan_type !== 'bundle').length})` },
{ key: 'active', label: `Active (${plans.filter((p) => p.is_active).length})` },
{ key: 'inactive', label: `Inactive (${plans.filter((p) => !p.is_active).length})` },
].map((f) => (
<button
key={f.key}
type="button"
onClick={() => {
onTypeFilterChange(f.key);
onPageChange(1);
}}
className={`px-3 py-1 rounded-lg text-xs font-bold transition-colors cursor-pointer ${
typeFilter === f.key
? 'bg-indigo-600 text-white shadow-sm'
: 'bg-white border border-slate-200 text-slate-600 hover:bg-slate-100 shadow-sm'
}`}
>
{f.label}
</button>
))}
</div>
</div>
{/* Plans Ledger Table */}
{filteredPlans.length === 0 ? (
<div className="bg-slate-50 border border-slate-200 rounded-2xl p-10 text-center space-y-2">
<Tag className="h-8 w-8 text-slate-300 mx-auto" />
<p className="text-sm font-bold text-slate-600">No subscription plans found</p>
<p className="text-xs text-slate-400">Try adjusting your search query or filter settings.</p>
</div>
) : (
<div className="border border-slate-200 rounded-2xl overflow-hidden shadow-sm bg-white p-4">
<div className="overflow-x-auto">
<table className="w-full text-left border-collapse min-w-[720px]">
<thead>
<tr className="bg-slate-50 border-b border-slate-200 text-[11px] uppercase font-bold text-slate-400 tracking-wider">
<th className="py-3.5 px-4">Plan Details</th>
<th className="py-3.5 px-3">Type & Duration</th>
<th className="py-3.5 px-3">Included Modules</th>
<th className="py-3.5 px-3">Live Pricing (INR / USD)</th>
<th className="py-3.5 px-3 text-center">Subscribers</th>
<th className="py-3.5 px-3 text-center">Status</th>
<th className="py-3.5 px-4 text-right">Actions</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100 text-xs">
{paginatedPlans.map((plan) => (
<tr
key={plan.base_code || plan.code || plan.id}
className={`hover:bg-slate-50/80 transition-colors ${
!plan.is_active ? 'opacity-65 bg-slate-50/30' : ''
}`}
>
{/* Plan Details */}
<td className="py-3.5 px-4">
<div className="space-y-0.5 max-w-xs">
<div className="flex items-center gap-1.5">
<span className="font-bold text-slate-800">{plan.name}</span>
</div>
<div className="flex items-center gap-2">
<code className="text-[10px] font-mono text-indigo-600 bg-indigo-50 px-1.5 py-0.5 rounded border border-indigo-100 font-bold">
{plan.base_code || plan.code}
</code>
</div>
{plan.description && (
<p className="text-[11px] text-slate-400 truncate max-w-xs">{plan.description}</p>
)}
</div>
</td>
{/* Type & Duration */}
<td className="py-3.5 px-3 whitespace-nowrap">
<div className="space-y-1">
<span
className={`inline-block px-2 py-0.5 rounded-md text-[10px] font-bold ${
plan.plan_type === 'bundle'
? 'bg-purple-100 text-purple-700 border border-purple-200'
: 'bg-blue-100 text-blue-700 border border-blue-200'
}`}
>
{plan.plan_type === 'bundle' ? 'Package Bundle' : 'Single Module'}
</span>
<p className="text-[10px] text-slate-500 capitalize">
{plan.billing_period === 'lifetime'
? 'Lifetime Access'
: `${plan.billing_period || 'yearly'} Billing`}
</p>
</div>
</td>
{/* Included Modules */}
<td className="py-3.5 px-3">
{plan.modules && plan.modules.length > 0 ? (
<div className="max-w-xs">
{plan.code === 'LIFETIME_ALL' || plan.base_code === 'LIFETIME_ALL' ? (
<div className="space-y-1">
<div className="flex items-center gap-2">
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-[10px] font-black bg-gradient-to-r from-amber-50 to-orange-50 text-amber-800 border border-amber-300 shadow-xs">
<Sparkles className="h-3 w-3 text-amber-500 shrink-0" />
All Modules ({plan.modules.length})
</span>
<button
type="button"
onClick={() => toggleExpandModules(plan.base_code || plan.code || plan.id)}
className="text-[10px] font-bold text-indigo-600 hover:text-indigo-800 underline cursor-pointer"
>
{expandedModules[plan.base_code || plan.code || plan.id] ? 'Hide' : 'View all'}
</button>
</div>
{expandedModules[plan.base_code || plan.code || plan.id] && (
<div className="flex flex-wrap gap-1 mt-1.5 p-2 bg-slate-50 border border-slate-200 rounded-lg max-h-36 overflow-y-auto">
{plan.modules.map((m: any, i: number) => (
<span
key={m.id || i}
className="text-[10px] font-bold px-1.5 py-0.5 rounded bg-white text-slate-700 border border-slate-200 whitespace-nowrap"
>
{m.name || m.slug}
</span>
))}
</div>
)}
</div>
) : (
<div>
<div className="flex flex-wrap gap-1 items-center">
{(expandedModules[plan.base_code || plan.code || plan.id]
? plan.modules
: plan.modules.slice(0, 3)
).map((m: any, i: number) => (
<span
key={m.id || i}
className="text-[10px] font-bold px-1.5 py-0.5 rounded bg-slate-100 text-slate-700 border border-slate-200 whitespace-nowrap"
>
{m.name || m.slug}
</span>
))}
{plan.modules.length > 3 && (
<button
type="button"
onClick={() => toggleExpandModules(plan.base_code || plan.code || plan.id)}
className="text-[10px] font-bold px-1.5 py-0.5 rounded bg-indigo-50 text-indigo-700 border border-indigo-200 hover:bg-indigo-100 transition-colors cursor-pointer whitespace-nowrap"
>
{expandedModules[plan.base_code || plan.code || plan.id]
? 'Show less'
: `+${plan.modules.length - 3} more`}
</button>
)}
</div>
</div>
)}
</div>
) : (
<span className="text-[10px] text-slate-400 italic">No modules attached</span>
)}
</td>
{/* Pricing */}
<td className="py-3.5 px-3 whitespace-nowrap">
<div className="space-y-0.5 font-bold">
<p className="text-xs text-slate-800 font-mono">₹{plan.price_inr ?? '—'}</p>
<p className="text-[10px] text-slate-400 font-mono">${plan.price_usd ?? '—'}</p>
</div>
</td>
{/* Subscribers */}
<td className="py-3.5 px-3 text-center whitespace-nowrap">
<span
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-bold ${
(plan.subscribers_count || 0) > 0
? 'bg-indigo-50 text-indigo-700 border border-indigo-200'
: 'bg-slate-50 text-slate-400 border border-slate-200'
}`}
>
<Users className="h-3 w-3" />
{plan.subscribers_count || 0}
</span>
</td>
{/* Live Status Toggle */}
<td className="py-3.5 px-3 text-center whitespace-nowrap">
<button
type="button"
onClick={() => onTogglePlanStatus(plan)}
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 ${
plan.is_active ? 'bg-indigo-600' : 'bg-slate-300'
}`}
title={plan.is_active ? 'Click to deactivate' : 'Click to activate'}
>
<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 ${
plan.is_active ? 'translate-x-4' : 'translate-x-0'
}`}
/>
</button>
<span className="block text-[9px] font-bold text-slate-400 mt-0.5">
{plan.is_active ? 'Active' : 'Inactive'}
</span>
</td>
{/* Actions */}
<td className="py-3.5 px-4 text-right whitespace-nowrap">
<button
type="button"
onClick={() => onOpenEditPrice(plan)}
className="inline-flex items-center gap-1 px-3 py-1.5 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 Info & Price
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
<Pagination
currentPage={safePage}
totalItems={totalFiltered}
itemsPerPage={perPage}
onPageChange={onPageChange}
onItemsPerPageChange={onPerPageChange}
/>
</div>
)}
</div>
);
};