import React, { useState } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { ArrowRightIcon } from '@heroicons/react/24/outline';
import { toast } from 'sonner';
import { api } from '../lib/api';
import type { ApiResponse } from '../lib/api';
import type { Appointment, AppointmentStatus } from '../types';
import { formatDate, formatDateTime } from '../lib/utils';
import PageHeader from '../components/ui/PageHeader';
import StatusBadge from '../components/ui/StatusBadge';
import ConfirmDialog from '../components/ui/ConfirmDialog';
import SearchableSelect from '../components/ui/SearchableSelect';
const ALL_STATUSES: { value: AppointmentStatus; label: string }[] = [
{ value: 'pending', label: 'رزرو شده' },
{ value: 'confirmed', label: 'تأیید شده' },
{ value: 'completed', label: 'تکمیل شده' },
{ value: 'cancelled_by_doctor', label: 'لغو پزشک' },
{ value: 'cancelled_by_user', label: 'لغو بیمار' },
{ value: 'no_show', label: 'غیبت' },
{ value: 'expired', label: 'منقضی' },
];
function InfoRow({ label, value }: { label: string; value: React.ReactNode }) {
return (
{label}
{value ?? '—'}
);
}
export default function AppointmentDetailPage() {
const { uuid } = useParams<{ uuid: string }>();
const navigate = useNavigate();
const qc = useQueryClient();
const [cancelOpen, setCancelOpen] = useState(false);
const [newStatus, setNewStatus] = useState('');
const { data, isLoading } = useQuery({
queryKey: ['appointment', uuid],
queryFn: () => api.get>(`/api/v1/appointment/${uuid}`),
enabled: !!uuid,
});
const statusMutation = useMutation({
mutationFn: (status: string) =>
api.patch>(`/api/v1/appointment/${uuid}/status`, { status }),
onSuccess: () => {
toast.success('وضعیت نوبت بروزرسانی شد');
qc.invalidateQueries({ queryKey: ['appointment', uuid] });
},
onError: (err: Error) => toast.error(err.message),
});
const cancelMutation = useMutation({
mutationFn: () => api.patch>(`/api/v1/appointment/${uuid}/cancel`, {}),
onSuccess: () => {
toast.success('نوبت لغو شد');
setCancelOpen(false);
qc.invalidateQueries({ queryKey: ['appointment', uuid] });
},
onError: (err: Error) => toast.error(err.message),
});
const appt = data?.data;
return (
navigate('/admin/appointments')}
className="flex items-center gap-2 text-sm text-slate-500 dark:text-slate-400 hover:text-slate-800 dark:hover:text-slate-100 transition-colors">
بازگشت
}
/>
{isLoading ? (
{Array.from({ length: 6 }).map((_, i) => (
))}
) : appt ? (
اطلاعات بیمار
{appt.patient_mobile}} />
وضعیت و اقدامات
({ value: s.value, label: s.label }))}
value={newStatus || null}
onChange={(v) => setNewStatus(v ? String(v) : '')}
placeholder="انتخاب وضعیت..."
isClearable
/>
) : (
نوبتی یافت نشد
)}
cancelMutation.mutate()}
onCancel={() => setCancelOpen(false)}
/>
);
}