import { useEffect, useState } from 'react'; import { hotelStaffTypeMappingListUsingPost } from '@api/typeMapping'; import type { RoomTypeMapping } from '@api/types'; import type { TaskOperationInput } from '../../../stores'; import DialogSurface from './DialogSurface'; type TaskOperationDialogProps = { open: boolean; onClose: () => void; onConfirm: (options: TaskOperationInput) => Promise; }; type RoomTypeMappingLike = RoomTypeMapping & { dyHotSpringName?: string; }; const OPERATION_OPTIONS: Array<{ label: string; value: 'open' | 'close' }> = [ { label: '开启', value: 'open' }, { label: '关闭', value: 'close' }, ]; function normalizeRoomType(item: RoomTypeMappingLike): RoomTypeMappingLike { return { ...item, dyHotSpringName: item.dyHotSpringName ?? item.dyHotSrpingName ?? '', dyHotSrpingName: item.dyHotSrpingName ?? item.dyHotSpringName ?? '', }; } export default function TaskOperationDialog({ open, onClose, onConfirm, }: TaskOperationDialogProps) { const [roomList, setRoomList] = useState([]); const [roomType, setRoomType] = useState(''); const [startTime, setStartTime] = useState(''); const [endTime, setEndTime] = useState(''); const [operation, setOperation] = useState<'open' | 'close' | ''>(''); const [loading, setLoading] = useState(false); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!open) return; setRoomType(''); setStartTime(''); setEndTime(''); setOperation(''); setError(null); setSubmitting(false); setLoading(true); void (async () => { try { const response = await hotelStaffTypeMappingListUsingPost({ body: {} as RoomTypeMapping }); setRoomList(Array.isArray(response?.data) ? response.data.map((item: RoomTypeMappingLike) => normalizeRoomType(item)) : []); } catch (requestError) { setRoomList([]); setError(requestError instanceof Error ? requestError.message : String(requestError)); } finally { setLoading(false); } })(); }, [open]); async function handleConfirm(): Promise { if (!roomType) { setError('请选择房型'); return; } if (!startTime || !endTime) { setError('请选择日期范围'); return; } if (startTime > endTime) { setError('开始日期不能晚于结束日期'); return; } if (!operation) { setError('请选择操作'); return; } setSubmitting(true); setError(null); try { await onConfirm({ roomType, startTime, endTime, operation, roomList: roomList.map((item) => ({ ...item })), }); } catch (confirmError) { setError(confirmError instanceof Error ? confirmError.message : String(confirmError)); setSubmitting(false); return; } setSubmitting(false); } return (
房型列表沿用现有 `typeMapping` 接口,提交时会兼容 `dyHotSrpingName / dyHotSpringName` 两种字段,避免任务执行时遗漏抖音温泉渠道。
{error ? (
{error}
) : null}
); }