iot-fontend/src/views/system/dept/components/dept-edit.vue

128 lines
3.2 KiB
Vue
Raw Normal View History

2024-11-12 15:44:05 +08:00
<template>
<a-button
v-permission="['system:dept:create']"
v-if="props.isCreate"
type="primary"
@click="handleClick"
>
2024-11-12 15:44:05 +08:00
<template #icon><icon-plus /></template>
新建
</a-button>
<a-button
v-permission="['system:dept:update']"
2024-11-12 15:44:05 +08:00
v-if="!props.isCreate"
type="outline"
size="small"
:style="{ marginRight: '10px', padding: '7px' }"
@click="handleClick"
>
<template #icon><icon-edit /></template>
修改
</a-button>
<a-modal
width="700px"
:visible="visible"
@ok="handleSubmit"
@cancel="handleCancel"
>
<template #title>{{ modalTitle }}</template>
<a-form ref="createEditRef" :model="formData" :style="{ width: '650px' }">
<a-form-item
field="name"
:label="$t('dept.info.name')"
:validate-trigger="['change', 'input']"
:rules="[{ required: true, message: t('dept.info.name.required') }]"
>
<a-input
v-model="formData.name"
:placeholder="$t('dept.info.name.placeholder')"
/>
</a-form-item>
<a-form-item field="remark" :label="$t('dept.info.remark')">
<a-textarea
v-model="formData.remark"
:placeholder="$t('dept.info.remark.placeholder')"
style="height: 100px"
/>
</a-form-item>
</a-form>
</a-modal>
</template>
<script lang="ts" setup>
import { useI18n } from 'vue-i18n';
import useVisible from '@/hooks/visible';
import { computed, PropType, ref } from 'vue';
import { FormInstance } from '@arco-design/web-vue/es/form';
import { Message } from '@arco-design/web-vue';
import { DeptRecord } from '@/api/dept';
import { useDeptStore } from '@/store';
2024-11-12 15:44:05 +08:00
const props = defineProps({
dept: {
type: Object as PropType<DeptRecord>,
},
isCreate: Boolean,
});
const { t } = useI18n();
const modalTitle = computed(() => {
return props.isCreate ? t('createDept') : t('editDept');
});
const { visible, setVisible } = useVisible(false);
const deptStore = useDeptStore();
const createEditRef = ref<FormInstance>();
2024-11-12 15:44:05 +08:00
const formData = ref<DeptRecord>({
id: '',
remark: '',
createTime: '',
name: '',
enabled: '',
...props.dept,
});
2024-11-12 15:44:05 +08:00
const emit = defineEmits(['refresh']);
2024-11-12 15:44:05 +08:00
// 组件被点击
const handleClick = () => {
setVisible(true);
};
2024-11-12 15:44:05 +08:00
// 提交
const handleSubmit = async () => {
const valid = await createEditRef.value?.validate();
if (!valid) {
// 新增
if (props.isCreate) {
const res = await deptStore.createDept(formData.value);
if (res.status === 200) {
Message.success({
content: t('create.sucess'),
duration: 5 * 1000,
});
}
createEditRef.value?.resetFields();
} else {
// 编辑
const res = await deptStore.updateDept(formData.value);
if (res.status === 200) {
Message.success({
content: t('modify.sucess'),
duration: 5 * 1000,
});
}
2024-11-12 15:44:05 +08:00
}
emit('refresh');
setVisible(false);
2024-11-12 15:44:05 +08:00
}
};
2024-11-12 15:44:05 +08:00
// 关闭
const handleCancel = async () => {
createEditRef.value?.resetFields();
setVisible(false);
};
2024-11-12 15:44:05 +08:00
</script>
<style scoped></style>