import { BookingDetail, Day, Draft, Project, Slot } from './types.uts' const DRAFT_KEY = 'sf_booking_draft' const LAST_BOOKING_KEY = 'sf_last_booking' export function emptyDraft(): Draft { return { project: null, day: null, date: '', slot: null, phone: '' } as Draft } export function getDraft(): Draft { const raw = uni.getStorageSync(DRAFT_KEY) if (raw == null || raw == '') return emptyDraft() const draft = raw as Draft if (draft.phone == null) draft.phone = '' if (draft.date == null) draft.date = '' return draft } export function saveDraft(draft: Draft): Draft { uni.setStorageSync(DRAFT_KEY, draft) return draft } export function clearDraft(): void { const phone = getDraft().phone uni.removeStorageSync(DRAFT_KEY) if (phone.length > 0) { saveDraft({ project: null, day: null, date: '', slot: null, phone } as Draft) } } export function setProject(project: Project): Draft { const draft = getDraft() const next = { project: { id: project.id, category_id: project.category_id, name: project.name, subtitle: project.subtitle, price_text: project.price_text, image: project.image, image_key: project.image_key, sortIndex: project.sortIndex } as Project, day: null, date: '', slot: null, phone: draft.phone } as Draft return saveDraft(next) } export function setSchedule(day: Day, slot: Slot): Draft { const draft = getDraft() const nextDay = { date: day.date, status: day.status, slots: [] } as Day const nextSlot = { id: slot.id, start_time: slot.start_time, status: slot.status } as Slot const next = { project: draft.project, day: nextDay, date: nextDay.date, slot: nextSlot, phone: draft.phone } as Draft return saveDraft(next) } export function setPhone(phone: string): Draft { const draft = getDraft() draft.phone = phone return saveDraft(draft) } export function setLastBooking(detail: BookingDetail): void { uni.setStorageSync(LAST_BOOKING_KEY, detail) } export function getLastBooking(): BookingDetail | null { const raw = uni.getStorageSync(LAST_BOOKING_KEY) if (raw == null || raw == '') return null return raw as BookingDetail }