yangyin
2024-11-18 efd9873b6400110f7651c29480ca1f3e46f6d845
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
    <ywDialog
        v-model="dialogIsShow"
        :headerIcon="dialogHeaderIcon"
        :title="dialogTitle"
        width="380"
        @dlgClosed="closeDialog"
        @submit="submitFormValue"
    >
        <el-form :model="dialogFormValue" ref="dialogFormRef" :rules="dialogFormRules" label-width="80">
            <el-row :gutter="35">
                <el-col :span="24" class="mb20">
                    <el-form-item label="部门名称" prop="name">
                        <el-input v-model="dialogFormValue.name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20"
                    ><el-form-item label="父级" prop="parent_id" auto-complete="new-password">
                        <el-tree-select
                            filterable
                            style="width: 100%"
                            v-model="dialogFormValue.parent_id"
                            :props="{
                                id: 'id',
                                label: 'name',
                                children: 'children',
                            }"
                            :data="parentSelectOptions"
                            node-key="id"
                            :clearable="true"
                            :accordion="true"
                            :expandNode="false"
                            :check-strictly="true"
                            placeholder="请选择父级"
                        >
                        </el-tree-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </ywDialog>
</template>
 
<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { computed, ref, toRefs, watch } from 'vue';
import * as departmentApi from '/@/api/department/index';
import ywDialog from '/@/components/dialog/yw-dialog.vue';
import { formatDate } from '/@/utils/formatTime';
import { deepClone } from '/@/utils/other';
import { travelTree } from '/@/utils/util';
const props = defineProps(['item', 'departmentTreeList']);
const { departmentTreeList, item } = toRefs(props);
const emit = defineEmits(['update', 'insert']);
const parentSelectOptions = computed(() => {
    const cloneTreeData = deepClone(tableTreeData.value);
    travelTree(cloneTreeData, (value, index, array, parent) => {
        if ((value as any).id === dialogFormValue.value.id) {
            if (parent === array) {
                parent?.splice(index, 1);
            } else {
                parent?.Children?.splice(index, 1);
            }
        }
    });
    return cloneTreeData;
});
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditDialog = ref(false);
const dialogTitle = computed(() => {
    return isEditDialog.value ? `修改部门【${props.item?.name}】` : `添加部门`;
});
const dialogHeaderIcon = computed(() => {
    return isEditDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const dialogIsShow = defineModel({
    type: Boolean,
});
const dialogFormRef = ref<FormInstance>(null);
const dialogFormRules = ref<FormRules>({
    name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
});
const openOperateDialog = (row?) => {
    if (row) {
        isEditDialog.value = true;
        const { id, parent_id, name, create_user, create_time } = row;
        dialogFormValue.value = deepClone({ id, parent_id, name, create_user, create_time });
    } else {
        isEditDialog.value = false;
        dialogFormValue.value = {
            name: '',
            parent_id: null,
        };
    }
};
const closeDialog = () => {
    dialogIsShow.value = false;
    dialogFormRef.value.clearValidate();
};
 
const submitFormValue = async () => {
    const valid = await dialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
    const updateTime = formatDate(new Date());
 
    if (isEditDialog.value) {
        const res = await departmentApi.update_department_info(dialogFormValue.value);
        emit('update', { ...dialogFormValue.value });
        closeDialog();
        ElMessage.success('修改部门成功');
    } else {
        const res = await departmentApi.add_department_info(dialogFormValue.value);
        emit('insert');
        // tableData.value.push(newData);
        closeDialog();
        ElMessage.success('添加部门成功');
    }
};
 
//#endregion
const dialogFormValue = ref(null);
watch(
    () => dialogIsShow.value,
    (val) => {
        if (!val) return;
        openOperateDialog(item.value);
    }
);
const tableTreeData = ref([]);
watch(
    () => departmentTreeList.value,
    (val) => {
        if (!val) return;
        tableTreeData.value = val;
    }
);
watch(
    () => item.value,
    (val) => {
        if (!val) return;
        const { id, parent_id, name, create_user, create_time } = item.value;
        dialogFormValue.value = { id, parent_id, name, create_user, create_time };
    }
);
</script>
<style scoped lang="scss"></style>