'use client'; import { useState } from 'react'; import { Header } from '@/components/layouts/header'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { useTenants, useCreateTenant } from '@/lib/hooks/use-tenants'; import { useTenantViewStore } from '@/stores/tenant-view-store'; import { useAuthStore } from '@/stores/auth-store'; import { Building, Plus, Users, Eye, Calendar } from 'lucide-react'; export default function ClientesPage() { const { user } = useAuthStore(); const { data: tenants, isLoading } = useTenants(); const createTenant = useCreateTenant(); const { setViewingTenant } = useTenantViewStore(); const [showForm, setShowForm] = useState(false); const [formData, setFormData] = useState({ nombre: '', rfc: '', plan: 'starter' as const, }); // Only admins can access this page if (user?.role !== 'admin') { return ( <>

No tienes permisos para ver esta página.

); } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { await createTenant.mutateAsync(formData); setFormData({ nombre: '', rfc: '', plan: 'starter' }); setShowForm(false); } catch (error) { console.error('Error creating tenant:', error); } }; const handleViewClient = (tenantId: string, tenantName: string) => { setViewingTenant(tenantId, tenantName); window.location.href = '/dashboard'; }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('es-MX', { year: 'numeric', month: 'short', day: 'numeric', }); }; const planLabels: Record = { starter: 'Starter', business: 'Business', professional: 'Professional', enterprise: 'Enterprise', }; const planColors: Record = { starter: 'bg-muted text-muted-foreground', business: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-100', professional: 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-100', enterprise: 'bg-amber-100 text-amber-800 dark:bg-amber-900 dark:text-amber-100', }; return ( <>
{/* Stats */}

{tenants?.length || 0}

Total Clientes

{tenants?.reduce((acc, t) => acc + (t._count?.users || 0), 0) || 0}

Total Usuarios

{/* Add Client Form */} {showForm && ( Nuevo Cliente Registra un nuevo cliente para gestionar su facturación
setFormData({ ...formData, nombre: e.target.value })} placeholder="Empresa SA de CV" required />
setFormData({ ...formData, rfc: e.target.value.toUpperCase() })} placeholder="XAXX010101000" maxLength={13} required />
)} {/* Clients List */} Lista de Clientes {isLoading ? (
Cargando...
) : tenants && tenants.length > 0 ? (
{tenants.map((tenant) => (
{tenant.nombre.substring(0, 2).toUpperCase()}

{tenant.nombre}

{tenant.rfc} {planLabels[tenant.plan]}
{tenant._count?.users || 0} usuarios
{formatDate(tenant.createdAt)}
))}
) : (
No hay clientes registrados
)}
); }