import { useEffect, useMemo, useRef, useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useParams, useSearchParams, Link } from 'react-router-dom';
import {
ChevronRightIcon, PencilIcon, ClipboardDocumentCheckIcon, DocumentTextIcon,
CalendarDaysIcon, CreditCardIcon, BanknotesIcon, ChatBubbleLeftRightIcon,
PhoneArrowUpRightIcon, PaperClipIcon, ClipboardDocumentListIcon,
ArrowUpTrayIcon, TrashIcon, DocumentIcon,
} from '@heroicons/react/24/outline';
import { PlusIcon } from '@heroicons/react/24/outline';
import { toast } from 'sonner';
import { api } from '../lib/api';
import type { ApiResponse } from '../lib/api';
import type { PatientRecord } from '../types';
import { useAuthStore } from '../stores/authStore';
import { formatDate, formatRial, formatTime, formatNumber } from '../lib/utils';
import DataTable, { type Column } from '../components/ui/DataTable';
import type { WalletTxn } from '../hooks/usePatientWallet';
import type { WalletModalSubmit } from '../components/WalletTransactionModal';
import Modal from '../components/ui/Modal';
import ConfirmDialog from '../components/ui/ConfirmDialog';
import PersianDateInput from '../components/ui/PersianDateInput';
import PatientCaseBanner, { Breadcrumb } from '../components/PatientCaseBanner';
import SessionServiceCard, { type SessionCardData } from '../components/SessionServiceCard';
import AppointmentTurnCard, { type AppointmentCardData } from '../components/AppointmentTurnCard';
import SessionPaymentAccordion, { type SessionPaymentData } from '../components/SessionPaymentAccordion';
import InvoiceSummaryModal from '../components/InvoiceSummaryModal';
import SearchableSelect from '../components/ui/SearchableSelect';
import { TurnsFilter, AddTurn } from '../components/icons/FilesToolbarIcons';
import {
TabServices, TabInfo, TabCalendar, TabCard, TabWallet, TabSMS, TabCall, TabAttach, TabBody,
} from '../components/icons/FilesServiceIcons';
import PatientRecordInfoForm from '../components/PatientRecordInfoForm';
import WalletTransactionModal from '../components/WalletTransactionModal';
import { usePatientWallet } from '../hooks/usePatientWallet';
import {
profileToFormValues, formValuesToPayload,
GENDER_OPTS, MARITAL_OPTS, EDUCATION_OPTS, REFERRAL_OPTS,
} from '../lib/patientForm';
type TabKey = 'services' | 'info' | 'appointments' | 'payments' | 'wallet' | 'messages' | 'callcenter' | 'attach' | 'records';
const TABS: { key: TabKey; label: string; icon: (c: string) => React.ReactNode }[] = [
{ key: 'services', label: 'سرویسها', icon: (c) => },
{ key: 'info', label: 'اطلاعات پرونده', icon: (c) => },
{ key: 'appointments', label: 'نوبتها', icon: (c) => },
{ key: 'payments', label: 'پرداختها', icon: (c) => },
{ key: 'wallet', label: 'کیف پول', icon: (c) => },
{ key: 'messages', label: 'پیامها', icon: (c) => },
{ key: 'callcenter', label: 'کال سنتر', icon: (c) => },
{ key: 'attach', label: 'ضمیمه', icon: (c) => },
{ key: 'records', label: 'پرونده پزشکی', icon: (c) => },
];
function Placeholder({ label }: { label: string }) {
return (
محتوای «{label}» بهزودی تکمیل میشود.
);
}
/** پرونده — the tabbed patient case-file (Figma "جزئیات پرونده"). */
export default function PatientDetailPage() {
const { uuid } = useParams<{ uuid: string }>();
// ?tab=wallet etc. lets other pages (e.g. appointment forms) deep-link a tab
const [searchParams] = useSearchParams();
const requested = searchParams.get('tab') as TabKey | null;
const [tab, setTab] = useState(
requested && TABS.some((t) => t.key === requested) ? requested : 'services',
);
const { data, isLoading } = useQuery>({
queryKey: ['patient', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}`),
enabled: !!uuid,
});
const record = data?.data;
const qc = useQueryClient();
const [invoiceUuid, setInvoiceUuid] = useState(null);
const [settleTarget, setSettleTarget] = useState(null);
// ── فرم «اطلاعات پرونده» (اینلاین، معادل tauri FileInfoSection) ──────────────
// استان/شهر (Location) و بیمهٔ پایه (insurance-pricing) برای گزینههای فرم.
const [editProvinceId, setEditProvinceId] = useState(null);
const seededProvince = useRef(false);
useEffect(() => {
if (!seededProvince.current && record?.profile) {
setEditProvinceId(record.profile.province_id ?? null);
seededProvince.current = true;
}
}, [record]);
const provincesQ = useQuery({
queryKey: ['provinces'],
queryFn: () => api.get('/api/v1/provinces'),
staleTime: 600_000,
});
const citiesQ = useQuery({
queryKey: ['cities', editProvinceId],
queryFn: () => api.get(`/api/v1/cities${editProvinceId ? `?province_id=${editProvinceId}` : ''}`),
staleTime: 300_000,
});
const pricingQ = useQuery>({
queryKey: ['insurance-pricing'],
queryFn: () => api.get('/api/v1/insurance-pricing'),
enabled: !!uuid,
});
const locOpts = (raw: any) =>
(raw?.data?.data ?? raw?.data ?? []).map((x: any) => ({ value: Number(x.id), label: x.name }));
const baseInsuranceOptions = ((pricingQ.data?.data as any)?.insurances ?? [])
.filter((i: any) => i.type === 'basic')
.map((i: any) => ({ value: String(i.insurance_id), label: i.insurance_name }));
const suppInsuranceOptions = ((pricingQ.data?.data as any)?.insurances ?? [])
.filter((i: any) => i.type === 'supplementary')
.map((i: any) => ({ value: String(i.insurance_id), label: i.insurance_name }));
const updateProfileMut = useMutation({
mutationFn: (body: object) => api.patch(`/api/v1/patient/${uuid}`, body),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['patient', uuid] });
toast.success('اطلاعات بیمار بهروزرسانی شد');
},
onError: (e: any) => toast.error(e?.message || 'خطا در بهروزرسانی اطلاعات بیمار'),
});
// sessions + appointments load eagerly so the banner (debt / next visit) is ready.
const sessionsQ = useQuery>({
queryKey: ['patient-sessions', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}/sessions`),
enabled: !!uuid,
});
const appointmentsQ = useQuery>({
queryKey: ['patient-appointments', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}/appointments`),
enabled: !!uuid,
});
const sessions = sessionsQ.data?.data ?? [];
const hasDebt = sessions.some((s) => !s.is_paid);
const nowSec = Math.floor(Date.now() / 1000);
const nextAppointment = (appointmentsQ.data?.data ?? [])
.filter((a: any) => (a.starts_at ?? 0) >= nowSec && !String(a.status).startsWith('cancelled'))
.sort((a: any, b: any) => a.starts_at - b.starts_at)[0]?.starts_at ?? null;
const settle = useMutation({
mutationFn: ({ sessionUuid, method }: { sessionUuid: string; method: string }) =>
api.patch(`/api/v1/session/${sessionUuid}`, { payment_method: method }),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['patient-sessions', uuid] });
// پرداخت از کیف پول موجودی را کم میکند → دفتر کیف پول را هم تازه کن.
qc.invalidateQueries({ queryKey: ['patient-wallet', uuid] });
toast.success('پرداخت ثبت شد');
setSettleTarget(null);
},
onError: (e: any) => toast.error(e?.message || 'خطا در ثبت پرداخت'),
});
return (
{/* breadcrumb + patient banner (tauri BreadcrumbHeader + FileServicesHeader) */}
setTab('messages')}
/>
{/* tab bar */}
{TABS.map((t) => {
const on = t.key === tab;
return (
);
})}
{/* tab content */}
{isLoading ? (
در حال بارگذاری...
) : tab === 'info' ? (
updateProfileMut.mutate(formValuesToPayload(v))}
isSubmitting={updateProfileMut.isPending}
onProvinceChange={setEditProvinceId}
/>
) : tab === 'services' ? (
{/* filter + new-service (tauri ServiceCardsSection header) */}
{sessionsQ.isLoading ? (
در حال بارگذاری...
) : sessions.length === 0 ? (
سرویسی ثبت نشده است
) : (
{sessions.map((s) => (
setSettleTarget(u)} onViewInvoice={(iv) => setInvoiceUuid(iv)} />
))}
)}
) : tab === 'appointments' ? (
) : tab === 'payments' ? (
) : tab === 'wallet' ? (
) : tab === 'callcenter' ? (
) : tab === 'attach' ? (
) : tab === 'records' ? (
) : tab === 'messages' ? (
) : (
t.key === tab)!.label} />
)}
setInvoiceUuid(null)} />
{/* انتخاب روش پرداختِ مراجعه (نقدی / کارت / کیف پول) */}
setSettleTarget(null)}>
روش تسویهٔ این مراجعه را انتخاب کنید:
{[
{ method: 'cash', label: 'نقدی' },
{ method: 'card', label: 'کارت به کارت' },
{ method: 'wallet', label: 'کیف پول بیمار' },
].map((m) => (
))}
);
}
interface Attachment { uuid: string; name: string; url: string; mime?: string | null; size?: number | null }
const formatBytes = (n?: number | null) => {
if (!n) return '';
if (n < 1024) return `${n} B`;
if (n < 1024 * 1024) return `${Math.round(n / 1024)} KB`;
return `${(n / 1024 / 1024).toFixed(1)} MB`;
};
/** ضمیمه — patient attachments: upload (raw body), list, delete. */
function AttachmentsTab({ uuid }: { uuid: string }) {
const qc = useQueryClient();
const fileRef = useRef(null);
const [uploading, setUploading] = useState(false);
const { data, isLoading } = useQuery>({
queryKey: ['patient-attachments', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}/attachments`),
});
const items = data?.data ?? [];
const del = useMutation({
mutationFn: (attUuid: string) => api.delete(`/api/v1/patient/attachment/${attUuid}`),
onSuccess: () => { qc.invalidateQueries({ queryKey: ['patient-attachments', uuid] }); toast.success('ضمیمه حذف شد'); },
onError: (e: any) => toast.error(e.message),
});
const onFile = async (file: File) => {
setUploading(true);
try {
const token = useAuthStore.getState().token;
const res = await fetch(`/api/v1/patient/${uuid}/attachment?name=${encodeURIComponent(file.name)}`, {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="${file.name}"`,
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
body: file,
});
if (!res.ok) throw new Error('خطا در آپلود فایل');
qc.invalidateQueries({ queryKey: ['patient-attachments', uuid] });
toast.success('فایل آپلود شد');
} catch (e: any) {
toast.error(e.message);
} finally {
setUploading(false);
if (fileRef.current) fileRef.current.value = '';
}
};
return (
{isLoading ? (
در حال بارگذاری...
) : items.length === 0 ? (
هنوز فایلی ضمیمه نشده است.
) : (
{items.map((a) => (
{a.name}
{a.size ?
{formatBytes(a.size)}
: null}
))}
)}
);
}
interface MedicalItem { uuid: string; title: string; body?: string | null; recorded_at: number }
/** پرونده پزشکی — medical exam entries: list + add/edit modal + delete. */
function MedicalRecordsTab({ uuid }: { uuid: string }) {
const qc = useQueryClient();
const [modal, setModal] = useState<'create' | MedicalItem | null>(null);
const [delTarget, setDelTarget] = useState(null);
const [title, setTitle] = useState('');
const [date, setDate] = useState('');
const [body, setBody] = useState('');
const { data, isLoading } = useQuery>({
queryKey: ['patient-medical', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}/medical-records`),
});
const items = data?.data ?? [];
const invalidate = () => qc.invalidateQueries({ queryKey: ['patient-medical', uuid] });
const open = (m?: MedicalItem) => {
setTitle(m?.title ?? ''); setBody(m?.body ?? '');
setDate(m?.recorded_at ? new Date(m.recorded_at * 1000).toISOString().slice(0, 10) : '');
setModal(m ?? 'create');
};
const save = useMutation({
mutationFn: () => {
const payload = { title, body: body || null, recorded_at: date ? Math.floor(new Date(date).getTime() / 1000) : undefined };
return modal === 'create'
? api.post(`/api/v1/patient/${uuid}/medical-record`, payload)
: api.patch(`/api/v1/patient/medical-record/${(modal as MedicalItem).uuid}`, payload);
},
onSuccess: () => { invalidate(); setModal(null); toast.success('ذخیره شد'); },
onError: (e: any) => toast.error(e.message),
});
const del = useMutation({
mutationFn: (u: string) => api.delete(`/api/v1/patient/medical-record/${u}`),
onSuccess: () => { invalidate(); setDelTarget(null); toast.success('حذف شد'); },
onError: (e: any) => { toast.error(e.message); setDelTarget(null); },
});
return (
{isLoading ? (
در حال بارگذاری...
) : items.length === 0 ? (
معاینهای ثبت نشده است.
) : (
{items.map((m) => (
{m.title}
{formatDate(m.recorded_at)}
{m.body &&
{m.body}
}
))}
)}
setModal(null)} title={modal === 'create' ? 'ثبت معاینه جدید' : 'ویرایش معاینه'}>
delTarget && del.mutate(delTarget.uuid)}
onCancel={() => setDelTarget(null)}
loading={del.isPending}
/>
);
}
interface Message { uuid: string; body: string; channel: string; created_at: number }
const CHANNEL_LABEL: Record = { sms: 'پیامک', note: 'یادداشت', call: 'تماس', email: 'ایمیل' };
/** پیامها — patient message/communication log: send + list + delete. */
function MessagesTab({ uuid }: { uuid: string }) {
const qc = useQueryClient();
const [body, setBody] = useState('');
const { data, isLoading } = useQuery>({
queryKey: ['patient-messages', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}/messages`),
});
const items = data?.data ?? [];
const invalidate = () => qc.invalidateQueries({ queryKey: ['patient-messages', uuid] });
const send = useMutation({
mutationFn: () => api.post(`/api/v1/patient/${uuid}/message`, { body, channel: 'sms' }),
onSuccess: () => { invalidate(); setBody(''); toast.success('پیام ثبت شد'); },
onError: (e: any) => toast.error(e.message),
});
const del = useMutation({
mutationFn: (u: string) => api.delete(`/api/v1/patient/message/${u}`),
onSuccess: () => { invalidate(); toast.success('پیام حذف شد'); },
onError: (e: any) => toast.error(e.message),
});
return (
{isLoading ? (
در حال بارگذاری...
) : items.length === 0 ? (
پیامی ثبت نشده است.
) : (
{items.map((m) => (
{m.body}
{CHANNEL_LABEL[m.channel] ?? m.channel}
{formatDate(m.created_at)}
))}
)}
);
}
interface Call { uuid: string; subject: string; summary?: string | null; outcome: string; called_at: number; personnel?: string | null }
/** کال سنتر — patient call log: register a call + filterable history (all / success / missed). */
function CallCenterTab({ uuid }: { uuid: string }) {
const qc = useQueryClient();
const userName = useAuthStore((s) => s.userName);
const [filter, setFilter] = useState<'all' | 'success' | 'missed'>('all');
const [date, setDate] = useState('');
const [time, setTime] = useState('');
const [subject, setSubject] = useState('');
const [summary, setSummary] = useState('');
const [outcome, setOutcome] = useState<'success' | 'missed'>('success');
const { data, isLoading } = useQuery>({
queryKey: ['patient-calls', uuid],
queryFn: () => api.get(`/api/v1/patient/${uuid}/calls`),
});
const calls = data?.data ?? [];
const shown = filter === 'all' ? calls : calls.filter((c) => c.outcome === filter);
const successCount = calls.filter((c) => c.outcome === 'success').length;
const missedCount = calls.filter((c) => c.outcome === 'missed').length;
const invalidate = () => qc.invalidateQueries({ queryKey: ['patient-calls', uuid] });
const create = useMutation({
mutationFn: () => {
const iso = date ? `${date}T${time || '00:00'}` : null;
const calledAt = iso ? Math.floor(new Date(iso).getTime() / 1000) : Math.floor(Date.now() / 1000);
return api.post(`/api/v1/patient/${uuid}/call`, { subject, summary, outcome, called_at: calledAt, personnel: userName });
},
onSuccess: () => { invalidate(); setDate(''); setTime(''); setSubject(''); setSummary(''); setOutcome('success'); toast.success('تماس ثبت شد'); },
onError: (e: any) => toast.error(e.message),
});
const del = useMutation({
mutationFn: (u: string) => api.delete(`/api/v1/patient/call/${u}`),
onSuccess: () => { invalidate(); toast.success('تماس حذف شد'); },
onError: (e: any) => toast.error(e.message),
});
const chip = (key: 'all' | 'success' | 'missed', label: string) => (
);
return (
{/* register form */}
ثبت تماس جدید
setTime(e.target.value)} dir="ltr" />
setSubject(e.target.value)} placeholder="موضوع تماس" />
{/* history */}
{chip('all', 'همه')}
{chip('success', `تماسهای موفق (${successCount})`)}
{chip('missed', `بیپاسخ (${missedCount})`)}
تاریخچه تماسها
{isLoading ? (
در حال بارگذاری...
) : shown.length === 0 ? (
تماسی ثبت نشده است.
) : (
{shown.map((c) => {
const ok = c.outcome === 'success';
return (
{c.summary &&
{c.summary}
}
{formatDate(c.called_at)}
{c.personnel &&
{c.personnel}
}
);
})}
)}
);
}
type WalletFilter = 'all' | 'credit' | 'debit';
const WALLET_FILTERS: { key: WalletFilter; label: string }[] = [
{ key: 'all', label: 'همه' },
{ key: 'credit', label: 'واریزی' },
{ key: 'debit', label: 'برداشت' },
];
const METHOD_LABEL: Record = {
card: 'کارت به کارت', pos: 'کارتخوان', cash: 'نقدی', wallet: 'کیف پول', gateway: 'درگاه اینترنتی',
};
const STATUS_LABEL: Record = { confirmed: 'تأیید شده' };
type WalletRow = WalletTxn & { row_no: number };
/**
* کیف پول — کارت موجودی + مودال شارژ/برداشت (روش پرداخت واقعی) + فیلتر
* همه/واریزی/برداشت روی دفترِ کاملِ تراکنشها (DataTable با ستون ثبتکننده،
* روش پرداخت، دلیل و وضعیت). طراحی مطابق پنل.
*/
function WalletTab({ uuid }: { uuid: string }) {
const { balanceRials, transactions, isLoading, charge, withdraw } = usePatientWallet(uuid);
const [modalOpen, setModalOpen] = useState(false);
const [filter, setFilter] = useState('all');
const submitting = charge.isPending || withdraw.isPending;
const handleSubmit = ({ mode, ...body }: WalletModalSubmit) => {
const mut = mode === 'charge' ? charge : withdraw;
mut.mutate(body, {
onSuccess: () => {
toast.success(mode === 'charge' ? 'کیف پول شارژ شد' : 'برداشت از کیف پول انجام شد');
setModalOpen(false);
},
onError: (e: any) => toast.error(e?.message || 'خطا در ثبت تراکنش'),
});
};
const shown = filter === 'all' ? transactions : transactions.filter((t) => t.type === filter);
const rows: WalletRow[] = shown.map((t, i) => ({ ...t, row_no: i + 1 }));
const columns: Column[] = [
{ key: 'row_no', header: 'ردیف', className: 'w-[60px]', render: (r) => formatNumber(r.row_no) },
{
key: 'amount', header: 'مبلغ', render: (r) => (
{r.type === 'credit' ? '+' : '−'}{formatRial(r.amount_rials)}
),
},
{
key: 'type', header: 'نوع تراکنش', render: (r) => (
{r.type === 'credit' ? 'واریز' : 'برداشت'}
),
},
{ key: 'method', header: 'روش پرداخت', render: (r) => (r.payment_method ? METHOD_LABEL[r.payment_method] ?? r.payment_method : '—') },
{ key: 'reason', header: 'دلیل', render: (r) => r.description || '—' },
{ key: 'by', header: 'ثبتکننده', render: (r) => r.created_by_name || '—' },
{ key: 'date', header: 'تاریخ', render: (r) => formatDate(r.created_at) },
{ key: 'time', header: 'ساعت', render: (r) => formatTime(r.created_at) },
{ key: 'status', header: 'وضعیت', render: (r) => {STATUS_LABEL[r.status ?? ''] ?? 'تأیید شده'} },
];
return (
{/* موجودی + دکمه تراکنش جدید */}
موجودی کیف پول
{formatRial(balanceRials)}
{/* فیلتر تراکنشها */}
تراکنشها:
{WALLET_FILTERS.map((f) => {
const on = filter === f.key;
return (
);
})}
setModalOpen(false)}
onSubmit={handleSubmit}
/>
columns={columns}
data={rows}
loading={isLoading}
emptyMessage="تراکنشی ثبت نشده است"
/>
);
}
/**
* پرداختها — settlement history grouped by مراجعه (session), ported from tauri
* PaymentsSection. Reuses the already-fetched sessions; each session is an
* accordion showing its settlement line or the empty message.
*/
function PaymentsTab({ q }: { q: { data?: ApiResponse; isLoading: boolean } }) {
const items = (q.data?.data ?? []) as SessionPaymentData[];
// undefined = default (first panel open, matching tauri); null = user closed all.
const [expanded, setExpanded] = useState(undefined);
const openUuid = expanded === undefined ? (items[0]?.uuid ?? null) : expanded;
if (q.isLoading) return در حال بارگذاری...
;
if (items.length === 0) return پرداختی ثبت نشده است
;
return (
{items.map((s) => (
setExpanded(openUuid === s.uuid ? null : s.uuid)}
/>
))}
);
}
const APPT_SORT_OPTS = [
{ value: 'newest', label: 'جدیدترین' },
{ value: 'oldest', label: 'قدیمیترین' },
{ value: 'reserved', label: 'رزرو شده' },
{ value: 'done', label: 'انجام شده' },
{ value: 'cancelled', label: 'لغو شده' },
];
const CANCELLED_STATUSES = ['cancelled_by_doctor', 'cancelled_by_user', 'no_show', 'expired'];
/**
* نوبتها — the appointments tab, ported from tauri TurnsSection: a sort/filter
* toolbar + reserve/new buttons, over a grid of AppointmentTurnCard. Sorting and
* filtering are client-side over the already-fetched list (tauri leaves them inert).
*/
function AppointmentsTab({ uuid, q }: {
uuid: string;
q: { data?: ApiResponse; isLoading: boolean };
}) {
const [sort, setSort] = useState('newest');
const items = q.data?.data ?? [];
const queryKey = ['patient-appointments', uuid];
const shown = useMemo(() => {
let list = [...items];
if (sort === 'reserved') list = list.filter((a) => a.status !== 'completed' && !CANCELLED_STATUSES.includes(a.status));
else if (sort === 'done') list = list.filter((a) => a.status === 'completed');
else if (sort === 'cancelled') list = list.filter((a) => CANCELLED_STATUSES.includes(a.status));
list.sort((a, b) => (sort === 'oldest' ? a.starts_at - b.starts_at : b.starts_at - a.starts_at));
return list;
}, [items, sort]);
return (
{/* toolbar: sort + filter (right of RTL) · reserve/new buttons (left) */}
setSort(String(v ?? 'newest'))} height={48} />
{q.isLoading ? (
در حال بارگذاری...
) : shown.length === 0 ? (
نوبتی ثبت نشده است
) : (
)}
);
}