import React, { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useNavigate } from 'react-router-dom'; import { EyeIcon, TrashIcon, PencilIcon, MagnifyingGlassIcon } from '@heroicons/react/24/outline'; import { toast } from 'sonner'; import { api } from '../lib/api'; import type { ApiResponse, PaginatedResponse } from '../lib/api'; import type { Clinic } from '../types'; import { formatDate, formatNumber } from '../lib/utils'; import DataTable, { Column } from '../components/ui/DataTable'; import { ActiveBadge } from '../components/ui/StatusBadge'; import Pagination from '../components/ui/Pagination'; import ConfirmDialog from '../components/ui/ConfirmDialog'; const HUES_LIST = [256, 205, 162, 295, 272]; export default function ClinicsPage() { const navigate = useNavigate(); const qc = useQueryClient(); const [page, setPage] = useState(1); const [search, setSearch] = useState(''); const [deleteTarget, setDeleteTarget] = useState(null); const limit = 15; const { data, isLoading } = useQuery({ queryKey: ['clinics', page, search], queryFn: () => api.get>( `/api/v1/clinics?page=${page}&limit=${limit}${search ? `&search=${encodeURIComponent(search)}` : ''}`, ), }); const deleteMutation = useMutation({ mutationFn: (c: Clinic) => api.delete>(`/api/v1/clinic/${c.uuid}`), onSuccess: () => { toast.success('کلینیک حذف شد'); setDeleteTarget(null); qc.invalidateQueries({ queryKey: ['clinics'] }); }, onError: (err: Error) => toast.error(err.message), }); const columns: Column[] = [ { key: 'name', header: 'نام کلینیک', render: (c) => { const hue = HUES_LIST[(c.uuid?.charCodeAt(0) ?? 0) % HUES_LIST.length]; return (
{c.logo ? ( ) : (
{c.name?.[0] ?? '?'}
)}
{c.name}
); }, }, { key: 'phone', header: 'تلفن', render: (c) => c.phone ? {c.phone} : '—' }, { key: 'doctors_count', header: 'پزشکان', render: (c) => ( {formatNumber(c.doctors_count ?? 0)} پزشک ), }, { key: 'is_active', header: 'وضعیت', render: (c) => }, { key: 'created_at', header: 'تاریخ ثبت', render: (c) => formatDate(c.created_at) }, ]; const items = data?.data ?? []; const total = data?.meta?.totalRecords ?? 0; return (

کلینیک‌ها

{total} کلینیک ثبت‌شده
{ setSearch(e.target.value); setPage(1); }} placeholder="جستجو بر اساس نام کلینیک..." />
columns={columns} data={items} loading={isLoading} emptyMessage="هیچ کلینیکی یافت نشد" actions={(clinic) => ( <> )} />
deleteTarget && deleteMutation.mutate(deleteTarget)} onCancel={() => setDeleteTarget(null)} />
); }