2025-12-25 11:20:56 +08:00
|
|
|
|
<template>
|
2026-01-01 14:43:43 +08:00
|
|
|
|
<PopupContainer
|
|
|
|
|
|
v-model="visible"
|
|
|
|
|
|
title="交易详情"
|
|
|
|
|
|
height="85%"
|
|
|
|
|
|
:closeable="false"
|
2025-12-25 11:20:56 +08:00
|
|
|
|
>
|
2026-01-01 14:43:43 +08:00
|
|
|
|
<template #header-actions>
|
|
|
|
|
|
<van-button size="small" type="primary" plain @click="handleOffsetClick">抵账</van-button>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2026-01-06 13:45:39 +08:00
|
|
|
|
<van-form style="margin-top: 12px;">
|
2025-12-25 11:20:56 +08:00
|
|
|
|
<van-cell-group inset>
|
|
|
|
|
|
<van-cell title="交易时间" :value="formatDate(transaction.occurredAt)" />
|
|
|
|
|
|
<van-cell title="记录时间" :value="formatDate(transaction.createTime)" />
|
|
|
|
|
|
</van-cell-group>
|
|
|
|
|
|
|
|
|
|
|
|
<van-cell-group inset title="交易明细">
|
|
|
|
|
|
<van-field
|
|
|
|
|
|
v-model="editForm.reason"
|
|
|
|
|
|
name="reason"
|
|
|
|
|
|
label="交易摘要"
|
|
|
|
|
|
placeholder="请输入交易摘要"
|
|
|
|
|
|
type="textarea"
|
|
|
|
|
|
rows="2"
|
|
|
|
|
|
autosize
|
|
|
|
|
|
maxlength="200"
|
|
|
|
|
|
show-word-limit
|
|
|
|
|
|
/>
|
|
|
|
|
|
<van-field
|
|
|
|
|
|
v-model="editForm.amount"
|
|
|
|
|
|
name="amount"
|
|
|
|
|
|
label="交易金额"
|
|
|
|
|
|
placeholder="请输入交易金额"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
:rules="[{ required: true, message: '请输入交易金额' }]"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<van-field
|
|
|
|
|
|
v-model="editForm.balance"
|
|
|
|
|
|
name="balance"
|
|
|
|
|
|
label="交易后余额"
|
|
|
|
|
|
placeholder="请输入交易后余额"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
:rules="[{ required: true, message: '请输入交易后余额' }]"
|
|
|
|
|
|
/>
|
2026-01-03 11:07:33 +08:00
|
|
|
|
|
|
|
|
|
|
<van-field name="type" label="交易类型">
|
|
|
|
|
|
<template #input>
|
|
|
|
|
|
<van-radio-group v-model="editForm.type" direction="horizontal">
|
|
|
|
|
|
<van-radio :name="0">支出</van-radio>
|
|
|
|
|
|
<van-radio :name="1">收入</van-radio>
|
|
|
|
|
|
<van-radio :name="2">不计</van-radio>
|
|
|
|
|
|
</van-radio-group>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</van-field>
|
|
|
|
|
|
|
2025-12-26 15:21:31 +08:00
|
|
|
|
<van-field name="classify" label="交易分类">
|
|
|
|
|
|
<template #input>
|
|
|
|
|
|
<span v-if="!editForm.classify" style="color: #c8c9cc;">请选择交易分类</span>
|
|
|
|
|
|
<span v-else>{{ editForm.classify }}</span>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</van-field>
|
|
|
|
|
|
|
2026-01-08 14:41:50 +08:00
|
|
|
|
<!-- 分类选择组件 -->
|
|
|
|
|
|
<ClassifySelector
|
|
|
|
|
|
v-model="editForm.classify"
|
|
|
|
|
|
:type="editForm.type"
|
|
|
|
|
|
@change="handleClassifyChange"
|
|
|
|
|
|
/>
|
2025-12-25 11:20:56 +08:00
|
|
|
|
</van-cell-group>
|
2026-01-06 13:45:39 +08:00
|
|
|
|
</van-form>
|
2025-12-25 11:20:56 +08:00
|
|
|
|
|
2026-01-06 13:45:39 +08:00
|
|
|
|
<template #footer>
|
|
|
|
|
|
<van-button
|
|
|
|
|
|
round
|
|
|
|
|
|
block
|
|
|
|
|
|
type="primary"
|
|
|
|
|
|
:loading="submitting"
|
|
|
|
|
|
@click="onSubmit"
|
|
|
|
|
|
>
|
|
|
|
|
|
保存修改
|
|
|
|
|
|
</van-button>
|
|
|
|
|
|
</template>
|
2026-01-01 14:43:43 +08:00
|
|
|
|
</PopupContainer>
|
2025-12-25 11:20:56 +08:00
|
|
|
|
|
2026-01-01 14:43:43 +08:00
|
|
|
|
<!-- 抵账候选列表弹窗 -->
|
|
|
|
|
|
<PopupContainer
|
|
|
|
|
|
v-model="showOffsetPopup"
|
|
|
|
|
|
title="选择抵账交易"
|
|
|
|
|
|
height="70%"
|
|
|
|
|
|
>
|
|
|
|
|
|
<van-list>
|
|
|
|
|
|
<van-cell
|
|
|
|
|
|
v-for="item in offsetCandidates"
|
|
|
|
|
|
:key="item.id"
|
|
|
|
|
|
:title="item.reason"
|
|
|
|
|
|
:label="formatDate(item.occurredAt)"
|
|
|
|
|
|
:value="item.amount"
|
|
|
|
|
|
is-link
|
|
|
|
|
|
@click="handleCandidateSelect(item)"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<van-empty v-if="offsetCandidates.length === 0" description="暂无匹配的抵账交易" />
|
|
|
|
|
|
</van-list>
|
|
|
|
|
|
</PopupContainer>
|
2025-12-25 11:20:56 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { ref, reactive, watch, defineProps, defineEmits } from 'vue'
|
2026-01-01 14:43:43 +08:00
|
|
|
|
import { showToast, showConfirmDialog } from 'vant'
|
|
|
|
|
|
import PopupContainer from '@/components/PopupContainer.vue'
|
2026-01-08 14:41:50 +08:00
|
|
|
|
import ClassifySelector from '@/components/ClassifySelector.vue'
|
2026-01-01 14:43:43 +08:00
|
|
|
|
import { updateTransaction, getCandidatesForOffset, offsetTransactions } from '@/api/transactionRecord'
|
2025-12-25 11:20:56 +08:00
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
|
show: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: false
|
|
|
|
|
|
},
|
|
|
|
|
|
transaction: {
|
|
|
|
|
|
type: Object,
|
|
|
|
|
|
default: null
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits(['update:show', 'save'])
|
|
|
|
|
|
|
|
|
|
|
|
const visible = ref(false)
|
|
|
|
|
|
const submitting = ref(false)
|
|
|
|
|
|
|
|
|
|
|
|
// 编辑表单
|
|
|
|
|
|
const editForm = reactive({
|
|
|
|
|
|
id: 0,
|
|
|
|
|
|
reason: '',
|
|
|
|
|
|
amount: '',
|
|
|
|
|
|
balance: '',
|
|
|
|
|
|
type: 0,
|
2025-12-26 15:21:31 +08:00
|
|
|
|
classify: ''
|
2025-12-25 11:20:56 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 监听props变化
|
|
|
|
|
|
watch(() => props.show, (newVal) => {
|
|
|
|
|
|
visible.value = newVal
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.transaction, (newVal) => {
|
|
|
|
|
|
if (newVal) {
|
|
|
|
|
|
// 填充编辑表单
|
|
|
|
|
|
editForm.id = newVal.id
|
|
|
|
|
|
editForm.reason = newVal.reason || ''
|
|
|
|
|
|
editForm.amount = String(newVal.amount)
|
|
|
|
|
|
editForm.balance = String(newVal.balance)
|
|
|
|
|
|
editForm.type = newVal.type
|
|
|
|
|
|
editForm.classify = newVal.classify || ''
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watch(visible, (newVal) => {
|
|
|
|
|
|
emit('update:show', newVal)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 监听交易类型变化,重新加载分类
|
|
|
|
|
|
watch(() => editForm.type, (newVal) => {
|
2025-12-26 15:21:31 +08:00
|
|
|
|
// 清空已选的分类
|
2025-12-25 11:20:56 +08:00
|
|
|
|
editForm.classify = ''
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 提交编辑
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
submitting.value = true
|
|
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
|
id: editForm.id,
|
|
|
|
|
|
reason: editForm.reason,
|
|
|
|
|
|
amount: parseFloat(editForm.amount),
|
|
|
|
|
|
balance: parseFloat(editForm.balance),
|
|
|
|
|
|
type: editForm.type,
|
2025-12-26 15:21:31 +08:00
|
|
|
|
classify: editForm.classify
|
2025-12-25 11:20:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const response = await updateTransaction(data)
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
|
showToast('保存成功')
|
|
|
|
|
|
visible.value = false
|
2025-12-29 20:51:20 +08:00
|
|
|
|
emit('save', data)
|
2025-12-25 11:20:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
showToast(response.message || '保存失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('保存出错:', error)
|
|
|
|
|
|
showToast('保存失败')
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
submitting.value = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 14:41:50 +08:00
|
|
|
|
// 分类选择变化
|
|
|
|
|
|
const handleClassifyChange = () => {
|
|
|
|
|
|
if (editForm.id > 0 && editForm.type >= 0) {
|
2026-01-04 16:52:20 +08:00
|
|
|
|
// 直接保存
|
|
|
|
|
|
onSubmit()
|
|
|
|
|
|
}
|
2025-12-25 11:20:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空分类
|
|
|
|
|
|
const formatDate = (dateString) => {
|
|
|
|
|
|
if (!dateString) return ''
|
|
|
|
|
|
const date = new Date(dateString)
|
|
|
|
|
|
return date.toLocaleString('zh-CN', {
|
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
|
minute: '2-digit'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-01-01 14:43:43 +08:00
|
|
|
|
|
|
|
|
|
|
// 抵账相关
|
|
|
|
|
|
const showOffsetPopup = ref(false)
|
|
|
|
|
|
const offsetCandidates = ref([])
|
|
|
|
|
|
|
|
|
|
|
|
const handleOffsetClick = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await getCandidatesForOffset(editForm.id)
|
|
|
|
|
|
if (res.success) {
|
|
|
|
|
|
offsetCandidates.value = res.data || []
|
|
|
|
|
|
showOffsetPopup.value = true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showToast(res.message || '获取抵账列表失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取抵账列表出错:', error)
|
|
|
|
|
|
showToast('获取抵账列表失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleCandidateSelect = (candidate) => {
|
|
|
|
|
|
showConfirmDialog({
|
|
|
|
|
|
title: '确认抵账',
|
|
|
|
|
|
message: `确认将当前交易与 "${candidate.reason}" (${candidate.amount}) 互相抵消吗?\n抵消后两笔交易将被删除。`,
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await offsetTransactions(editForm.id, candidate.id)
|
|
|
|
|
|
if (res.success) {
|
|
|
|
|
|
showToast('抵账成功')
|
|
|
|
|
|
showOffsetPopup.value = false
|
|
|
|
|
|
visible.value = false
|
|
|
|
|
|
emit('save') // 触发列表刷新
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showToast(res.message || '抵账失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('抵账出错:', error)
|
|
|
|
|
|
showToast('抵账失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
// on cancel
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-12-25 11:20:56 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
</style>
|