2024-09-12 16:00:53 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { computed, ref } from 'vue';
|
|
|
|
|
|
|
|
import { useVbenModal } from '@vben/common-ui';
|
|
|
|
import { $t } from '@vben/locales';
|
2024-09-25 14:46:02 +08:00
|
|
|
import { cloneDeep } from '@vben/utils';
|
2024-09-12 16:00:53 +08:00
|
|
|
|
|
|
|
import { useVbenForm } from '#/adapter';
|
2024-09-23 08:46:22 +08:00
|
|
|
import { noticeAdd, noticeInfo, noticeUpdate } from '#/api/system/notice';
|
2024-09-12 16:00:53 +08:00
|
|
|
import { Tinymce } from '#/components/tinymce';
|
|
|
|
|
|
|
|
import { modalSchema } from './data';
|
|
|
|
|
|
|
|
const emit = defineEmits<{ reload: [] }>();
|
|
|
|
|
|
|
|
const isUpdate = ref(false);
|
|
|
|
const title = computed(() => {
|
|
|
|
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
|
|
|
});
|
|
|
|
|
|
|
|
const [BasicForm, formApi] = useVbenForm({
|
|
|
|
layout: 'vertical',
|
|
|
|
schema: modalSchema(),
|
|
|
|
showDefaultActions: false,
|
|
|
|
wrapperClass: 'grid-cols-2',
|
|
|
|
});
|
|
|
|
|
|
|
|
const [BasicModal, modalApi] = useVbenModal({
|
|
|
|
fullscreenButton: false,
|
|
|
|
onCancel: handleCancel,
|
|
|
|
onConfirm: handleConfirm,
|
|
|
|
onOpenChange: async (isOpen) => {
|
|
|
|
if (!isOpen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
modalApi.modalLoading(true);
|
2024-09-25 14:46:02 +08:00
|
|
|
const { id } = modalApi.getData() as { id?: number | string };
|
|
|
|
isUpdate.value = !!id;
|
|
|
|
if (isUpdate.value && id) {
|
2024-09-23 08:46:22 +08:00
|
|
|
const record = await noticeInfo(id);
|
2024-09-25 14:46:02 +08:00
|
|
|
await formApi.setValues(record);
|
2024-09-12 16:00:53 +08:00
|
|
|
}
|
|
|
|
modalApi.modalLoading(false);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
async function handleConfirm() {
|
|
|
|
try {
|
|
|
|
modalApi.modalLoading(true);
|
|
|
|
const { valid } = await formApi.validate();
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-25 14:46:02 +08:00
|
|
|
const data = cloneDeep(await formApi.getValues());
|
2024-09-12 16:00:53 +08:00
|
|
|
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
|
|
|
emit('reload');
|
|
|
|
await handleCancel();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
modalApi.modalLoading(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleCancel() {
|
|
|
|
modalApi.close();
|
|
|
|
await formApi.resetForm();
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<BasicModal :fullscreen-button="true" :title="title" class="w-[800px]">
|
|
|
|
<BasicForm>
|
|
|
|
<template #noticeContent="slotProps">
|
|
|
|
<Tinymce v-bind="slotProps" width="100%" />
|
|
|
|
</template>
|
|
|
|
</BasicForm>
|
|
|
|
</BasicModal>
|
|
|
|
</template>
|