|
|
@@ -0,0 +1,115 @@
|
|
|
+<script lang="ts" setup>
|
|
|
+import { computed, reactive, ref, unref } from 'vue';
|
|
|
+
|
|
|
+import { alert, useVbenModal } from '@vben/common-ui';
|
|
|
+
|
|
|
+import { Button, Input } from 'ant-design-vue';
|
|
|
+
|
|
|
+import { useFormOptions, useVbenForm } from '#/adapter';
|
|
|
+import { SpecApi } from '#/api';
|
|
|
+import { Icon } from '#/components/icon';
|
|
|
+
|
|
|
+import { useSchema } from '../data.config';
|
|
|
+
|
|
|
+defineOptions({
|
|
|
+ name: 'SpecEdit',
|
|
|
+});
|
|
|
+const emit = defineEmits(['success']);
|
|
|
+const modelRef = ref<Record<string, any>>({});
|
|
|
+const isUpdate = ref(true);
|
|
|
+const state = reactive<{ specDetails: any[] }>({
|
|
|
+ specDetails: [],
|
|
|
+});
|
|
|
+
|
|
|
+const [Form, { validate, setValues, getValues }] = useVbenForm(
|
|
|
+ useFormOptions({
|
|
|
+ schema: useSchema(),
|
|
|
+ }),
|
|
|
+);
|
|
|
+
|
|
|
+const [Modal, { close, setState, getData, lock, unlock }] = useVbenModal({
|
|
|
+ fullscreenButton: false,
|
|
|
+ draggable: true,
|
|
|
+ closeOnClickModal: false,
|
|
|
+ onCancel() {
|
|
|
+ close();
|
|
|
+ },
|
|
|
+ onConfirm: async () => {
|
|
|
+ try {
|
|
|
+ const { valid } = await validate();
|
|
|
+ if (!valid) return;
|
|
|
+ const values = await getValues();
|
|
|
+ lock();
|
|
|
+ const postParams = unref(modelRef);
|
|
|
+ Object.assign(postParams, values);
|
|
|
+ postParams.specDetails = state.specDetails;
|
|
|
+ await (unref(isUpdate)
|
|
|
+ ? SpecApi.editDetail(postParams as SpecApi.RecordItem)
|
|
|
+ : SpecApi.addDetail(postParams as SpecApi.BasicRecordItem));
|
|
|
+ alert('操作成功');
|
|
|
+
|
|
|
+ emit('success');
|
|
|
+ close();
|
|
|
+ } finally {
|
|
|
+ unlock();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ onOpenChange: async (isOpen: boolean) => {
|
|
|
+ if (isOpen) {
|
|
|
+ setState({ loading: true });
|
|
|
+ const data = getData<Record<string, any>>();
|
|
|
+ isUpdate.value = !!data.isUpdate;
|
|
|
+ modelRef.value = { ...data.baseData };
|
|
|
+
|
|
|
+ if (unref(isUpdate)) {
|
|
|
+ const entity = await SpecApi.getDetail(data.baseData.id);
|
|
|
+ state.specDetails = entity.specDetails;
|
|
|
+ modelRef.value = { ...entity };
|
|
|
+ setValues(entity);
|
|
|
+ } else {
|
|
|
+ handleAdd();
|
|
|
+ }
|
|
|
+ setState({ loading: false });
|
|
|
+ }
|
|
|
+ },
|
|
|
+});
|
|
|
+
|
|
|
+const getTitle = computed(() => (unref(isUpdate) ? '编辑规格' : '新增规格'));
|
|
|
+
|
|
|
+const handleAdd = () => {
|
|
|
+ state.specDetails.push({ name: '' });
|
|
|
+};
|
|
|
+
|
|
|
+const handleDelete = (index: number) => {
|
|
|
+ state.specDetails.splice(index, 1);
|
|
|
+};
|
|
|
+</script>
|
|
|
+<template>
|
|
|
+ <Modal class="w-[1000px]" :title="getTitle">
|
|
|
+ <Form>
|
|
|
+ <template #specDetails>
|
|
|
+ <div class="w-full">
|
|
|
+ <div
|
|
|
+ v-for="(item, index) in state.specDetails"
|
|
|
+ :key="index"
|
|
|
+ class="mb-4 flex w-full items-center last:mb-0"
|
|
|
+ >
|
|
|
+ <Input.Group compact class="!flex">
|
|
|
+ <Input v-model:value="item.name" />
|
|
|
+ <Button type="primary" v-if="index === 0" @click="handleAdd">
|
|
|
+ <template #icon>
|
|
|
+ <Icon icon="material-symbols:add-rounded" />
|
|
|
+ </template>
|
|
|
+ </Button>
|
|
|
+ <Button type="primary" danger v-else @click="handleDelete(index)">
|
|
|
+ <template #icon>
|
|
|
+ <Icon icon="material-symbols:delete-outline" />
|
|
|
+ </template>
|
|
|
+ </Button>
|
|
|
+ </Input.Group>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </template>
|
|
|
+ </Form>
|
|
|
+ </Modal>
|
|
|
+</template>
|