import { onLoad, onShow } from '@dcloudio/uni-app' import { ref } from 'vue' import { resolveValidHomeId } from '@/api/home' import { createLocation, fetchRoomList } from '@/api/location' import { getErrorMessage } from '@/utils/api-error' import { parseId, sameId } from '@/utils/device-id' import type { RoomOption } from '@/types/location' import { stashRoomSelection } from '../../device-list-patch' import type { DeviceRoomRouteQuery } from './room.types' let openerEventChannel: UniApp.EventChannel | undefined export function useDeviceRoom() { const homeId = ref('') const deviceId = ref('') const selectedLocationId = ref('') const pendingLocationName = ref() const roomOptions = ref([]) const listLoading = ref(false) const saving = ref(false) const addDialogVisible = ref(false) const initialized = ref(false) function applyQuery(query?: DeviceRoomRouteQuery) { if (query?.deviceId) deviceId.value = String(query.deviceId) const locationId = parseId(query?.locationId) if (locationId) { selectedLocationId.value = locationId } if (query?.locationName) { pendingLocationName.value = decodeURIComponent(String(query.locationName)) } } function syncSelectedLocation() { const options = roomOptions.value if (!options.length) return if (selectedLocationId.value) { const matchedById = options.find((item) => sameId(item.locationId, selectedLocationId.value), ) if (matchedById) return } if (pendingLocationName.value) { const matchedByName = options.find((item) => item.name === pendingLocationName.value) if (matchedByName) { selectedLocationId.value = String(matchedByName.locationId) } } } async function resolveHomeId(query?: DeviceRoomRouteQuery) { homeId.value = await resolveValidHomeId(query?.homeId) } async function loadLocations() { if (!homeId.value) return listLoading.value = true try { roomOptions.value = await fetchRoomList(homeId.value) syncSelectedLocation() } catch (err) { uni.showToast({ title: getErrorMessage(err, '加载房间列表失败'), icon: 'none' }) } finally { listLoading.value = false } } async function initPage(query?: DeviceRoomRouteQuery) { applyQuery(query) await resolveHomeId(query) if (!homeId.value) { uni.showToast({ title: '未找到家庭信息', icon: 'none' }) return } await loadLocations() initialized.value = true } function onOpenAddDialog() { addDialogVisible.value = true } async function onConfirmAddRoom(name: string) { const trimmed = name.trim() if (!trimmed) { uni.showToast({ title: '请输入位置名称', icon: 'none' }) return } if (!homeId.value) { uni.showToast({ title: '未找到家庭信息', icon: 'none' }) return } if (roomOptions.value.some((item) => item.name === trimmed)) { uni.showToast({ title: '位置名称已存在', icon: 'none' }) return } if (saving.value) return saving.value = true try { await createLocation({ homeId: homeId.value, locationName: trimmed, }) await loadLocations() const created = roomOptions.value.find((item) => item.name === trimmed) uni.showToast({ title: '添加成功', icon: 'success' }) if (created) { void onSelectRoom(created) } } catch (err) { uni.showToast({ title: getErrorMessage(err, '添加失败'), icon: 'none' }) } finally { saving.value = false } } async function onSelectRoom(room: RoomOption) { if (saving.value || listLoading.value) return if (sameId(selectedLocationId.value, room.locationId)) { uni.navigateBack() return } selectedLocationId.value = String(room.locationId) pendingLocationName.value = room.name const payload = { locationId: room.locationId, name: room.name, } stashRoomSelection({ deviceId: deviceId.value, locationId: payload.locationId, name: payload.name, }) openerEventChannel?.emit('roomSelected', payload) uni.navigateBack() } onLoad((query?: DeviceRoomRouteQuery) => { const pages = getCurrentPages() const currentPage = pages[pages.length - 1] as { getOpenerEventChannel?: () => UniApp.EventChannel } if (typeof currentPage.getOpenerEventChannel === 'function') { openerEventChannel = currentPage.getOpenerEventChannel() } void initPage(query) }) onShow(() => { if (!initialized.value || !homeId.value) return void loadLocations() }) function isRoomSelected(locationId: string | number) { return sameId(locationId, selectedLocationId.value) } return { roomOptions, selectedLocationId, listLoading, saving, addDialogVisible, isRoomSelected, onOpenAddDialog, onConfirmAddRoom, onSelectRoom, } }