|
@@ -0,0 +1,196 @@
|
|
|
|
+<script setup lang="ts">
|
|
|
|
+import { useAntdForm, useFormRules } from '@/hooks/common/form';
|
|
|
|
+import { $t } from '@/locales';
|
|
|
|
+import { fetchDeptAdd, fetchDeptList, getDicts, fetchDeptDetail, fetchDeptEdit } from '@/service/api';
|
|
|
|
+import { computed, reactive, ref, watch } from 'vue';
|
|
|
|
+defineOptions({
|
|
|
|
+ name: 'DeptOperateDrawer'
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+interface Props {
|
|
|
|
+ /** the type of operation */
|
|
|
|
+ operateType: AntDesign.TableOperateType;
|
|
|
|
+ /** the edit row data */
|
|
|
|
+ rowData?: Api.Admin.SysDept | null;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const props = defineProps<Props>();
|
|
|
|
+
|
|
|
|
+interface Emits {
|
|
|
|
+ (e: 'submitted'): void;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const emit = defineEmits<Emits>();
|
|
|
|
+
|
|
|
|
+const visible = defineModel<boolean>('visible', {
|
|
|
|
+ default: false
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+const { formRef, validate, resetFields } = useAntdForm();
|
|
|
|
+const { defaultRequiredRule, formRules, createRequiredRule } = useFormRules();
|
|
|
|
+
|
|
|
|
+const title = computed(() => {
|
|
|
|
+ const titles: Record<AntDesign.TableOperateType, string> = {
|
|
|
|
+ add: $t('page.admin.sysDept.addDept'),
|
|
|
|
+ edit: $t('page.admin.sysDept.editDept')
|
|
|
|
+ };
|
|
|
|
+ return titles[props.operateType];
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+type Model = Pick<
|
|
|
|
+ Api.Admin.SysDept,
|
|
|
|
+ 'parentId' | 'deptName' | 'orderNum' | 'leader' | 'phone' | 'email' | 'status'>;
|
|
|
|
+
|
|
|
|
+const model: Model = reactive(createDefaultModel());
|
|
|
|
+
|
|
|
|
+function createDefaultModel(): Model {
|
|
|
|
+ return {
|
|
|
|
+ parentId: null,
|
|
|
|
+ deptName: '',
|
|
|
|
+ orderNum: 10,
|
|
|
|
+ leader: '',
|
|
|
|
+ phone: '',
|
|
|
|
+ email: '',
|
|
|
|
+ status: '2',
|
|
|
|
+ };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type RuleKey = Extract<keyof Model, 'parentId' | 'deptName' | 'orderNum' | 'leader' | 'phone' | 'email' | 'status'>;
|
|
|
|
+
|
|
|
|
+const rules: Record<RuleKey, App.Global.FormRule> = {
|
|
|
|
+ parentId: createRequiredRule($t('page.admin.sysDept.form.parentId')),
|
|
|
|
+ deptName: defaultRequiredRule,
|
|
|
|
+ orderNum: defaultRequiredRule,
|
|
|
|
+ leader: defaultRequiredRule,
|
|
|
|
+ phone: formRules.phone,
|
|
|
|
+ email: formRules.email,
|
|
|
|
+ status: defaultRequiredRule
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const deptList = ref<CommonType.Option<string>[]>([]);
|
|
|
|
+async function getDeptList() {
|
|
|
|
+ const { data } = await fetchDeptList()
|
|
|
|
+ const dept = { deptId: '0', value: '0', deptName: '主类目', label: '主类目', children: [], selectable: false }
|
|
|
|
+ dept.children = data
|
|
|
|
+ deptList.value = [dept]
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const deptStatus = ref<CommonType.Option<string>[]>([]);
|
|
|
|
+async function getDeptStatusOptions() {
|
|
|
|
+ const { data } = await getDicts('sys_normal_disable')
|
|
|
|
+ deptStatus.value = data || []
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function getDeptDetails() {
|
|
|
|
+ let deptId = props.rowData?.deptId + ''
|
|
|
|
+ const { data } = await fetchDeptDetail(deptId)
|
|
|
|
+ data.status = data.status + ''
|
|
|
|
+ data.orderNum = data.sort
|
|
|
|
+ Object.assign(model, data);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function handleInitModel() {
|
|
|
|
+ Object.assign(model, createDefaultModel());
|
|
|
|
+ console.log('handleInitModel', props.operateType, props.rowData)
|
|
|
|
+ if (props.operateType === 'edit' && props.rowData) {
|
|
|
|
+ getDeptDetails()
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function closeDrawer() {
|
|
|
|
+ visible.value = false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function handleSubmit() {
|
|
|
|
+ await validate();
|
|
|
|
+ let fnMap = {
|
|
|
|
+ add: () => {
|
|
|
|
+ const { code, msg } = fetchDeptAdd(model)
|
|
|
|
+ if (code === 200) {
|
|
|
|
+ window.$message?.success(msg);
|
|
|
|
+ closeDrawer();
|
|
|
|
+ emit('submitted');
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ edit: () => {
|
|
|
|
+ let deptId = props.rowData?.deptId + ''
|
|
|
|
+ const { code, msg } = fetchDeptEdit(model, deptId)
|
|
|
|
+ if (code === 200) {
|
|
|
|
+ window.$message?.success(msg);
|
|
|
|
+ closeDrawer();
|
|
|
|
+ emit('submitted');
|
|
|
|
+ }
|
|
|
|
+ console.log('edit')
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ fnMap[props.operateType] && fnMap[props.operateType]()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+watch(visible, () => {
|
|
|
|
+ if (visible.value) {
|
|
|
|
+ handleInitModel()
|
|
|
|
+ resetFields()
|
|
|
|
+ getDeptList()
|
|
|
|
+ getDeptStatusOptions()
|
|
|
|
+ }
|
|
|
|
+});
|
|
|
|
+</script>
|
|
|
|
+
|
|
|
|
+<template>
|
|
|
|
+ <AModal v-model:open="visible" :title="title" width="800px">
|
|
|
|
+ <AForm ref="formRef" :model="model" :rules="rules" :label-col="{ lg: 8, xs: 4 }" label-wrap class="pr-20px">
|
|
|
|
+ <ARow wrap>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.parentId')" name="parentId">
|
|
|
|
+ <ATreeSelect v-model:value="model.parentId" show-search :tree-data="deptList"
|
|
|
|
+ :fieldNames="{ value: 'deptId', children: 'children', label: 'deptName' }"
|
|
|
|
+ :placeholder="$t('page.admin.sysDept.form.parentId')" allowClear />
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.deptName')" name="deptName">
|
|
|
|
+ <AInput v-model:value="model.deptName" :placeholder="$t('page.admin.sysDept.form.deptName')" allowClear />
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.orderNum')" name="orderNum">
|
|
|
|
+ <AInputNumber v-model:value="model.orderNum" :min="0" :placeholder="$t('page.admin.sysDept.form.orderNum')"
|
|
|
|
+ allowClear />
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.leader')" name="leader">
|
|
|
|
+ <AInput v-model:value="model.leader" :placeholder="$t('page.admin.sysDept.form.leader')" allowClear />
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.phone')" name="phone">
|
|
|
|
+ <AInput v-model:value="model.phone" :placeholder="$t('page.admin.sysDept.form.phone')" allowClear />
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.email')" name="email">
|
|
|
|
+ <AInput v-model:value="model.email" :placeholder="$t('page.admin.sysDept.form.email')" allowClear />
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ <ACol :span="12" :md="12" :xs="24">
|
|
|
|
+ <AFormItem :label="$t('page.admin.sysDept.status')" name="status">
|
|
|
|
+ <ARadioGroup v-model:value="model.status">
|
|
|
|
+ <ARadio v-for="item in deptStatus" :key="item.value" :value="item.value">
|
|
|
|
+ {{ item.label }}
|
|
|
|
+ </ARadio>
|
|
|
|
+ </ARadioGroup>
|
|
|
|
+ </AFormItem>
|
|
|
|
+ </ACol>
|
|
|
|
+ </ARow>
|
|
|
|
+ </AForm>
|
|
|
|
+ <template #footer>
|
|
|
|
+ <ASpace :size="16">
|
|
|
|
+ <AButton @click="closeDrawer">{{ $t('common.cancel') }}</AButton>
|
|
|
|
+ <AButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</AButton>
|
|
|
|
+ </ASpace>
|
|
|
|
+ </template>
|
|
|
|
+ </AModal>
|
|
|
|
+</template>
|
|
|
|
+
|
|
|
|
+<style scoped></style>
|