import {type PatientData, SORT_SYMBOLS, type WardInfo, WardTypes} from "~/utils/models.ts"; import {ActionIcon, Group, Table, Text} from "@mantine/core"; import {useEffect, useMemo, useState} from "react"; import {apiGetWardPatients, apiTransferWard, apiGetWardList} from "~/utils/hms_api.ts"; import {getUserDisplayFullName, showErrorMessage, showInfoMessage} from "~/utils/utils.ts"; import {confirmCheckWardPatients, confirmDeleteWard, confirmEditOrCreateWard} from "~/components/subs/confirms.tsx"; import EyeIcon from "mdi-react/EyeIcon"; import {iconMStyle} from "~/styles.ts"; import PencilIcon from "mdi-react/PencilIcon"; import DeleteIcon from "mdi-react/DeleteIcon"; import SwapHorizontalIcon from "mdi-react/SwapHorizontalIcon"; import {modals} from "@mantine/modals"; import {Button, Select, Stack} from "@mantine/core"; import {useForm} from "@mantine/form"; import { ResponsiveTableContainer } from "./ResponsiveTableContainer"; function ConfirmTransferWard({patientId, currentWardId, onSuccess}: {patientId: number, currentWardId: number, onSuccess: () => void}) { const [loading, setLoading] = useState(false); const [wards, setWards] = useState([]); const [loadingWards, setLoadingWards] = useState(true); const form = useForm({ initialValues: { patient_id: patientId, target_ward_id: '', }, validate: { target_ward_id: (value) => (!value ? 'Please select a ward' : null), }, }); useEffect(() => { // Load wards for selection apiGetWardList(1, true).then(res => { if (res.success) { setWards(res.data.wards); } else { showErrorMessage(res.message, "Failed to load wards"); } }) .catch(err => { showErrorMessage("Failed to load wards", "Error"); }) .finally(() => { setLoadingWards(false); }); }, []); const wardOptions = useMemo(() => { return wards .filter(ward => ward.id !== currentWardId && ward.current_occupancy < ward.total_capacity) // Exclude current ward and show only those with available capacity .map(ward => ({ value: ward.id.toString(), label: `${ward.name} (${ward.current_occupancy}/${ward.total_capacity}) - ${WardTypes[ward.type]}` })); }, [wards, currentWardId]); const handleFormSubmit = (values: typeof form.values) => { setLoading(true); apiTransferWard( values.patient_id, parseInt(values.target_ward_id) ).then(res => { if (res.success) { showInfoMessage("Patient transferred successfully", "Success"); modals.closeAll(); onSuccess(); } else { showErrorMessage(res.message, "Failed to transfer patient"); } }) .catch(err => { showErrorMessage("Failed to transfer patient", "Error"); }) .finally(() => { setLoading(false); }); }; return (