2026-06-22 11:58:53 +08:00
|
|
|
import { watch } from 'vue'
|
|
|
|
|
|
2026-06-22 15:56:06 +08:00
|
|
|
function isApplicationPreviewDateField(fieldKey = '') {
|
|
|
|
|
return ['time', 'time_return'].includes(String(fieldKey || '').trim())
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 11:58:53 +08:00
|
|
|
export function useTravelReimbursementApplicationPreviewDateEditor({
|
|
|
|
|
applicationPreviewEditor,
|
|
|
|
|
cancelApplicationPreviewEditor,
|
|
|
|
|
commitApplicationPreviewDateEditor,
|
|
|
|
|
composerDateMode,
|
|
|
|
|
composerDatePickerOpen,
|
|
|
|
|
composerRangeEndDate,
|
|
|
|
|
composerRangeStartDate,
|
|
|
|
|
composerSingleDate,
|
|
|
|
|
formatDateInputValue,
|
|
|
|
|
isApplicationPreviewEditing,
|
|
|
|
|
messages,
|
|
|
|
|
openApplicationPreviewEditor,
|
|
|
|
|
travelCalculatorOpen
|
|
|
|
|
}) {
|
|
|
|
|
function applyLinkedApplicationPreviewDateSelection(selection) {
|
|
|
|
|
const editor = applicationPreviewEditor.value
|
2026-06-22 15:56:06 +08:00
|
|
|
if (!isApplicationPreviewDateField(editor.fieldKey) || !editor.messageId) {
|
2026-06-22 11:58:53 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const targetMessage = messages.value.find((item) =>
|
|
|
|
|
String(item.id || '') === String(editor.messageId || '')
|
|
|
|
|
)
|
|
|
|
|
if (!targetMessage?.applicationPreview) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applicationPreviewEditor.value = {
|
|
|
|
|
...editor,
|
|
|
|
|
dateMode: selection.mode === 'range' ? 'range' : 'single',
|
|
|
|
|
singleDate: selection.startDate,
|
|
|
|
|
rangeStartDate: selection.startDate,
|
|
|
|
|
rangeEndDate: selection.endDate || selection.startDate
|
|
|
|
|
}
|
|
|
|
|
return commitApplicationPreviewDateEditor(targetMessage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncComposerDateFromApplicationEditor() {
|
|
|
|
|
const editor = applicationPreviewEditor.value
|
|
|
|
|
const today = formatDateInputValue()
|
|
|
|
|
composerDateMode.value = editor.dateMode === 'range' ? 'range' : 'single'
|
|
|
|
|
composerSingleDate.value = editor.singleDate || today
|
|
|
|
|
composerRangeStartDate.value = editor.rangeStartDate || composerSingleDate.value || today
|
|
|
|
|
composerRangeEndDate.value = editor.rangeEndDate || composerRangeStartDate.value || today
|
|
|
|
|
composerDatePickerOpen.value = true
|
|
|
|
|
travelCalculatorOpen.value = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openApplicationPreviewEditorFromUi(message, fieldKey, value) {
|
|
|
|
|
openApplicationPreviewEditor(message, fieldKey, value)
|
2026-06-22 15:56:06 +08:00
|
|
|
if (isApplicationPreviewDateField(fieldKey) && isApplicationPreviewEditing(message, fieldKey)) {
|
2026-06-22 11:58:53 +08:00
|
|
|
syncComposerDateFromApplicationEditor()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(composerDatePickerOpen, (open, previousOpen) => {
|
2026-06-22 15:56:06 +08:00
|
|
|
if (!open && previousOpen && isApplicationPreviewDateField(applicationPreviewEditor.value.fieldKey)) {
|
2026-06-22 11:58:53 +08:00
|
|
|
cancelApplicationPreviewEditor()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
applyLinkedApplicationPreviewDateSelection,
|
|
|
|
|
openApplicationPreviewEditorFromUi
|
|
|
|
|
}
|
|
|
|
|
}
|