import { Autocomplete, Box, TextField, FormControlLabel, Checkbox, Typography, } from "@mui/material"; import { Edit, useAutocomplete } from "@refinedev/mui"; import { useForm } from "@refinedev/react-hook-form"; import { Controller } from "react-hook-form"; import { useParams } from "react-router"; import { LinkedItems } from "../../components/LinkedItems"; import { StationItem, VehicleItem, stationFields, vehicleFields, } from "./types"; export const RouteEdit = () => { const { saveButtonProps, register, control, formState: { errors }, } = useForm({}); const { id: routeId } = useParams<{ id: string }>(); const { autocompleteProps: carrierAutocompleteProps } = useAutocomplete({ resource: "carrier", onSearch: (value) => [ { field: "short_name", operator: "contains", value, }, ], }); return ( ( option.id === field.value ) || null } onChange={(_, value) => { field.onChange(value?.id || ""); }} getOptionLabel={(item) => { return item ? item.short_name : ""; }} isOptionEqualToValue={(option, value) => { return option.id === value?.id; }} filterOptions={(options, { inputValue }) => { return options.filter((option) => option.short_name .toLowerCase() .includes(inputValue.toLowerCase()) ); }} renderInput={(params) => ( )} /> )} /> String(value), })} error={!!(errors as any)?.route_number} helperText={(errors as any)?.route_number?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="text" label={"Номер маршрута"} name="route_number" /> ( field.onChange(e.target.checked)} /> } /> )} /> (Прямой / Обратный) { try { // Разбиваем строку на строки и парсим каждую строку как пару координат const lines = value.trim().split("\n"); return lines.map((line) => { const [lat, lon] = line .trim() .split(/[\s,]+/) .map(Number); if (isNaN(lat) || isNaN(lon)) { throw new Error("Invalid coordinates"); } return [lat, lon]; }); } catch { return []; } }, validate: (value: unknown) => { if (!Array.isArray(value)) return "Неверный формат"; if (value.length === 0) return "Введите хотя бы одну пару координат"; if ( !value.every( (point: unknown) => Array.isArray(point) && point.length === 2 ) ) { return "Каждая строка должна содержать две координаты"; } if ( !value.every((point: unknown[]) => point.every( (coord: unknown) => !isNaN(Number(coord)) && typeof coord === "number" ) ) ) { return "Координаты должны быть числами"; } return true; }, })} error={!!(errors as any)?.path} helperText={(errors as any)?.path?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="text" label={"Координаты маршрута *"} name="path" placeholder="55.7558 37.6173 55.7539 37.6208" multiline rows={4} sx={{ marginBottom: 2, }} /> Number(value), })} error={!!(errors as any)?.scale_min} helperText={(errors as any)?.scale_min?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="number" label={"Масштаб (мин)"} name="scale_min" /> Number(value), })} error={!!(errors as any)?.scale_max} helperText={(errors as any)?.scale_max?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="number" label={"Масштаб (макс)"} name="scale_max" /> Number(value), })} error={!!(errors as any)?.rotate} helperText={(errors as any)?.rotate?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="number" label={"Поворот"} name="rotate" /> Number(value), })} error={!!(errors as any)?.center_latitude} helperText={(errors as any)?.center_latitude?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="number" label={"Центр. широта"} name="center_latitude" /> Number(value), })} error={!!(errors as any)?.center_longitude} helperText={(errors as any)?.center_longitude?.message} margin="normal" fullWidth InputLabelProps={{ shrink: true }} type="number" label={"Центр. долгота"} name="center_longitude" /> {routeId && ( <> type="edit" parentId={routeId} parentResource="route" childResource="station" fields={stationFields} title="станции" /> type="edit" parentId={routeId} parentResource="route" childResource="vehicle" fields={vehicleFields} title="транспортные средства" /> )} ); };