900 lines
29 KiB
Vue
900 lines
29 KiB
Vue
<template>
|
||
<MobileShell title="新建预约">
|
||
<div v-if="draftSavedAtText" class="card draft-tip">
|
||
<span>{{ draftSavedAtText }}</span>
|
||
<button class="text-btn" type="button" @click="clearDraft">清空</button>
|
||
</div>
|
||
|
||
<section class="card form-card">
|
||
<div class="field field--stack customer-combobox">
|
||
<label class="required">客户</label>
|
||
<div class="customer-control">
|
||
<button v-if="selectedCustomerReady" class="customer-selected-row" type="button" @click="openCustomerDropdown">
|
||
<span>{{ form.customerName || '未填写姓名' }}</span>
|
||
<strong>{{ form.phone || '未填写手机号' }}</strong>
|
||
</button>
|
||
<input
|
||
v-else
|
||
v-model.trim="customerSearchText"
|
||
placeholder="搜索客户姓名或手机号"
|
||
@focus="openCustomerDropdown"
|
||
@input="onCustomerSearchInput"
|
||
@keydown.escape="customerDropdownOpen = false"
|
||
/>
|
||
<button v-if="customerSearchText || selectedCustomerReady" class="customer-clear" type="button" @click="clearCustomerSelection">×</button>
|
||
</div>
|
||
<div v-if="customerDropdownOpen" class="customer-dropdown">
|
||
<div v-if="customerSearching" class="customer-muted">正在搜索客户...</div>
|
||
<template v-else>
|
||
<div v-if="customerOptions.length > 0" class="customer-options-scroll">
|
||
<button v-for="customer in customerOptions" :key="String(customer.id)" class="customer-option" type="button" @click="selectCustomer(customer)">
|
||
<strong>{{ customer.nickname || customer.account || customer.phone || customer.contactPhone }}</strong>
|
||
<span>{{ customer.phone || customer.contactPhone || '未填写手机号' }}</span>
|
||
</button>
|
||
</div>
|
||
<div v-else class="customer-muted">暂无匹配客户,可继续创建新客户</div>
|
||
</template>
|
||
<div class="customer-new">
|
||
<strong>新客户</strong>
|
||
<input v-model.trim="newCustomerName" placeholder="客户姓名" />
|
||
<input v-model.trim="newCustomerPhone" inputmode="numeric" placeholder="手机号" />
|
||
<button class="secondary-btn" type="button" @click="useNewCustomer">使用新客户</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="field"><label class="required">预约日期</label><input v-model="form.appointmentDate" type="date" @change="onDateChange" /></div>
|
||
<div class="field"><label class="required">预约作品</label><button class="field-value picker-value" type="button" @click="productModalVisible = true">{{ selectedProductTitle }}</button></div>
|
||
<div class="field"><label class="required">预计时长</label><input v-model.trim="form.durationMinutes" inputmode="numeric" placeholder="分钟" @input="refreshAvailableTimes" /></div>
|
||
<div class="field"><label class="required">指定员工</label><select v-model="form.artistId" @change="refreshAvailableTimes"><option value="">请选择员工</option><option v-for="artist in artists" :key="String(artist.id)" :value="String(artist.id)">{{ artist.name || artist.nickname }}</option></select></div>
|
||
<div class="field"><label class="required">预约时间</label><button class="field-value picker-value" type="button" @click="openTimeSheet">{{ form.appointmentTime || timeSelectTip }}</button></div>
|
||
<div class="field field--stack"><label>备注</label><textarea v-model.trim="form.remark" placeholder="请输入预约备注"></textarea></div>
|
||
</section>
|
||
|
||
<div v-if="conflictTip" class="conflict-tip" :class="`conflict-tip--${conflictLevel}`">{{ conflictTip }}</div>
|
||
|
||
<div class="fixed-bottom">
|
||
<div class="fixed-bottom__inner button-row">
|
||
<button class="secondary-btn" type="button" @click="saveDraft">保存草稿</button>
|
||
<button class="primary-btn" type="button" :disabled="loading" @click="submit">{{ loading ? '提交中...' : '提交预约' }}</button>
|
||
</div>
|
||
</div>
|
||
|
||
<BaseModal
|
||
:model-value="productModalVisible"
|
||
title="选择预约作品"
|
||
:has-unsaved-changes="manualProductChanged"
|
||
@update:model-value="updateProductModalVisible"
|
||
>
|
||
<div class="product-modal">
|
||
<input v-model.trim="productKeyword" class="search-input" placeholder="搜索作品、标签、美甲师" @input="filterProducts" />
|
||
<div class="list">
|
||
<button v-for="product in filteredProducts" :key="String(product.id)" class="product-card" type="button" @click="selectProduct(product)">
|
||
<img v-if="product.coverImage" :src="product.coverImage" alt="" />
|
||
<div>
|
||
<strong>{{ product.title }}</strong>
|
||
<span>{{ product.priceText }} · {{ product.durationText || '时长待定' }}</span>
|
||
<small>{{ product.artistText }}</small>
|
||
<div v-if="product.tagList.length > 0" class="tag-row">
|
||
<i v-for="tag in product.tagList" :key="tag">{{ tag }}</i>
|
||
</div>
|
||
</div>
|
||
</button>
|
||
<EmptyState v-if="filteredProducts.length === 0" title="暂无匹配作品" description="可以调整关键词或手动新增预约作品" />
|
||
</div>
|
||
<PageSection title="手动新增预约作品">
|
||
<div class="manual-form">
|
||
<input v-model.trim="form.manualProductTitle" class="search-input" placeholder="作品名称" />
|
||
<input v-model.trim="form.manualProductPrice" class="search-input" inputmode="decimal" placeholder="价格" />
|
||
<input v-model.trim="form.manualProductTags" class="search-input" placeholder="标签,逗号分隔" />
|
||
<textarea v-model.trim="form.manualProductDescription" class="search-input textarea" placeholder="作品说明"></textarea>
|
||
<input v-model.trim="form.manualProductTargetAudience" class="search-input" placeholder="适合人群(选填)" />
|
||
<input v-model.trim="form.manualProductPrecautions" class="search-input" placeholder="注意事项(选填)" />
|
||
<div class="manual-image-row">
|
||
<img v-if="form.manualProductImageUrl" :src="form.manualProductImageUrl" alt="" />
|
||
<div v-else class="manual-image-placeholder">图</div>
|
||
<label class="secondary-btn image-upload">
|
||
{{ manualImageUploading ? '上传中...' : '上传图片' }}
|
||
<input type="file" accept="image/*" :disabled="manualImageUploading" @change="uploadManualProductImage" />
|
||
</label>
|
||
<button v-if="form.manualProductImageUrl" class="text-btn" type="button" @click="removeManualProductImage">移除</button>
|
||
</div>
|
||
<button class="secondary-btn" type="button" @click="useManualProduct">使用手动作品</button>
|
||
</div>
|
||
</PageSection>
|
||
</div>
|
||
</BaseModal>
|
||
|
||
<BaseModal
|
||
:model-value="timeSheetVisible"
|
||
title="选择可约时间"
|
||
@update:model-value="updateTimeSheetVisible"
|
||
>
|
||
<div class="time-sheet">
|
||
<LoadingState v-if="timeLoading" text="正在计算可约时间" />
|
||
<EmptyState v-else-if="availableTimeGroups.length === 0" title="暂无可约时间" :description="timeSelectTip" />
|
||
<section v-for="group in availableTimeGroups" v-else :key="group.key">
|
||
<h3>{{ group.title }}</h3>
|
||
<div class="time-grid">
|
||
<button v-for="time in group.times" :key="time.value" type="button" @click="selectAvailableTime(time.value)">
|
||
<strong>{{ time.value }}</strong>
|
||
<span>{{ time.rangeText }}</span>
|
||
</button>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</BaseModal>
|
||
</MobileShell>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||
import { useRoute, useRouter } from 'vue-router'
|
||
import BaseModal from '@/components/BaseModal.vue'
|
||
import EmptyState from '@/components/EmptyState.vue'
|
||
import LoadingState from '@/components/LoadingState.vue'
|
||
import MobileShell from '@/components/MobileShell.vue'
|
||
import PageSection from '@/components/PageSection.vue'
|
||
import { managerCustomersApi } from '@/api/managerCustomers'
|
||
import { managerEmployeesApi } from '@/api/managerEmployees'
|
||
import { managerPlanningApi } from '@/api/managerPlanning'
|
||
import { managerProductsApi } from '@/api/managerProducts'
|
||
import { fileApi } from '@/api/file'
|
||
import type { AppointmentForm, Artist, Product } from '@/types/manager'
|
||
import type { UserInfo } from '@/types/common'
|
||
import { useSessionStore } from '@/stores/session'
|
||
import { useToastStore } from '@/stores/toast'
|
||
import { formatDate, minutesOfDay, timeFromMinutes } from '@/utils/date'
|
||
import { buildAvailableTimeGroups, type AvailableTimeGroup } from '@/utils/available-times'
|
||
import { buildClientRequestId, normalizeDuration } from '@/utils/planning'
|
||
import { readDraftForm, removeDraftForm, writeDraftForm } from '@/utils/appointment-draft'
|
||
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
|
||
import { markPlanningRefresh } from '@/utils/storage'
|
||
|
||
interface ProductCard extends Product {
|
||
coverImage: string
|
||
priceText: string
|
||
durationText: string
|
||
artistText: string
|
||
tagList: string[]
|
||
}
|
||
|
||
interface AppointmentModalState {
|
||
productModalVisible: boolean
|
||
timeSheetVisible: boolean
|
||
}
|
||
|
||
const APPOINTMENT_MODAL_STATE_DRAFT_NAME = 'appointment-modal-state'
|
||
|
||
const route = useRoute()
|
||
const router = useRouter()
|
||
const session = useSessionStore()
|
||
const toast = useToastStore()
|
||
const baseForm = readDraftForm(session.userInfo)
|
||
const modalStateDraft = readFormModalDraft<AppointmentModalState>(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
const form = reactive<AppointmentForm>({
|
||
customerUserId: '',
|
||
customerName: String(route.query.customerName || baseForm?.customerName || ''),
|
||
phone: String(route.query.phone || baseForm?.phone || ''),
|
||
appointmentDate: String(route.query.date || baseForm?.appointmentDate || formatDate(new Date())),
|
||
appointmentTime: baseForm?.appointmentTime || '',
|
||
productId: baseForm?.productId || '',
|
||
manualProductTitle: baseForm?.manualProductTitle || '',
|
||
manualProductPrice: baseForm?.manualProductPrice || '',
|
||
manualProductDescription: baseForm?.manualProductDescription || '',
|
||
manualProductTags: baseForm?.manualProductTags || '',
|
||
manualProductTargetAudience: baseForm?.manualProductTargetAudience || '',
|
||
manualProductPrecautions: baseForm?.manualProductPrecautions || '',
|
||
manualProductImageFileIds: baseForm?.manualProductImageFileIds || '',
|
||
manualProductImageUrl: baseForm?.manualProductImageUrl || '',
|
||
durationMinutes: baseForm?.durationMinutes || '',
|
||
artistId: String(route.query.artistId || baseForm?.artistId || ''),
|
||
remark: baseForm?.remark || ''
|
||
})
|
||
const draftSavedAtText = ref(baseForm ? '已恢复本机草稿' : '')
|
||
const customerOptions = ref<UserInfo[]>([])
|
||
const customerTimer = ref<number | null>(null)
|
||
const customerSearchText = ref([form.customerName, form.phone].filter(Boolean).join(' '))
|
||
const customerDropdownOpen = ref(false)
|
||
const customerSearching = ref(false)
|
||
const newCustomerName = ref(form.customerName)
|
||
const newCustomerPhone = ref(form.phone)
|
||
const products = ref<ProductCard[]>([])
|
||
const filteredProducts = ref<ProductCard[]>([])
|
||
const productKeyword = ref('')
|
||
const productModalVisible = ref(Boolean(modalStateDraft?.open && modalStateDraft.form.productModalVisible))
|
||
const artists = ref<Artist[]>([])
|
||
const selectedProduct = ref<ProductCard | null>(null)
|
||
const timeSheetVisible = ref(Boolean(modalStateDraft?.open && modalStateDraft.form.timeSheetVisible))
|
||
const availableTimeGroups = ref<AvailableTimeGroup[]>([])
|
||
const timeLoading = ref(false)
|
||
const timeSelectTip = ref('请先选择美甲师并填写时长')
|
||
const conflictTip = ref('')
|
||
const conflictLevel = ref<'free' | 'danger'>('free')
|
||
const loading = ref(false)
|
||
const manualImageUploading = ref(false)
|
||
const selectedCustomerReady = computed(() => {
|
||
return Boolean(form.customerName || form.phone)
|
||
})
|
||
const manualProductChanged = computed(() => {
|
||
return Boolean(
|
||
form.manualProductTitle
|
||
|| form.manualProductPrice
|
||
|| form.manualProductTags
|
||
|| form.manualProductDescription
|
||
|| form.manualProductTargetAudience
|
||
|| form.manualProductPrecautions
|
||
|| form.manualProductImageFileIds
|
||
|| form.manualProductImageUrl
|
||
)
|
||
})
|
||
const appointmentFormChanged = computed(() => {
|
||
return Boolean(
|
||
form.customerUserId
|
||
|| form.customerName
|
||
|| form.phone
|
||
|| form.appointmentTime
|
||
|| form.productId
|
||
|| form.manualProductTitle
|
||
|| form.manualProductPrice
|
||
|| form.manualProductDescription
|
||
|| form.manualProductTags
|
||
|| form.manualProductTargetAudience
|
||
|| form.manualProductPrecautions
|
||
|| form.manualProductImageFileIds
|
||
|| form.manualProductImageUrl
|
||
|| form.durationMinutes
|
||
|| form.artistId
|
||
|| form.remark
|
||
)
|
||
})
|
||
|
||
const syncAppointmentDraft = (): void => {
|
||
if (!appointmentFormChanged.value) {
|
||
return
|
||
}
|
||
writeDraftForm(session.userInfo, { ...form })
|
||
if (!draftSavedAtText.value) {
|
||
draftSavedAtText.value = '草稿已自动保存到本机'
|
||
}
|
||
}
|
||
|
||
const syncAppointmentModalState = (): void => {
|
||
if (!productModalVisible.value && !timeSheetVisible.value) {
|
||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
return
|
||
}
|
||
writeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME, {
|
||
productModalVisible: productModalVisible.value,
|
||
timeSheetVisible: timeSheetVisible.value
|
||
}, true)
|
||
}
|
||
|
||
const updateProductModalVisible = (value: boolean): void => {
|
||
productModalVisible.value = value
|
||
syncAppointmentModalState()
|
||
}
|
||
|
||
const updateTimeSheetVisible = (value: boolean): void => {
|
||
timeSheetVisible.value = value
|
||
syncAppointmentModalState()
|
||
}
|
||
|
||
const selectedProductTitle = computed(() => {
|
||
if (selectedProduct.value) {
|
||
return selectedProduct.value.title
|
||
}
|
||
if (form.manualProductTitle) {
|
||
return form.manualProductTitle
|
||
}
|
||
return '请选择作品'
|
||
})
|
||
|
||
const parseProductTags = (product: Product): string[] => {
|
||
if (Array.isArray(product.tagList) && product.tagList.length > 0) {
|
||
return product.tagList.map(tag => String(tag || '').trim()).filter(Boolean)
|
||
}
|
||
if (Array.isArray(product.tagInfos) && product.tagInfos.length > 0) {
|
||
return product.tagInfos.map(tag => String(tag.tagName || '').trim()).filter(Boolean)
|
||
}
|
||
return String(product.tags || '')
|
||
.split(/[;,,、]/)
|
||
.map(tag => tag.trim())
|
||
.filter(Boolean)
|
||
}
|
||
|
||
const resolveProductArtistText = (product: Product, artistOptions: Artist[]): string => {
|
||
const artistIds = String(product.bookableArtists || '')
|
||
.split(',')
|
||
.map(id => id.trim())
|
||
.filter(Boolean)
|
||
if (artistIds.length === 0) {
|
||
return '全部美甲师'
|
||
}
|
||
const artistNames = artistIds
|
||
.map(id => artistOptions.find(artist => String(artist.id) === id))
|
||
.filter((artist): artist is Artist => Boolean(artist))
|
||
.map(artist => artist.name || artist.nickname || '')
|
||
.filter(Boolean)
|
||
return artistNames.length > 0 ? artistNames.join('、') : '指定美甲师'
|
||
}
|
||
|
||
const normalizeProduct = (product: Product, artistOptions: Artist[]): ProductCard => {
|
||
const imageUrls = Array.isArray(product.imageUrls) ? product.imageUrls : []
|
||
const durationText = product.approximateDuration || (product.serviceDurationMinutes ? `${product.serviceDurationMinutes}分钟` : '')
|
||
return {
|
||
...product,
|
||
coverImage: imageUrls[0] || '',
|
||
tagList: parseProductTags(product).slice(0, 4),
|
||
priceText: product.price || product.price === 0 ? `¥${product.price}` : '价格待定',
|
||
durationText,
|
||
artistText: resolveProductArtistText(product, artistOptions)
|
||
}
|
||
}
|
||
|
||
const filterProducts = (): void => {
|
||
const keyword = productKeyword.value.toLowerCase()
|
||
filteredProducts.value = products.value.filter(product => {
|
||
if (!keyword) {
|
||
return true
|
||
}
|
||
return [product.title, product.priceText, product.durationText, product.artistText, ...product.tagList]
|
||
.some(value => String(value || '').toLowerCase().includes(keyword))
|
||
})
|
||
}
|
||
|
||
const loadOptions = async (): Promise<void> => {
|
||
const [productData, employeeData] = await Promise.all([
|
||
managerProductsApi.list({ page: 1, size: 100 }),
|
||
managerEmployeesApi.list()
|
||
])
|
||
artists.value = employeeData
|
||
const productList = Array.isArray(productData) ? productData : productData.list
|
||
products.value = productList.map(product => normalizeProduct(product, artists.value))
|
||
filteredProducts.value = products.value
|
||
if (form.productId) {
|
||
selectedProduct.value = products.value.find(item => String(item.id) === String(form.productId)) || null
|
||
}
|
||
}
|
||
|
||
const scheduleCustomerSearch = (): void => {
|
||
if (customerTimer.value !== null) {
|
||
window.clearTimeout(customerTimer.value)
|
||
}
|
||
customerTimer.value = window.setTimeout(() => {
|
||
void searchCustomers()
|
||
}, 250)
|
||
}
|
||
|
||
const searchCustomers = async (): Promise<void> => {
|
||
const keyword = customerSearchText.value.trim()
|
||
customerSearching.value = true
|
||
try {
|
||
const data = await managerCustomersApi.list({ page: 1, size: 20, keyword })
|
||
customerOptions.value = data.list || []
|
||
} finally {
|
||
customerSearching.value = false
|
||
}
|
||
}
|
||
|
||
const openCustomerDropdown = (): void => {
|
||
customerDropdownOpen.value = true
|
||
if (customerOptions.value.length === 0) {
|
||
scheduleCustomerSearch()
|
||
}
|
||
}
|
||
|
||
const onCustomerSearchInput = (): void => {
|
||
form.customerUserId = ''
|
||
form.customerName = ''
|
||
form.phone = ''
|
||
newCustomerName.value = customerSearchText.value
|
||
customerDropdownOpen.value = true
|
||
scheduleCustomerSearch()
|
||
}
|
||
|
||
const clearCustomerSelection = (): void => {
|
||
form.customerUserId = ''
|
||
form.customerName = ''
|
||
form.phone = ''
|
||
customerSearchText.value = ''
|
||
newCustomerName.value = ''
|
||
newCustomerPhone.value = ''
|
||
customerOptions.value = []
|
||
customerDropdownOpen.value = true
|
||
}
|
||
|
||
const selectCustomer = (customer: UserInfo): void => {
|
||
form.customerUserId = String(customer.id)
|
||
form.customerName = customer.nickname || customer.account || ''
|
||
form.phone = customer.phone || customer.contactPhone || ''
|
||
customerSearchText.value = [form.customerName, form.phone].filter(Boolean).join(' ')
|
||
newCustomerName.value = form.customerName
|
||
newCustomerPhone.value = form.phone
|
||
customerOptions.value = []
|
||
customerDropdownOpen.value = false
|
||
}
|
||
|
||
const useNewCustomer = (): void => {
|
||
const name = newCustomerName.value.trim()
|
||
const phone = newCustomerPhone.value.trim()
|
||
if (!name || !phone) {
|
||
toast.error('请填写新客户姓名和手机号')
|
||
return
|
||
}
|
||
form.customerUserId = ''
|
||
form.customerName = name
|
||
form.phone = phone
|
||
customerSearchText.value = [name, phone].filter(Boolean).join(' ')
|
||
customerOptions.value = []
|
||
customerDropdownOpen.value = false
|
||
}
|
||
|
||
const onDateChange = async (): Promise<void> => {
|
||
form.appointmentTime = ''
|
||
await refreshAvailableTimes()
|
||
}
|
||
|
||
const selectProduct = (product: ProductCard): void => {
|
||
selectedProduct.value = product
|
||
form.productId = String(product.id)
|
||
form.manualProductTitle = ''
|
||
form.manualProductPrice = ''
|
||
form.manualProductDescription = ''
|
||
form.manualProductTags = ''
|
||
form.manualProductTargetAudience = ''
|
||
form.manualProductPrecautions = ''
|
||
form.manualProductImageFileIds = ''
|
||
form.manualProductImageUrl = ''
|
||
form.durationMinutes = String(Number.parseInt(product.approximateDuration || '', 10) || product.serviceDurationMinutes || form.durationMinutes)
|
||
updateProductModalVisible(false)
|
||
void refreshAvailableTimes()
|
||
}
|
||
|
||
const useManualProduct = (): void => {
|
||
const title = form.manualProductTitle.trim()
|
||
const price = Number(form.manualProductPrice)
|
||
if (!title || Number.isNaN(price) || price < 0) {
|
||
toast.error('请填写手动预约作品名称和价格')
|
||
return
|
||
}
|
||
form.productId = ''
|
||
selectedProduct.value = null
|
||
updateProductModalVisible(false)
|
||
}
|
||
|
||
const uploadManualProductImage = async (event: Event): Promise<void> => {
|
||
const input = event.target as HTMLInputElement
|
||
const file = input.files?.[0]
|
||
if (!file) {
|
||
return
|
||
}
|
||
manualImageUploading.value = true
|
||
try {
|
||
const uploaded = await fileApi.uploadObjectFile(file)
|
||
form.manualProductImageFileIds = String(uploaded.id)
|
||
form.manualProductImageUrl = uploaded.primaryUrl || uploaded.fileUrl || ''
|
||
} finally {
|
||
input.value = ''
|
||
manualImageUploading.value = false
|
||
}
|
||
}
|
||
|
||
const removeManualProductImage = (): void => {
|
||
form.manualProductImageFileIds = ''
|
||
form.manualProductImageUrl = ''
|
||
}
|
||
|
||
const refreshAvailableTimes = async (): Promise<void> => {
|
||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||
if (!form.appointmentDate || !form.artistId || !durationMinutes) {
|
||
availableTimeGroups.value = []
|
||
timeSelectTip.value = '请先选择美甲师并填写时长'
|
||
return
|
||
}
|
||
timeLoading.value = true
|
||
try {
|
||
const calendar = await managerPlanningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate, artistId: form.artistId })
|
||
availableTimeGroups.value = buildAvailableTimeGroups(calendar, form)
|
||
timeSelectTip.value = availableTimeGroups.value.length > 0 ? '请选择预约时间' : '当前美甲师当天暂无可约时间'
|
||
} finally {
|
||
timeLoading.value = false
|
||
}
|
||
}
|
||
|
||
const openTimeSheet = async (): Promise<void> => {
|
||
if (!form.appointmentDate || !form.artistId || !normalizeDuration(form.durationMinutes)) {
|
||
toast.error('请先选择美甲师并填写时长')
|
||
return
|
||
}
|
||
updateTimeSheetVisible(true)
|
||
await refreshAvailableTimes()
|
||
}
|
||
|
||
const selectAvailableTime = (value: string): void => {
|
||
form.appointmentTime = value
|
||
updateTimeSheetVisible(false)
|
||
void checkAvailability()
|
||
}
|
||
|
||
const checkAvailability = async (): Promise<{ ok: boolean; tip?: string }> => {
|
||
if (!form.appointmentDate || !form.appointmentTime || !form.artistId || !normalizeDuration(form.durationMinutes)) {
|
||
conflictTip.value = ''
|
||
return { ok: true }
|
||
}
|
||
const startMinute = minutesOfDay(form.appointmentTime)
|
||
const endMinute = startMinute + normalizeDuration(form.durationMinutes)
|
||
const calendar = await managerPlanningApi.calendar({ startDate: form.appointmentDate, endDate: form.appointmentDate, artistId: form.artistId })
|
||
const daySummary = (calendar.daySummaries || []).find(item => item.date === form.appointmentDate)
|
||
const businessStartTime = String(daySummary?.businessStartTime || calendar.businessStartTime || '10:00').slice(0, 5)
|
||
const businessEndTime = String(daySummary?.businessEndTime || calendar.businessEndTime || '22:00').slice(0, 5)
|
||
if (daySummary?.dayStatus === 'rest') {
|
||
conflictTip.value = '当前日期为店铺休息日'
|
||
conflictLevel.value = 'danger'
|
||
return { ok: false, tip: conflictTip.value }
|
||
}
|
||
if (startMinute < minutesOfDay(businessStartTime) || endMinute > minutesOfDay(businessEndTime)) {
|
||
conflictTip.value = `该时间超出营业时间 ${businessStartTime}-${businessEndTime}`
|
||
conflictLevel.value = 'danger'
|
||
return { ok: false, tip: conflictTip.value }
|
||
}
|
||
conflictTip.value = `当前时段 ${form.appointmentTime}-${timeFromMinutes(endMinute)} 可预约`
|
||
conflictLevel.value = 'free'
|
||
return { ok: true, tip: conflictTip.value }
|
||
}
|
||
|
||
const submit = async (): Promise<void> => {
|
||
const hasManualProduct = form.manualProductTitle.trim() && form.manualProductPrice.trim()
|
||
if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.artistId || !form.appointmentTime) {
|
||
toast.error('请补全预约信息')
|
||
return
|
||
}
|
||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||
if (!durationMinutes) {
|
||
toast.error('请填写预约时长')
|
||
return
|
||
}
|
||
const availability = await checkAvailability()
|
||
if (!availability.ok) {
|
||
toast.error(availability.tip || '该时段已被预约')
|
||
return
|
||
}
|
||
loading.value = true
|
||
try {
|
||
const payload = {
|
||
...form,
|
||
customerUserId: form.customerUserId || undefined,
|
||
productId: form.productId || undefined,
|
||
artistId: form.artistId,
|
||
durationMinutes,
|
||
manualProductPrice: hasManualProduct ? Number(form.manualProductPrice) : undefined,
|
||
clientRequestId: buildClientRequestId('manager-appointment')
|
||
}
|
||
const result = await managerPlanningApi.createAppointment(payload)
|
||
removeDraftForm(session.userInfo)
|
||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
markPlanningRefresh()
|
||
toast.success('预约已创建')
|
||
await router.replace(`/appointments/${result.orderItemId}`)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const saveDraft = (): void => {
|
||
writeDraftForm(session.userInfo, { ...form })
|
||
draftSavedAtText.value = '草稿已保存到本机'
|
||
toast.success('草稿已保存')
|
||
}
|
||
|
||
const clearDraft = (): void => {
|
||
removeDraftForm(session.userInfo)
|
||
removeFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
|
||
draftSavedAtText.value = ''
|
||
toast.success('草稿已清空')
|
||
}
|
||
|
||
onMounted(async () => {
|
||
await loadOptions()
|
||
await searchCustomers()
|
||
await refreshAvailableTimes()
|
||
})
|
||
|
||
watch(form, syncAppointmentDraft, { deep: true })
|
||
watch(productModalVisible, syncAppointmentModalState)
|
||
watch(timeSheetVisible, syncAppointmentModalState)
|
||
</script>
|
||
|
||
<style scoped>
|
||
.draft-tip {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
margin-bottom: 12px;
|
||
padding: 10px 12px;
|
||
color: var(--accent);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.customer-combobox {
|
||
position: relative;
|
||
z-index: 30;
|
||
overflow: visible;
|
||
}
|
||
|
||
.customer-combobox:focus-within {
|
||
z-index: 80;
|
||
}
|
||
|
||
.customer-control {
|
||
position: relative;
|
||
width: 100%;
|
||
}
|
||
|
||
.customer-control input {
|
||
padding-right: 42px;
|
||
}
|
||
|
||
.customer-selected-row {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
width: 100%;
|
||
min-height: 24px;
|
||
padding-right: 42px;
|
||
color: var(--ink);
|
||
font-size: 14px;
|
||
text-align: left;
|
||
}
|
||
|
||
.customer-selected-row span,
|
||
.customer-selected-row strong {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.customer-selected-row span {
|
||
min-width: 0;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.customer-selected-row strong {
|
||
flex: 0 0 auto;
|
||
max-width: 54%;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.customer-clear {
|
||
position: absolute;
|
||
top: 50%;
|
||
right: 8px;
|
||
width: 28px;
|
||
height: 28px;
|
||
transform: translateY(-50%);
|
||
border-radius: 50%;
|
||
background: var(--subtle);
|
||
color: var(--muted);
|
||
font-size: 18px;
|
||
line-height: 1;
|
||
}
|
||
|
||
.customer-dropdown {
|
||
position: absolute;
|
||
z-index: 90;
|
||
top: calc(100% + 8px);
|
||
right: 0;
|
||
left: 0;
|
||
display: grid;
|
||
max-height: min(520px, 70vh);
|
||
gap: 8px;
|
||
overflow-y: auto;
|
||
padding: 10px;
|
||
border: 1px solid var(--border);
|
||
border-radius: 12px;
|
||
background: var(--card-bg);
|
||
box-shadow: 0 16px 36px rgba(19, 32, 24, 0.16);
|
||
isolation: isolate;
|
||
-webkit-overflow-scrolling: touch;
|
||
}
|
||
|
||
.customer-options-scroll {
|
||
display: grid;
|
||
max-height: calc(44px * 5 + 8px * 4);
|
||
gap: 8px;
|
||
overflow-y: auto;
|
||
padding-right: 2px;
|
||
-webkit-overflow-scrolling: touch;
|
||
}
|
||
|
||
.customer-option {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
width: 100%;
|
||
min-height: 44px;
|
||
padding: 9px 10px;
|
||
border-radius: 10px;
|
||
background: var(--subtle);
|
||
text-align: left;
|
||
}
|
||
|
||
.customer-option strong,
|
||
.customer-option span {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.customer-option strong {
|
||
min-width: 0;
|
||
}
|
||
|
||
.customer-option span,
|
||
.customer-muted {
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.customer-option span {
|
||
flex: 0 0 auto;
|
||
max-width: 52%;
|
||
text-align: right;
|
||
}
|
||
|
||
.customer-new {
|
||
display: grid;
|
||
gap: 8px;
|
||
padding-top: 8px;
|
||
border-top: 1px solid var(--border);
|
||
}
|
||
|
||
.picker-value {
|
||
justify-content: flex-start;
|
||
color: var(--ink);
|
||
text-align: left;
|
||
}
|
||
|
||
.conflict-tip {
|
||
margin-top: 12px;
|
||
padding: 10px 12px;
|
||
border-radius: 12px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.conflict-tip--danger {
|
||
background: #fff0ee;
|
||
color: var(--danger);
|
||
}
|
||
|
||
.product-modal,
|
||
.time-sheet {
|
||
display: grid;
|
||
gap: 12px;
|
||
padding: 14px;
|
||
}
|
||
|
||
.product-card {
|
||
display: grid;
|
||
grid-template-columns: 72px 1fr;
|
||
gap: 10px;
|
||
width: 100%;
|
||
padding: 10px;
|
||
border-radius: 14px;
|
||
background: var(--subtle);
|
||
text-align: left;
|
||
}
|
||
|
||
.product-card img {
|
||
width: 72px;
|
||
height: 72px;
|
||
border-radius: 12px;
|
||
object-fit: cover;
|
||
}
|
||
|
||
.product-card strong,
|
||
.product-card span,
|
||
.product-card small {
|
||
display: block;
|
||
}
|
||
|
||
.product-card span,
|
||
.product-card small {
|
||
margin-top: 3px;
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.tag-row {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 5px;
|
||
margin-top: 6px;
|
||
}
|
||
|
||
.tag-row i {
|
||
padding: 2px 6px;
|
||
border-radius: 999px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
font-size: 11px;
|
||
font-style: normal;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.manual-form {
|
||
display: grid;
|
||
gap: 8px;
|
||
}
|
||
|
||
.manual-image-row {
|
||
display: grid;
|
||
grid-template-columns: 72px 1fr auto;
|
||
align-items: center;
|
||
gap: 10px;
|
||
}
|
||
|
||
.manual-image-row img,
|
||
.manual-image-placeholder {
|
||
width: 72px;
|
||
height: 72px;
|
||
border-radius: 12px;
|
||
object-fit: cover;
|
||
}
|
||
|
||
.manual-image-placeholder {
|
||
display: grid;
|
||
place-items: center;
|
||
background: var(--subtle);
|
||
color: var(--muted);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.image-upload {
|
||
position: relative;
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
min-height: 40px;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.image-upload input {
|
||
position: absolute;
|
||
inset: 0;
|
||
opacity: 0;
|
||
}
|
||
|
||
.textarea {
|
||
min-height: 88px;
|
||
padding: 12px;
|
||
resize: vertical;
|
||
}
|
||
|
||
.time-sheet h3 {
|
||
margin: 0 0 8px;
|
||
}
|
||
|
||
.time-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(3, 1fr);
|
||
gap: 8px;
|
||
}
|
||
|
||
.time-grid button {
|
||
display: grid;
|
||
gap: 2px;
|
||
padding: 10px 4px;
|
||
border-radius: 12px;
|
||
background: var(--accent-soft);
|
||
color: var(--accent);
|
||
}
|
||
|
||
.time-grid span {
|
||
font-size: 11px;
|
||
}
|
||
</style>
|