Files
baby-tracker/apps/web/src/components/timeline/TimelineManualModal.vue
T
2026-09-26 03:58:48 +08:00

393 lines
14 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { reactive, watch } from 'vue'
import { showToast } from 'vant'
import { useCareStore } from '../../stores/careStore'
import type { ExcretionType, FeedingMethod, RecordType } from '../../types/domain'
import { getLocalDateStr, getLocalTimeStr } from '../../utils/format'
const props = defineProps<{
show: boolean
}>()
const emit = defineEmits<{
(e: 'update:show', value: boolean): void
(e: 'success'): void
}>()
const store = useCareStore()
const quickNotes = ['拍嗝顺利', '溢奶微量', '金黄色便便', '安睡无闹', '微胀气', '吃得香']
const initialNow = store.getServerNow()
const manualForm = reactive({
recordType: 'feeding' as RecordType,
startDate: getLocalDateStr(initialNow),
startTimeStr: getLocalTimeStr(initialNow),
hasEndTime: false,
endDate: getLocalDateStr(initialNow),
endTimeStr: '',
durationMinutes: null as number | null,
feedingMethod: 'breastfeed' as FeedingMethod,
amountMl: null as number | null,
breastMode: 'both' as 'left' | 'right' | 'both',
leftMinutes: 10 as number | null,
rightMinutes: 10 as number | null,
excretionType: 'pee' as ExcretionType,
operatorName: store.operatorName,
note: ''
})
watch(() => props.show, (newVal) => {
if (newVal) {
initForm()
}
})
function initForm() {
const now = store.getServerNow()
manualForm.recordType = 'feeding'
manualForm.startDate = getLocalDateStr(now)
manualForm.startTimeStr = getLocalTimeStr(now)
manualForm.hasEndTime = true
manualForm.endDate = getLocalDateStr(now)
manualForm.durationMinutes = 20
manualForm.feedingMethod = store.preferences.defaultFeedingMethod || 'breastfeed'
manualForm.amountMl = manualForm.feedingMethod === 'bottle' ? (store.preferences.defaultBottleAmount || 30) : null
manualForm.breastMode = 'both'
manualForm.leftMinutes = 10
manualForm.rightMinutes = 10
manualForm.excretionType = 'pee'
manualForm.operatorName = store.operatorName
manualForm.note = ''
autoUpdateEndTime()
}
function handleBreastModeChange(mode: 'left' | 'right' | 'both') {
manualForm.breastMode = mode
const currentTotal = manualForm.durationMinutes || 20
if (mode === 'both') {
manualForm.leftMinutes = Math.floor(currentTotal / 2) || 10
manualForm.rightMinutes = Math.ceil(currentTotal / 2) || 10
manualForm.durationMinutes = (manualForm.leftMinutes || 0) + (manualForm.rightMinutes || 0)
} else if (mode === 'left') {
manualForm.leftMinutes = currentTotal || 20
manualForm.rightMinutes = null
manualForm.durationMinutes = manualForm.leftMinutes
} else {
manualForm.rightMinutes = currentTotal || 20
manualForm.leftMinutes = null
manualForm.durationMinutes = manualForm.rightMinutes
}
autoUpdateEndTime()
}
function onSplitMinutesChange() {
if (manualForm.breastMode === 'both') {
manualForm.durationMinutes = (manualForm.leftMinutes || 0) + (manualForm.rightMinutes || 0)
} else if (manualForm.breastMode === 'left') {
manualForm.durationMinutes = manualForm.leftMinutes || 0
} else {
manualForm.durationMinutes = manualForm.rightMinutes || 0
}
autoUpdateEndTime()
}
function onTotalDurationChange() {
const total = manualForm.durationMinutes || 0
if (manualForm.breastMode === 'both') {
manualForm.leftMinutes = Math.floor(total / 2)
manualForm.rightMinutes = Math.ceil(total / 2)
} else if (manualForm.breastMode === 'left') {
manualForm.leftMinutes = total
manualForm.rightMinutes = null
} else {
manualForm.rightMinutes = total
manualForm.leftMinutes = null
}
autoUpdateEndTime()
}
function autoUpdateEndTime() {
if (!manualForm.startDate || !manualForm.startTimeStr || !manualForm.durationMinutes) return
try {
const startMs = new Date(`${manualForm.startDate}T${manualForm.startTimeStr}:00`).getTime()
if (isNaN(startMs)) return
const endMs = startMs + manualForm.durationMinutes * 60000
const endDateObj = new Date(endMs)
const y = endDateObj.getFullYear()
const m = String(endDateObj.getMonth() + 1).padStart(2, '0')
const d = String(endDateObj.getDate()).padStart(2, '0')
const hh = String(endDateObj.getHours()).padStart(2, '0')
const mm = String(endDateObj.getMinutes()).padStart(2, '0')
manualForm.endDate = `${y}-${m}-${d}`
manualForm.endTimeStr = `${hh}:${mm}`
manualForm.hasEndTime = true
} catch {
// ignore
}
}
function appendNote(tag: string) {
manualForm.note = manualForm.note ? `${manualForm.note},${tag}` : tag
}
async function submitManual() {
if (!manualForm.startDate || !manualForm.startTimeStr) {
showToast('请选择开始时间')
return
}
const startTime = `${manualForm.startDate}T${manualForm.startTimeStr}:00`
let endTime: string | null = null
if (manualForm.hasEndTime && manualForm.endDate && manualForm.endTimeStr) {
endTime = `${manualForm.endDate}T${manualForm.endTimeStr}:00`
}
const finalNote = manualForm.note.trim()
try {
await store.manualLog({
recordType: manualForm.recordType,
startTime,
endTime,
durationMinutes: manualForm.durationMinutes,
leftDurationMinutes: manualForm.recordType === 'feeding' && manualForm.feedingMethod === 'breastfeed' ? (manualForm.breastMode === 'right' ? null : manualForm.leftMinutes) : null,
rightDurationMinutes: manualForm.recordType === 'feeding' && manualForm.feedingMethod === 'breastfeed' ? (manualForm.breastMode === 'left' ? null : manualForm.rightMinutes) : null,
feedingMethod: manualForm.recordType === 'feeding' ? manualForm.feedingMethod : null,
amountMl: manualForm.recordType === 'feeding' && manualForm.feedingMethod !== 'breastfeed' ? manualForm.amountMl : null,
excretionType: manualForm.recordType === 'excretion' ? manualForm.excretionType : null,
operatorName: manualForm.operatorName || store.operatorName,
note: finalNote
})
emit('update:show', false)
emit('success')
showToast({ message: '补录成功', type: 'success' })
} catch (err: any) {
showToast(err.message || '补录失败')
}
}
</script>
<template>
<van-popup
:show="props.show"
position="bottom"
round
safe-area-inset-bottom
teleport="body"
class="timeline-modal-popup"
@update:show="(val: boolean) => emit('update:show', val)"
>
<div class="modal-sheet-inner">
<div class="modal-header">
<h3>补录照护记录</h3>
<button class="close-btn" type="button" @click="emit('update:show', false)">×</button>
</div>
<div class="modal-form">
<!-- 记录类型选择 -->
<label>
记录类型
<div class="chip-group">
<button
type="button"
class="chip"
:class="{ active: manualForm.recordType === 'feeding' }"
@click="manualForm.recordType = 'feeding'"
>
🍼 喂奶
</button>
<button
type="button"
class="chip"
:class="{ active: manualForm.recordType === 'sleep' }"
@click="manualForm.recordType = 'sleep'"
>
🌙 睡眠
</button>
<button
type="button"
class="chip"
:class="{ active: manualForm.recordType === 'excretion' }"
@click="manualForm.recordType = 'excretion'"
>
🧷 尿布排泄
</button>
</div>
</label>
<!-- 开始起止时间设置 -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
<label>
开始日期
<input v-model="manualForm.startDate" type="date" @change="autoUpdateEndTime" />
</label>
<label>
开始时间
<input v-model="manualForm.startTimeStr" type="time" @change="autoUpdateEndTime" />
</label>
</div>
<!-- 喂奶专属表单 -->
<template v-if="manualForm.recordType === 'feeding'">
<label>
喂养方式
<select v-model="manualForm.feedingMethod">
<option value="breastfeed">亲喂 (母乳)</option>
<option value="bottle">瓶喂 (母乳)</option>
<option value="formula">配方奶</option>
<option value="mixed">混合喂养</option>
</select>
</label>
<!-- 亲喂:侧别与分侧耗时 -->
<div v-if="manualForm.feedingMethod === 'breastfeed'" style="background: #faf7f2; border: 1px solid #eee5d8; border-radius: 12px; padding: 12px;">
<label style="margin-bottom: 8px;">
亲喂模式
<div class="chip-group">
<button
type="button"
class="chip sm"
:class="{ active: manualForm.breastMode === 'both' }"
@click="handleBreastModeChange('both')"
>
双侧 (左右各喂)
</button>
<button
type="button"
class="chip sm"
:class="{ active: manualForm.breastMode === 'left' }"
@click="handleBreastModeChange('left')"
>
仅左侧
</button>
<button
type="button"
class="chip sm"
:class="{ active: manualForm.breastMode === 'right' }"
@click="handleBreastModeChange('right')"
>
仅右侧
</button>
</div>
</label>
<!-- 分侧耗时 -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px;">
<label v-if="manualForm.breastMode === 'left' || manualForm.breastMode === 'both'">
左侧耗时 (分钟)
<input v-model.number="manualForm.leftMinutes" type="number" min="0" max="120" placeholder="分钟" @input="onSplitMinutesChange" />
</label>
<label v-if="manualForm.breastMode === 'right' || manualForm.breastMode === 'both'">
右侧耗时 (分钟)
<input v-model.number="manualForm.rightMinutes" type="number" min="0" max="120" placeholder="分钟" @input="onSplitMinutesChange" />
</label>
</div>
<!-- 总时长 -->
<label style="margin-top: 8px;">
亲喂总时长 (分钟)
<input v-model.number="manualForm.durationMinutes" type="number" min="1" max="180" @input="onTotalDurationChange" />
</label>
</div>
<label v-else>
单次奶量 (ml)
<input v-model.number="manualForm.amountMl" type="number" min="5" max="400" step="5" />
</label>
</template>
<!-- 睡眠专属表单 -->
<template v-if="manualForm.recordType === 'sleep'">
<label>
睡眠时长 (分钟)
<input v-model.number="manualForm.durationMinutes" type="number" min="1" placeholder="留空则按起止时间自动计算" @input="autoUpdateEndTime" />
</label>
</template>
<!-- 排泄专属表单 -->
<template v-if="manualForm.recordType === 'excretion'">
<label>
排泄类型
<div class="chip-group">
<button
type="button"
class="chip"
:class="{ active: manualForm.excretionType === 'pee' }"
@click="manualForm.excretionType = 'pee'"
>
💧 小便 (尿尿)
</button>
<button
type="button"
class="chip"
:class="{ active: manualForm.excretionType === 'poop' }"
@click="manualForm.excretionType = 'poop'"
>
💩 大便 (便便)
</button>
</div>
</label>
</template>
<!-- 结束时间(选填/联动) -->
<div style="border-top: 1px dashed #e8e3d8; padding-top: 10px;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;">
<span style="font-size: 13px; font-weight: 700; color: #4b5563;">记录结束时间</span>
<input v-model="manualForm.hasEndTime" type="checkbox" style="width: 18px; height: 18px;" />
</div>
<div v-if="manualForm.hasEndTime" style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
<label>
结束日期
<input v-model="manualForm.endDate" type="date" />
</label>
<label>
结束时间
<input v-model="manualForm.endTimeStr" type="time" />
</label>
</div>
</div>
<!-- 记录人 -->
<label>
记录人称谓
<input v-model="manualForm.operatorName" placeholder="例如:宝妈、宝爸" />
<div class="chip-group">
<button
v-for="m in store.members"
:key="m.memberId"
type="button"
class="chip sm"
:class="{ active: manualForm.operatorName === m.roleName }"
@click="manualForm.operatorName = m.roleName"
>
{{ m.roleName }}
</button>
</div>
</label>
<!-- 备注与快捷标签 -->
<label>
备注说明
<input v-model="manualForm.note" placeholder="例如:拍嗝顺利、状态好" />
<div class="chip-group">
<button
v-for="tag in quickNotes"
:key="tag"
type="button"
class="chip sm"
@click="appendNote(tag)"
>
+ {{ tag }}
</button>
</div>
</label>
<div class="modal-actions">
<button class="secondary-button" type="button" @click="emit('update:show', false)">取消</button>
<button class="save-button" type="button" @click="submitManual">确定补录</button>
</div>
</div>
</div>
</van-popup>
</template>