import { createBoxes } from '@/data/demo-data.js' import { createItemStates } from '@/utils/picking-state.js' import { stableTaskIdentity, stableTaskIdentityKey } from '@/utils/task-identity.js' function clone(value) { return JSON.parse(JSON.stringify(value)) } function createInitialState() { const boxes = createBoxes() return { boxes, currentBoxIndex: 0, itemStates: createItemStates(boxes[0].items), partialQuantities: {}, scannedBoxCodes: {}, boxDrafts: {}, completedBoxes: {}, scanValue: '', scanHistory: [] } } let state = createInitialState() const STORAGE_PREFIX = 'pda-picking-draft' function storageAvailable() { return typeof uni !== 'undefined' && typeof uni.getStorageSync === 'function' && typeof uni.setStorageSync === 'function' } export function getPickingDraftKey(identity = {}) { const identityKey = stableTaskIdentityKey(identity) return identityKey ? `${STORAGE_PREFIX}:${identityKey}` : '' } function draftPayload(nextState, identity) { const value = stableTaskIdentity(identity) return { operatorId: value.operatorId, taskId: value.taskId, loadingPlanId: value.loadingPlanId, containerBookingId: value.containerBookingId, warehouseId: identity.warehouseId ?? null, currentBoxIndex: nextState.currentBoxIndex, itemStates: nextState.itemStates, partialQuantities: nextState.partialQuantities, scannedBoxCodes: nextState.scannedBoxCodes, boxDrafts: nextState.boxDrafts, completedBoxes: nextState.completedBoxes, scanHistory: nextState.scanHistory, updatedAt: new Date().toISOString() } } export function getPickingSession() { return clone(state) } export function savePickingSession(nextState, identity = {}) { state = clone(nextState) const key = getPickingDraftKey(identity) if (key && storageAvailable()) uni.setStorageSync(key, draftPayload(state, identity)) } export function restorePickingSession(identity = {}) { const key = getPickingDraftKey(identity) if (!key || !storageAvailable()) return null const saved = uni.getStorageSync(key) const expected = stableTaskIdentity(identity) if (!saved || saved.operatorId !== expected.operatorId || saved.taskId !== expected.taskId || saved.loadingPlanId !== expected.loadingPlanId || saved.containerBookingId !== expected.containerBookingId) return null const restored = { ...state, ...clone(saved) } delete restored.operatorId delete restored.taskId delete restored.loadingPlanId delete restored.containerBookingId delete restored.warehouseId delete restored.updatedAt state = restored return clone(state) } export function clearPickingDraft(identity = {}) { const key = getPickingDraftKey(identity) if (key && typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(key) } export function resetPickingSession() { state = createInitialState() } export function getPickingOverview() { const completedCount = Object.values(state.completedBoxes).filter(Boolean).length return { boxes: clone(state.boxes), completedBoxes: { ...state.completedBoxes }, completedCount, pendingBoxCount: state.boxes.length - completedCount } }