import {useEffect, useState} from "react"; import { Modal, Input, Button, notification, Typography, Collapse, Steps, Row, Alert, Col, DatePicker, Spin, Grid } from "antd"; import PropTypes from "prop-types"; import getAllPatients from "../../api/patients/GetAllPatients.jsx"; import {useAuth} from "../../AuthContext.jsx"; import dayjs from "dayjs"; import getNotIssuedLenses from "../../api/lenses/GetNotIssuedLenses.jsx"; const {useBreakpoint} = Grid; const LensIssueFormModal = ({visible, onCancel, onSubmit}) => { const {user} = useAuth(); const screens = useBreakpoint(); const [patients, setPatients] = useState([]); const [lenses, setLenses] = useState([]); const [searchPatientString, setSearchPatientString] = useState(""); const [searchLensString, setSearchLensString] = useState(""); const [issueDate, setIssueDate] = useState(dayjs(new Date())); const [selectedPatient, setSelectedPatient] = useState(null); const [selectedLens, setSelectedLens] = useState(null); const [loading, setLoading] = useState(false); const [currentStep, setCurrentStep] = useState(0); useEffect(() => { if (visible) { fetchPatients(); fetchLenses(); } }, [visible]); const fetchPatients = async () => { try { const data = await getAllPatients(user.token); setPatients(data); } catch (error) { console.error(error); notification.error({ message: "Ошибка загрузки пациентов", description: "Проверьте подключение к сети.", }); } }; const fetchLenses = async () => { try { const data = await getNotIssuedLenses(user.token); setLenses(data); } catch (error) { console.error(error); notification.error({ message: "Ошибка загрузки линз", description: "Проверьте подключение к сети.", }); } }; const handleOk = async () => { try { setLoading(true); setCurrentStep(0); onSubmit(issueDate.format("YYYY-MM-DD"), selectedPatient.id, selectedLens.id); setSelectedPatient(null); setSelectedLens(null); setIssueDate(dayjs(new Date())); setLoading(false); } catch (errorInfo) { console.error("Validation Failed:", errorInfo); } }; const filteredPatients = patients .filter((patient) => { const searchLower = searchPatientString.toLowerCase(); return Object.values(patient) .filter(value => typeof value === "string") .some(value => value.toLowerCase().includes(searchLower)); }); const patientsItems = filteredPatients.map((patient) => ({ key: patient.id, label: `${patient.last_name} ${patient.first_name} (${new Date(patient.birthday).toLocaleDateString("ru-RU")})`, children:

Пациент: {patient.last_name} {patient.first_name}

Дата рождения: {new Date(patient.birthday).toLocaleDateString("ru-RU")}

Диагноз: {patient.diagnosis}

Email: {patient.email}

Телефон: {patient.phone}

, })); const filteredLenses = lenses.filter((lens) => { const searchLower = searchLensString.toLowerCase(); return Object.values(lens) .filter(value => typeof value === "string") .some(value => value.toLowerCase().includes(searchLower)); }) const lensesItems = filteredLenses.map((lens) => ({ key: lens.id, label: `Линза ${lens.side} ${lens.diameter} мм`, children:

Диаметр: {lens.diameter}

Тор: {lens.tor}

Пресетная рефракция: {lens.preset_refraction}

Диаметр: {lens.diameter}

FVC: {lens.fvc}

Острота зрения (Trial): {lens.trial}

Периферийная торичность: {lens.periphery_toricity}

Сторона: {lens.side}

Esa: {lens.esa}

, })); const SelectPatientStep = () => { return selectedPatient ? (
{selectedPatient.last_name} {selectedPatient.first_name}

Дата рождения: {new Date(selectedPatient.birthday).toLocaleDateString("ru-RU")}

Email: {selectedPatient.email}

Телефон: {selectedPatient.phone}

) : (<> setSearchPatientString(e.target.value)} style={{marginBottom: 16}} allowClear />
) }; const SelectLensStep = () => { return selectedLens ? (
{selectedLens.diameter} {selectedLens.tor} {selectedLens.preset_refraction}

Диаметр: {selectedLens.diameter}

Тор: {selectedLens.tor}

Пресетная рефракция: {selectedLens.preset_refraction}

Диаметр: {selectedLens.diameter}

FVC: {selectedLens.fvc}

Острота зрения (Trial): {selectedLens.trial}

Периферийная торичность: {selectedLens.periphery_toricity}

Сторона: {selectedLens.side}

Esa: {selectedLens.esa}

) : (<> setSearchLensString(e.target.value)} style={{marginBottom: 16}} allowClear />
) }; const ConfirmStep = () => { return (<> Дата выдачи: {issueDate?.toDate().toLocaleDateString("ru-RU")} setIssueDate(date)} format="DD.MM.YYYY" style={{width: "100%"}} />
{selectedPatient.last_name} {selectedPatient.first_name}

Дата рождения: {new Date(selectedPatient.birthday).toLocaleDateString("ru-RU")}

Email: {selectedPatient.email}

Телефон: {selectedPatient.phone}

Линза: {selectedLens.diameter} {selectedLens.tor} {selectedLens.preset_refraction}

{selectedLens.diameter} {selectedLens.tor} {selectedLens.preset_refraction}

Диаметр: {selectedLens.diameter}

Тор: {selectedLens.tor}

Пресетная рефракция: {selectedLens.preset_refraction}

Диаметр: {selectedLens.diameter}

FVC: {selectedLens.fvc}

Острота зрения (Trial): {selectedLens.trial}

Периферийная торичность: {selectedLens.periphery_toricity}

Сторона: {selectedLens.side}

Esa: {selectedLens.esa}

); }; const steps = [{ title: 'Выбор пациента', content: , }, { title: 'Выбор линзы', content: , }, { title: 'Подтверждение', content: , },]; const isActiveNextButton = () => { if (currentStep === 0 && !selectedPatient) { return false; } return !(currentStep === 1 && !selectedLens); }; const isActivePrevButton = () => { return currentStep > 0; }; const isActiveFinishButton = () => { return currentStep === steps.length - 1; }; return ( { setSelectedPatient(null); setSelectedLens(null); setCurrentStep(0); setIssueDate(dayjs(new Date())); onCancel(); }} footer={null} maskClosable={false} width={!screens.xs ? 700 : "90%"} centered > {loading ? (
) : (
{steps[currentStep].content}
)} {!screens.xs && ()}
); }; LensIssueFormModal.propTypes = { visible: PropTypes.bool.isRequired, onCancel: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired, }; export default LensIssueFormModal;