wujingjing
2025-04-16 692c19940b25bedd5b3d0e2cf2b8e73fe440f5c6
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!-- 修改父级对话框 -->
<template>
    <ywDialog
        v-model="modelValue"
        title="修改父级"
        headerIcon="ele-Menu"
        width="400"
        @closed="closeParentDialog"
        @submit="submitParentForm"
        @close="closeParentDialog"
    >
        <el-form :model="parentForm" ref="parentFormRef" label-width="80px" label-position="top">
            <el-form-item label="父级" prop="ParentID">
                <el-tree-select
                    filterable
                    class="w100"
                    v-model="parentForm.ParentID"
                    :props="{
                        id: 'ID',
                        label: 'Name',
                        children: 'Children',
                    }"
                    :data="parentSelectOptions"
                    node-key="ID"
                    :clearable="true"
                    :accordion="false"
                    :expandNode="false"
                    :check-strictly="true"
                    placeholder="请选择父级"
                >
                </el-tree-select>
            </el-form-item>
        </el-form>
    </ywDialog>
</template>
 
<script setup lang="ts">
import type { Ref } from 'vue';
import { toRefs, ref, computed, watch } from 'vue';
import ywDialog from '/@/components/dialog/yw-dialog.vue';
import { travelTree } from '/@/utils/util';
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import { deepClone } from '/@/utils/other';
import { maxInArray } from '/@/utils/istation/common';
import request from '/@/utils/request';
import { requestProps } from '/@/projectCom/common';
 
const props = defineProps({
    /** @description 当前行数据 */
    rowData: Object,
    /** @description 可选父级(树结构数据) */
    data: Array,
    modelValue: {
        type: Boolean,
    },
    api: Function,
    ...requestProps,
});
 
const emits = defineEmits<{
    (event: 'update:modelValue', value): void;
    /** @description 提交成功回调 */
    (event: 'submitSuccess', res): void;
    /** @description 提交失败回调 */
    (event: 'submitError', res): void;
}>();
 
const { modelValue, api, data, req, rowData } = toRefs(props);
const isShow = computed({
    get: () => {
        return modelValue.value;
    },
    set: (value) => {
        emits('update:modelValue', value);
    },
});
const parentForm = ref(null);
const parentFormRef = ref<FormInstance>(null);
const parentSelectOptions = computed(() => {
    const cloneTreeData = deepClone(data.value);
    travelTree(cloneTreeData, (value, index, array, parent) => {
        if ((value  as any).ID === parentForm.value.ID) {
            if (parent === array) {
                parent?.splice(index, 1);
            } else {
                parent?.Children?.splice(index, 1);
            }
        }
    });
    return cloneTreeData;
});
 
const closeParentDialog = () => {
    isShow.value = false;
    parentFormRef.value.clearValidate();
};
 
const setSortCode = (formValue: Ref<any>, parentID: string) => {
    if (!parentID || parentID === '0') {
        formValue.value.SortCode = 1;
    } else {
        let parent = null;
        travelTree(parentSelectOptions.value, (value) => {
            if ((value as any).ID === parentID) {
                parent = value;
                return true;
            }
        });
        let parentChildren = [];
        if (parent) {
            parentChildren = parent.Children;
            // 没有 parent,为根
        } else {
            parentChildren = parentSelectOptions.value;
        }
        let sort = 1;
        if (parentChildren && parentChildren?.length > 0) {
            let sorts = parentChildren.map((x) => {
                return x.SortCode;
            });
            let maxSort = maxInArray(sorts);
            sort = maxSort + 1;
        }
        formValue.value.SortCode = sort;
    }
};
 
const submitParentForm = async () => {
    const valid = await parentFormRef.value.validate().catch(() => {});
    if (!valid) return;
    const sendParentID = parentForm.value.ParentID ?? '0';
 
    setSortCode(parentForm, sendParentID);
    const res = await api.value({ ...parentForm.value, ParentID: sendParentID }, req.value);
    if (res?.Code === 0) {
        if (res.Data) {
            emits('submitSuccess', res);
            isShow.value = false;
            ElMessage.success('修改成功');
        } else {
            emits('submitError', res);
            ElMessage.error('添加失败');
        }
    } else {
        emits('submitError', res);
 
        ElMessage.error('修改失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
 
watch(
    () => modelValue.value,
    (val) => {
        if (val) {
            const { SortCode, ID, ParentID } = rowData.value;
            parentForm.value = { SortCode, ID, ParentID: ParentID === '0' ? '' : ParentID };
        }
    }
);
</script>
<style scoped lang="scss"></style>