|  |  | 
 |  |  | import React from 'react'; | 
 |  |  | import React, { useEffect } from 'react'; | 
 |  |  | import { useTranslate } from "react-admin"; | 
 |  |  | import { useForm, Controller } from 'react-hook-form'; | 
 |  |  | import { | 
 |  |  | 
 |  |  | import CheckOutlinedIcon from '@mui/icons-material/CheckOutlined'; | 
 |  |  | import RestartAltIcon from '@mui/icons-material/RestartAlt'; | 
 |  |  | import useCoolHook from './useCoolHook'; | 
 |  |  | import { handleControlAgv } from '../../http'; | 
 |  |  |  | 
 |  |  | function AgvControl(props) { | 
 |  |  |     const { curAgvNo } = props; | 
 |  |  |     const theme = useTheme(); | 
 |  |  |     const translate = useTranslate(); | 
 |  |  |  | 
 |  |  |     const { control, handleSubmit, reset, watch } = useForm({ | 
 |  |  |     const { control, handleSubmit, reset, watch, setValue } = useForm({ | 
 |  |  |         defaultValues: { | 
 |  |  |             taskMode: 'MOVE', | 
 |  |  |             startCode: '', | 
 |  |  | 
 |  |  |     }); | 
 |  |  |  | 
 |  |  |     const taskModes = [ | 
 |  |  |         { value: 'MOVE', label: '移动' }, | 
 |  |  |         { value: 'TO_CHARGE', label: '去充电' }, | 
 |  |  |         { value: 'TO_STANDBY', label: '去待机位' }, | 
 |  |  |         { value: 'LOC_TO_LOC', label: '库位到库位' }, | 
 |  |  |         { value: 'LOC_TO_STA', label: '库位到站点' }, | 
 |  |  |         { value: 'STA_TO_LOC', label: '站点到库位' }, | 
 |  |  |         { value: 'STA_TO_STA', label: '站点到站点' }, | 
 |  |  |         { value: 'MOVE', label: translate('page.map.insight.control.type.MOVE') }, | 
 |  |  |         { value: 'TO_CHARGE', label: translate('page.map.insight.control.type.TO_CHARGE') }, | 
 |  |  |         { value: 'TO_STANDBY', label: translate('page.map.insight.control.type.TO_STANDBY') }, | 
 |  |  |         { value: 'LOC_TO_LOC', label: translate('page.map.insight.control.type.LOC_TO_LOC') }, | 
 |  |  |         { value: 'LOC_TO_STA', label: translate('page.map.insight.control.type.LOC_TO_STA') }, | 
 |  |  |         { value: 'STA_TO_LOC', label: translate('page.map.insight.control.type.STA_TO_LOC') }, | 
 |  |  |         { value: 'STA_TO_STA', label: translate('page.map.insight.control.type.STA_TO_STA') }, | 
 |  |  |     ]; | 
 |  |  |  | 
 |  |  |     useEffect(() => { | 
 |  |  |         reset(); | 
 |  |  |     }, [curAgvNo, reset]); | 
 |  |  |  | 
 |  |  |     const onSubmit = (data) => { | 
 |  |  |         console.log(data); | 
 |  |  |         if (curAgvNo) { | 
 |  |  |             handleControlAgv({ agvNo: curAgvNo, ...data }, () => { | 
 |  |  |  | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  |     }; | 
 |  |  |  | 
 |  |  |     const taskMode = watch('taskMode'); | 
 |  |  | 
 |  |  |     const { | 
 |  |  |         options: endCodeOptions, | 
 |  |  |         setInputValue: setEndCodeInputValue, | 
 |  |  |         resetInput: resetEndCodeInput, | 
 |  |  |     } = useCoolHook('/code/page', 'data'); | 
 |  |  |  | 
 |  |  |     const { | 
 |  |  |         options: startLocOptions, | 
 |  |  |         setInputValue: setStartLocInputValue, | 
 |  |  |         resetInput: resetStartLocInput, | 
 |  |  |     } = useCoolHook('/loc/page', 'locNo'); | 
 |  |  |  | 
 |  |  |     const { | 
 |  |  |         options: endLocOptions, | 
 |  |  |         setInputValue: setEndLocInputValue, | 
 |  |  |         resetInput: resetEndLocInput, | 
 |  |  |     } = useCoolHook('/loc/page', 'locNo'); | 
 |  |  |  | 
 |  |  |     const { | 
 |  |  |         options: startStaOptions, | 
 |  |  |         setInputValue: setStartStaInputValue, | 
 |  |  |         resetInput: resetStartStaInput, | 
 |  |  |     } = useCoolHook('/sta/page', 'staNo'); | 
 |  |  |  | 
 |  |  |     const { | 
 |  |  |         options: endStaOptions, | 
 |  |  |         setInputValue: setEndStaInputValue, | 
 |  |  |         resetInput: resetEndStaInput, | 
 |  |  |     } = useCoolHook('/sta/page', 'staNo'); | 
 |  |  |  | 
 |  |  |     useEffect(() => { | 
 |  |  |         const fieldsToClear = ['endCode', 'startLocNo', 'endLocNo', 'startStaNo', 'endStaNo']; | 
 |  |  |  | 
 |  |  |         fieldsToClear.forEach(field => { | 
 |  |  |             if (!showField(field)) { | 
 |  |  |                 setValue(field, ''); | 
 |  |  |  | 
 |  |  |                 switch (field) { | 
 |  |  |                     case 'endCode': | 
 |  |  |                         resetEndCodeInput(); | 
 |  |  |                         break; | 
 |  |  |                     case 'startLocNo': | 
 |  |  |                         resetStartLocInput(); | 
 |  |  |                         break; | 
 |  |  |                     case 'endLocNo': | 
 |  |  |                         resetEndLocInput(); | 
 |  |  |                         break; | 
 |  |  |                     case 'startStaNo': | 
 |  |  |                         resetStartStaInput(); | 
 |  |  |                         break; | 
 |  |  |                     case 'endStaNo': | 
 |  |  |                         resetEndStaInput(); | 
 |  |  |                         break; | 
 |  |  |                     default: | 
 |  |  |                         break; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     }, [ | 
 |  |  |         taskMode, | 
 |  |  |         setValue, | 
 |  |  |         showField, | 
 |  |  |         resetEndCodeInput, | 
 |  |  |         resetStartLocInput, | 
 |  |  |         resetEndLocInput, | 
 |  |  |         resetStartStaInput, | 
 |  |  |         resetEndStaInput | 
 |  |  |     ]); | 
 |  |  |     return ( | 
 |  |  |         <> | 
 |  |  |             <form onSubmit={handleSubmit(onSubmit)}> | 
 |  |  | 
 |  |  |                                     <Controller | 
 |  |  |                                         name="startCode" | 
 |  |  |                                         control={control} | 
 |  |  |                                         rules={{ required: '起始地面码不能为空' }} | 
 |  |  |                                         rules={{ required: translate('ra.validation.required') }} | 
 |  |  |                                         render={({ field, fieldState }) => ( | 
 |  |  |                                             <TextField | 
 |  |  |                                                 {...field} | 
 |  |  |                                                 fullWidth | 
 |  |  |                                                 label="起始地面码" | 
 |  |  |                                                 label={translate('page.map.insight.control.startCode')} | 
 |  |  |                                                 error={!!fieldState.error} | 
 |  |  |                                                 helperText={fieldState.error?.message} | 
 |  |  |                                             /> | 
 |  |  | 
 |  |  |                                     <Controller | 
 |  |  |                                         name="endCode" | 
 |  |  |                                         control={control} | 
 |  |  |                                         rules={{ required: '目标地面码不能为空' }} | 
 |  |  |                                         rules={{ required: translate('ra.validation.required') }} | 
 |  |  |                                         render={({ field, fieldState }) => { | 
 |  |  |                                             const selectedOption = endCodeOptions.find(option => option.id === field.value) || null; | 
 |  |  |                                             return ( | 
 |  |  | 
 |  |  |                                                     renderInput={(params) => ( | 
 |  |  |                                                         <TextField | 
 |  |  |                                                             {...params} | 
 |  |  |                                                             label="目标地面码" | 
 |  |  |                                                             label={translate('page.map.insight.control.endCode')} | 
 |  |  |                                                             error={!!fieldState.error} | 
 |  |  |                                                             helperText={fieldState.error?.message} | 
 |  |  |                                                         /> | 
 |  |  | 
 |  |  |                                     <Controller | 
 |  |  |                                         name="startLocNo" | 
 |  |  |                                         control={control} | 
 |  |  |                                         rules={{ required: '起始库位不能为空' }} | 
 |  |  |                                         rules={{ required: translate('ra.validation.required') }} | 
 |  |  |                                         render={({ field, fieldState }) => { | 
 |  |  |                                             const selectedOption = startLocOptions.find(option => option.id === field.value) || null; | 
 |  |  |                                             return ( | 
 |  |  | 
 |  |  |                                                     renderInput={(params) => ( | 
 |  |  |                                                         <TextField | 
 |  |  |                                                             {...params} | 
 |  |  |                                                             label="起始库位" | 
 |  |  |                                                             label={translate('page.map.insight.control.startLoc')} | 
 |  |  |                                                             error={!!fieldState.error} | 
 |  |  |                                                             helperText={fieldState.error?.message} | 
 |  |  |                                                         /> | 
 |  |  | 
 |  |  |                                     <Controller | 
 |  |  |                                         name="endLocNo" | 
 |  |  |                                         control={control} | 
 |  |  |                                         rules={{ required: '目标库位不能为空' }} | 
 |  |  |                                         rules={{ required: translate('ra.validation.required') }} | 
 |  |  |                                         render={({ field, fieldState }) => { | 
 |  |  |                                             const selectedOption = endLocOptions.find(option => option.id === field.value) || null; | 
 |  |  |                                             return ( | 
 |  |  | 
 |  |  |                                                     renderInput={(params) => ( | 
 |  |  |                                                         <TextField | 
 |  |  |                                                             {...params} | 
 |  |  |                                                             label="目标库位" | 
 |  |  |                                                             label={translate('page.map.insight.control.endLoc')} | 
 |  |  |                                                             error={!!fieldState.error} | 
 |  |  |                                                             helperText={fieldState.error?.message} | 
 |  |  |                                                         /> | 
 |  |  | 
 |  |  |                                     <Controller | 
 |  |  |                                         name="startStaNo" | 
 |  |  |                                         control={control} | 
 |  |  |                                         rules={{ required: '起始站点不能为空' }} | 
 |  |  |                                         rules={{ required: translate('ra.validation.required') }} | 
 |  |  |                                         render={({ field, fieldState }) => { | 
 |  |  |                                             const selectedOption = startStaOptions.find(option => option.id === field.value) || null; | 
 |  |  |                                             return ( | 
 |  |  | 
 |  |  |                                                     renderInput={(params) => ( | 
 |  |  |                                                         <TextField | 
 |  |  |                                                             {...params} | 
 |  |  |                                                             label="起始站点" | 
 |  |  |                                                             label={translate('page.map.insight.control.startSta')} | 
 |  |  |                                                             error={!!fieldState.error} | 
 |  |  |                                                             helperText={fieldState.error?.message} | 
 |  |  |                                                         /> | 
 |  |  | 
 |  |  |                                     <Controller | 
 |  |  |                                         name="endStaNo" | 
 |  |  |                                         control={control} | 
 |  |  |                                         rules={{ required: '目标站点不能为空' }} | 
 |  |  |                                         rules={{ required: translate('ra.validation.required') }} | 
 |  |  |                                         render={({ field, fieldState }) => { | 
 |  |  |                                             const selectedOption = endStaOptions.find(option => option.id === field.value) || null; | 
 |  |  |                                             return ( | 
 |  |  | 
 |  |  |                                                     renderInput={(params) => ( | 
 |  |  |                                                         <TextField | 
 |  |  |                                                             {...params} | 
 |  |  |                                                             label="目标站点" | 
 |  |  |                                                             label={translate('page.map.insight.control.endSta')} | 
 |  |  |                                                             error={!!fieldState.error} | 
 |  |  |                                                             helperText={fieldState.error?.message} | 
 |  |  |                                                         /> | 
 |  |  | 
 |  |  |                             variant="outlined" | 
 |  |  |                             color="primary" | 
 |  |  |                             type="submit" | 
 |  |  |                             onClick={() => { | 
 |  |  |  | 
 |  |  |                             }} | 
 |  |  |                             sx={{ | 
 |  |  |                                 borderColor: theme => theme.palette.primary.main, | 
 |  |  |                                 color: theme => theme.palette.primary.main, |