wujingjing
2024-05-17 1c8b789894e34f697e23a1a82bdd4d17bb9323a6
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
<template>
    <el-row :gutter="8" class="h100">
        <el-col :span="4" :xs="24" class="h100">
            <el-card shadow="hover" class="h100 left-tree-card" v-loading="treeLoading">
                <LeftTreeByMgr
                    ref="leftTreeRef"
                    title-name="属性组列表"
                    :show-add="true"
                    :select-is-show="true"
                    :showSelectNodeIcon="true"
                    :selectFolderIcon="(_, data) => data.LogicalType === MODULE_CODE"
                    :select-data="selectTreeData"
                    :show-more-operate="true"
                    :select-props="{
                        id: 'LogicalID',
                        label: 'LogicalName',
                        children: 'Children',
                    }"
                    :default-select-value="currentSelectID"
                    :folder-icon="() => false"
                    @selectchange="selectNodeChange"
                    :treedata="listTreeData"
                    :current-node-key="currentListID"
                    :show-sorter="true"
                    @click="handleClickNode"
                    @node-drag-end="dragNodeEnd"
                    @tree-edit="openOperatePropertyGroupDialog"
                    @tree-delete="deleteCurrentPropertyGroup"
                    @tree-add="openOperatePropertyGroupDialog"
                >
                </LeftTreeByMgr>
            </el-card>
        </el-col>
        <el-col :span="20" :xs="24" class="flex-column h100">
            <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
                <el-form :inline="true" :model="queryParams">
                    <el-form-item label="名称" prop="Name">
                        <el-input v-model="queryParams.Name" style="width: 226.4px" placeholder="名称" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="编码" prop="Code">
                        <el-input v-model="queryParams.Code" style="width: 226.4px" placeholder="编码" clearable />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="ele-Search" @click="handleQueryTable"> 查询 </el-button>
                        <el-button icon="ele-Refresh" @click="resetQuery">重置 </el-button>
                        <el-button icon="ele-Plus" @click="openOperatePropertyDialog()"> 增加 </el-button>
                    </el-form-item>
                    <el-form-item label="排序">
                        <el-switch
                            v-model="isDragStatus"
                            @change="handleDragStatus"
                            inline-prompt
                            active-icon="ele-Check"
                            inactive-icon="ele-Close"
                        >
                        </el-switch>
                    </el-form-item>
                </el-form>
            </el-card>
 
            <el-card class="flex-auto scroll-table-card" shadow="hover" style="margin-top: 8px">
                <el-table
                    v-loading="tableLoading"
                    ref="draggableTableRef"
                    border
                    row-key="ID"
                    :row-class-name="isDragStatus ? 'cursor-move' : 'cursor-pointer'"
                    :cell-style="{ textAlign: 'center' }"
                    :header-cell-style="{ textAlign: 'center' }"
                    :data="displayTableData"
                    style="width: 100%"
                    highlight-current-row
                >
                    <el-table-column type="index" label="序号" width="55" align="center" />
                    <el-table-column prop="Name" label="名称" show-overflow-tooltip />
                    <el-table-column prop="Code" label="编码" show-overflow-tooltip />
                    <el-table-column prop="Format" label="格式" show-overflow-tooltip>
                        <template #default="scope">
                            {{ FORMAT_MAP[scope.row.Format] }}
                        </template>
                    </el-table-column>
 
                    <el-table-column prop="UnitName" label="单位" show-overflow-tooltip />
                    <el-table-column prop="DefaultValue" label="默认值" align="center" show-overflow-tooltip> </el-table-column>
                    <el-table-column prop="IsNull" label="可空" show-overflow-tooltip>
                        <template #default="scope">
                            <el-tag type="success" v-if="scope.row.IsNull">是</el-tag>
                            <el-tag type="danger" v-else>否</el-tag>
                        </template>
                    </el-table-column>
 
                    <el-table-column prop="Description" label="说明" show-overflow-tooltip />
                    <el-table-column label="操作" width="240" fixed="right" align="center" show-overflow-tooltip>
                        <template #default="scope">
                            <el-button icon="ele-Edit" size="small" text type="primary" @click="openOperatePropertyDialog(scope.row)">
                                编辑
                            </el-button>
                            <el-button icon="ele-Grid" size="small" text type="primary" @click="openDrawer(scope.row)"> 属性选项 </el-button>
 
                            <el-button icon="ele-Delete" size="small" text type="danger" @click="deleteCurrentRow(scope.row)"> 删除 </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </el-col>
        <!-- property 增加、修改数据对话框 -->
        <el-dialog
            :destroy-on-close="true"
            v-model="propertyDialogIsShow"
            width="500"
            :close-on-click-modal="false"
            @closed="closePropertyDialog"
        >
            <template #header>
                <div style="color: #fff">
                    <SvgIcon :name="propertyDialogHeaderIcon" :size="16" style="margin-right: 3px; display: inline; vertical-align: middle" />
                    <span> {{ propertyDialogTitle }} </span>
                </div>
            </template>
 
            <el-form :model="propertyDialogFormValue" ref="propertyDialogFormRef" :rules="propertyDialogFormRules" label-width="80">
                <el-form-item label="名称" prop="Name">
                    <el-input placeholder="请输入名称" v-model="propertyDialogFormValue.Name"></el-input>
                </el-form-item>
                <el-form-item label="编码" prop="Code">
                    <el-input placeholder="请输入唯一编码" v-model="propertyDialogFormValue.Code"></el-input>
                </el-form-item>
 
                <el-form-item label="属性格式" prop="Format">
                    <el-select v-model="propertyDialogFormValue.Format" class="w100" filterable @change="selectFormatChange">
                        <el-option
                            v-for="item of Object.keys(FORMAT_MAP)"
                            :key="item"
                            :label="FORMAT_MAP[item]"
                            :value="parseInt(item)"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="单位名称" prop="UnitName">
                    <el-input v-model="propertyDialogFormValue.UnitName" clearable />
                </el-form-item>
                <el-form-item label="默认值" prop="DefaultValue">
                    <el-radio-group
                        class="w100"
                        v-if="propertyDialogFormValue.Format === PropertyFormatEnum.Boolean"
                        v-model="propertyDialogFormValue.DefaultValue"
                    >
                        <el-radio label="true" value="true"></el-radio>
                        <el-radio label="false" value="false"></el-radio>
                    </el-radio-group>
                    <el-input v-else v-model="propertyDialogFormValue.DefaultValue"></el-input>
                </el-form-item>
                <el-form-item label="是否可空" prop="IsNull">
                    <el-checkbox v-model="propertyDialogFormValue.IsNull" />
                </el-form-item>
                <el-form-item label="说明" prop="Description">
                    <el-input v-model="propertyDialogFormValue.Description" type="textarea" :rows="3" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div>
                    <el-button @click="closePropertyDialog">取 消</el-button>
                    <el-button type="primary" @click="submitPropertyFormValue">确 定</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- propertyGroup 增加、修改数据对话框 -->
        <el-dialog
            :destroy-on-close="true"
            v-model="propertyGroupDialogIsShow"
            width="400"
            :close-on-click-modal="false"
            @closed="closePropertyGroupDialog"
        >
            <template #header>
                <div style="color: #fff">
                    <SvgIcon
                        :name="propertyGroupDialogHeaderIcon"
                        :size="16"
                        style="margin-right: 3px; display: inline; vertical-align: middle"
                    />
                    <span> {{ propertyGroupDialogTitle }} </span>
                </div>
            </template>
 
            <el-form
                :model="propertyGroupDialogFormValue"
                ref="propertyGroupDialogFormRef"
                :rules="propertyGroupDialogFormRules"
                label-width="55"
            >
                <el-form-item label="名称" prop="Name">
                    <el-input placeholder="请输入名称" v-model="propertyGroupDialogFormValue.Name"></el-input>
                </el-form-item>
 
                <el-form-item label="说明" prop="Description">
                    <el-input placeholder="请输入说明" v-model="propertyGroupDialogFormValue.Description" type="textarea" :rows="3" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div>
                    <el-button @click="closePropertyGroupDialog">取 消</el-button>
                    <el-button type="primary" @click="submitPropertyGroupFormValue">确 定</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 属性选项修改、编辑dialog -->
        <el-dialog
            :destroy-on-close="true"
            v-model="choiceDialogIsShow"
            width="400"
            :close-on-click-modal="false"
            @closed="closeChoiceDialog"
        >
            <template #header>
                <div style="color: #fff">
                    <SvgIcon :name="choiceDialogHeaderIcon" :size="16" style="margin-right: 3px; display: inline; vertical-align: middle" />
                    <span> {{ choiceDialogTitle }} </span>
                </div>
            </template>
 
            <el-form :model="choiceDialogFormValue" ref="choiceDialogFormRef" :rules="choiceDialogFormRules" label-width="55">
                <el-form-item label="名称" prop="Name">
                    <el-input placeholder="请输入名称" v-model="choiceDialogFormValue.Name"></el-input>
                </el-form-item>
                <el-form-item label="选项" prop="Choice">
                    <el-input placeholder="请输入选项" v-model="choiceDialogFormValue.Choice"></el-input>
                </el-form-item>
                <el-form-item label="说明" prop="Description">
                    <el-input placeholder="请输入说明" v-model="choiceDialogFormValue.Description" type="textarea" :rows="3" />
                </el-form-item>
            </el-form>
            <template v-slot:footer>
                <div>
                    <el-button @click="closeChoiceDialog">取 消</el-button>
                    <el-button type="primary" @click="submitChoiceFormValue">确 定</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 抽屉表格 -->
        <div class="custom-drawer">
            <el-drawer v-model="drawerIsShow" direction="rtl" size="30%">
                <template #header>
                    <div>
                        <SvgIcon name="ele-Grid" :size="16" style="margin-right: 3px; display: inline; vertical-align: middle" />
                        <span> {{ currentProp?.Name + ' - 属性选项配置' }} </span>
                    </div>
                </template>
                <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
                    <!-- 查询、重置、排序、增加表单 -->
                    <el-form :inline="true" :model="choiceQueryParams">
                        <el-form-item>
                            <el-button icon="ele-Plus" @click="openOperateChoiceDialog()"> 增加 </el-button>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-switch
                                v-model="isChoiceTableDrag"
                                @change="handleChoiceTableDrag"
                                inline-prompt
                                active-icon="ele-Check"
                                inactive-icon="ele-Close"
                            >
                            </el-switch>
                        </el-form-item>
                    </el-form>
                </el-card>
 
                <el-card class="flex-auto scroll-table-card" shadow="hover" style="margin-top: 8px">
                    <!-- 数据展示表格 -->
                    <el-table
                        v-loading="choiceTableLoading"
                        class="h100"
                        ref="draggableChoiceTableRef"
                        border
                        size="small"
                        row-key="ID"
                        :row-class-name="isChoiceTableDrag ? 'cursor-move' : 'cursor-pointer'"
                        :cell-style="{ textAlign: 'center' }"
                        :header-cell-style="{ textAlign: 'center' }"
                        :data="choiceTableData"
                        style="width: 100%"
                        highlight-current-row
                    >
                        <el-table-column prop="Name" label="名称" fixed="left" show-overflow-tooltip />
                        <el-table-column prop="Choice" label="选项" show-overflow-tooltip />
                        <el-table-column prop="Description" label="说明" show-overflow-tooltip />
                        <el-table-column label="操作" fixed="right" width="180" show-overflow-tooltip>
                            <template #default="scope">
                                <el-button icon="ele-Edit" size="small" text type="primary" @click="openOperateChoiceDialog(scope.row)">
                                    编辑
                                </el-button>
                                <el-button icon="ele-Delete" size="small" text type="danger" @click="deleteCurrentChoiceRow(scope.row)">
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-drawer>
        </div>
    </el-row>
</template>
 
<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import LeftTreeByMgr from '/@/components/tree/leftTreeByMgr.vue';
import type { PropType } from 'vue';
import { ref, onMounted, computed, nextTick } from 'vue';
import { deepClone } from '/@/utils/other';
import { useQueryTable } from '/@/hooks/useQueryTable';
import { updateSort, useTableSort } from '/@/hooks/useTableSort';
import { useValidateUniqueness } from '/@/hooks/useValidateUniqueness';
import {
    // 获取系统类型
    GetSysTypeSelectTreeData,
    // 属性接口
    DeleteAProperty,
    GetAllPropertyByID,
    GetIsExistPropertyCode,
    InsertAProperty,
    UpdateAProperty,
    UpdatePropertySorter,
    // 属性组接口
    GetPropertyGroupListTreeData,
    DeleteAPropertyGroup,
    InsertAPropertyGroup,
    UpdateAPropertyGroup,
    UpdatePropertyGroupSorter,
} from '/@/api/basic/property/propertyManage';
import {
    DeleteAPropChoice,
    GetAllPropChoice,
    InsertAPropChoice,
    UpdateAPropChoice,
    UpdatePropChoiceSorter,
} from '/@/api/basic/property/propChoice';
 
import { FORMAT_MAP, PropertyFormatEnum } from '/@/projectCom/basic/types';
 
import { booleanRegex, dateRegex, integerRegex, numberRegex } from '/@/utils/toolsValidate';
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import { MODULE_CODE } from '/@/constants';
import { arrayIsEmpty } from '/@/utils/util';
const props = defineProps({
    request: {
        type: Function as PropType<(config: AxiosRequestConfig<any>) => Promise<AxiosResponse<any, any>>>,
    },
});
 
//#region ====================== 左侧树数据,tree with select init ======================
const treeLoading = ref(false);
 
const selectTreeData = ref([]);
const currentSelectID = ref('');
const selectNodeChange = (data) => {
    currentSelectID.value = data.LogicalID;
    if (data.LogicalType === MODULE_CODE) {
        listTreeData.value = [];
        tableData.value = [];
        return;
    }
    getListTreeData(true);
};
 
const getSelectTreeData = async () => {
    treeLoading.value = true;
    const res = await GetSysTypeSelectTreeData(props.request).finally(() => {
        treeLoading.value = false;
    });
    if (res?.Code === 0) {
        selectTreeData.value = res.Data || [];
        const firstSelectTreeNode = selectTreeData.value[0]?.Children[0];
        if (firstSelectTreeNode) {
            selectNodeChange(firstSelectTreeNode);
        } else {
            listTreeData.value = [];
            tableData.value = [];
        }
    } else {
        ElMessage.error('获取系统类型列表失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
const leftTreeRef = ref(null);
const listTreeData = ref([]);
const currentListID = ref('');
const currentTreeNode = ref(null);
 
const handleClickNode = (data) => {
    currentTreeNode.value = data;
    nextTick(() => {
        leftTreeRef.value?.treeRef.setCurrentKey(data.ID);
    });
 
    currentListID.value = data.ID;
    getTableData();
};
const getListTreeData = async (selectFirst = false) => {
    treeLoading.value = true;
    const res = await GetPropertyGroupListTreeData(
        {
            TypeID: currentSelectID.value,
        },
        props.request
    ).finally(() => {
        treeLoading.value = false;
    });
    if (res?.Code === 0) {
        listTreeData.value = res.Data || [];
        if (selectFirst) {
            const firstListTreeNode = listTreeData.value[0];
            if (firstListTreeNode) {
                handleClickNode(firstListTreeNode);
            } else {
                tableData.value = [];
                currentTreeNode.value = null;
                currentListID.value = null;
            }
        } else {
            currentTreeNode.value && handleClickNode(currentTreeNode.value);
        }
    } else {
        ElMessage.error('获取属性组列表失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
//#endregion
 
//#region ====================== 左侧树拖拽 ======================
const dragNodeEnd = async (draggingNode, dropNode, dropType, ev, originTreeData) => {
    updateSort(
        listTreeData,
        listTreeData.value,
        originTreeData,
        () => {
            handleClickNode(draggingNode.data);
            getListTreeData();
        },
        UpdatePropertyGroupSorter,
        () => {
            handleClickNode(draggingNode.data);
        },
        undefined,
        props.request
    );
};
//#endregion
 
//#region ====================== 表格数据,table init ======================
const tableLoading = ref(false);
const tableData = ref([]);
const isDragStatus = ref(false);
const getTableData = async () => {
    tableLoading.value = true;
    const res = await GetAllPropertyByID({ GroupID: currentListID.value }, props.request).finally(() => {
        tableLoading.value = false;
    });
    if (res?.Code === 0) {
        tableData.value = res.Data || [];
    } else {
        ElMessage.error('获取属性失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
const deleteCurrentRow = (row: any) => {
    ElMessageBox.confirm(`确定删除属性:【${row.Name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const res = await DeleteAProperty(
            {
                ID: row.ID,
            },
            props.request
        );
        if (res?.Code === 0) {
            if (res.Data) {
                ElMessage.success('删除属性成功');
                getTableData();
            } else {
                ElMessage.error('删除属性失败');
            }
        } else {
            ElMessage.error('删除属性失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    });
};
//#endregion
 
//#region ====================== 表格查询、排序,search form init ======================
const queryParams = ref({
    Name: '',
    Code: '',
});
const { handleDragStatus, draggableTableRef } = useTableSort(
    tableData,
    UpdatePropertySorter,
    getTableData,
    undefined,
    false,
    props.request
);
 
const { resetQuery, handleQueryTable, displayTableData } = useQueryTable(tableData, queryParams, getTableData);
 
//#endregion
 
const selectFormatChange = () => {
    propertyDialogFormValue.value.DefaultValue = '';
    propertyDialogFormRef.value.clearValidate(['DefaultValue']);
};
 
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditPropertyDialog = ref(false);
const propertyDialogTitle = computed(() => {
    return isEditPropertyDialog.value ? '修改属性' : '添加属性';
});
const propertyDialogHeaderIcon = computed(() => {
    return isEditPropertyDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const propertyDialogFormValue = ref(null);
const propertyDialogIsShow = ref(false);
const propertyDialogFormRef = ref<FormInstance>(null);
const propertyInitialCode = ref('');
 
const { uniquenessValidator: propertyCodeValidator } = useValidateUniqueness(
    GetIsExistPropertyCode,
    propertyInitialCode,
    '编码',
    'Code',
    {
        TypeID: currentSelectID,
    },
    false,
    props.request
);
 
const defaultValueValidator = (rule: any, value: any, callback: any) => {
    if (!value) {
        callback();
    } else if (!propertyDialogFormValue.value.Format) {
        callback('请先选择属性格式,再填写默认值');
    } else {
        if (propertyDialogFormValue.value.Format === PropertyFormatEnum.Numeric && !numberRegex.test(value)) {
            callback('请输入数字');
        } else if (
            [PropertyFormatEnum.Bigint, PropertyFormatEnum.Integer].includes(propertyDialogFormValue.value.Format) &&
            !integerRegex.test(value)
        ) {
            callback('请输入整数');
        } else if (PropertyFormatEnum.Time === propertyDialogFormValue.value.Format && !dateRegex.test(value)) {
            callback('请输入正确的日期格式(yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd)');
        } else if (PropertyFormatEnum.Boolean === propertyDialogFormValue.value.Format && !booleanRegex.test(value)) {
            callback('请选择 true 或 false');
        } else {
            callback();
        }
    }
};
 
const propertyDialogFormRules = ref<FormRules>({
    Name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
    Code: [{ required: true, validator: propertyCodeValidator as any, trigger: 'blur' }],
    Format: [{ required: true, message: '请选择格式', trigger: 'change' }],
    DefaultValue: [{ validator: defaultValueValidator as any, trigger: 'blur' }],
});
const openOperatePropertyDialog = (row?) => {
    if (!currentListID.value) {
        return ElMessage.warning('请先选择属性组!');
    }
    if (row) {
        isEditPropertyDialog.value = true;
        const { ID, Name, Format, UnitName, IsNull, DefaultValue, Code, Description } = row;
        propertyInitialCode.value = Code;
        propertyDialogFormValue.value = deepClone({ ID, Name, Format, UnitName, IsNull, DefaultValue, Code, Description });
    } else {
        isEditPropertyDialog.value = false;
        propertyInitialCode.value = '';
        propertyDialogFormValue.value = {
            GroupID: currentListID.value,
            Name: '',
            Format: null,
            UnitName: '',
            IsNull: true,
            DefaultValue: '',
            Code: '',
            Description: '',
        };
    }
    propertyDialogIsShow.value = true;
};
 
const closePropertyDialog = () => {
    propertyDialogIsShow.value = false;
    propertyDialogFormRef.value.clearValidate();
};
 
const submitPropertyFormValue = async () => {
    const valid = await propertyDialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
 
    if (isEditPropertyDialog.value) {
        const res = await UpdateAProperty(propertyDialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getTableData();
                closePropertyDialog();
                ElMessage.success('修改属性成功');
            } else {
                ElMessage.error('修改属性失败');
            }
        } else {
            ElMessage.error('修改属性失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    } else {
        const res = await InsertAProperty(propertyDialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getTableData();
                closePropertyDialog();
                ElMessage.success('添加属性成功');
            } else {
                ElMessage.error('添加属性失败');
            }
        } else {
            ElMessage.error('添加属性失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    }
};
//#endregion
 
//#region ====================== 增加、删除属性组操作, dialog init======================
const isEditPropertyGroupDialog = ref(false);
const propertyGroupDialogTitle = computed(() => {
    return isEditPropertyGroupDialog.value ? '修改属性组' : '添加属性组';
});
const propertyGroupDialogHeaderIcon = computed(() => {
    return isEditPropertyGroupDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const propertyGroupDialogFormValue = ref(null);
const propertyGroupDialogIsShow = ref(false);
const propertyGroupDialogFormRef = ref<FormInstance>(null);
 
const propertyGroupDialogFormRules = ref<FormRules>({
    Name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
});
const openOperatePropertyGroupDialog = (data?) => {
    if (data) {
        isEditPropertyGroupDialog.value = true;
        const { ID, Name, Description } = data;
        propertyGroupDialogFormValue.value = deepClone({ ID, Name, Description });
    } else {
        isEditPropertyGroupDialog.value = false;
        propertyGroupDialogFormValue.value = { TypeID: currentSelectID.value, Name: '', Description: '' };
    }
    propertyGroupDialogIsShow.value = true;
};
 
const closePropertyGroupDialog = () => {
    propertyGroupDialogIsShow.value = false;
    propertyGroupDialogFormRef.value.clearValidate();
};
 
const submitPropertyGroupFormValue = async () => {
    const valid = await propertyGroupDialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
 
    if (isEditPropertyGroupDialog.value) {
        const res = await UpdateAPropertyGroup(propertyGroupDialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getListTreeData();
                closePropertyGroupDialog();
                ElMessage.success('修改属性组成功');
            } else {
                ElMessage.error('修改属性组失败');
            }
        } else {
            ElMessage.error('修改属性组失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    } else {
        const res = await InsertAPropertyGroup(propertyGroupDialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getListTreeData();
                closePropertyGroupDialog();
                ElMessage.success('添加属性组成功');
            } else {
                ElMessage.error('添加属性组失败');
            }
        } else {
            ElMessage.error('添加属性组失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    }
};
//#endregion
 
//#region ====================== 删除左侧树属性组数据 ======================
 
const deleteCurrentPropertyGroup = (row: any) => {
    ElMessageBox.confirm(`确定删除属性组:【${row.Name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const res = await DeleteAPropertyGroup(
            {
                ID: row.ID,
            },
            props.request
        );
        if (res?.Code === 0) {
            if (res.Data) {
                ElMessage.success('删除属性组成功');
                getListTreeData(true);
            } else {
                ElMessage.error('删除属性组失败');
            }
        } else {
            ElMessage.error('删除属性组失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    });
};
//#endregion
//#region ====================== 属性选项 drawer ======================
const drawerIsShow = ref(false);
const currentProp = ref(null);
 
const openDrawer = (row: any) => {
    drawerIsShow.value = true;
    currentProp.value = row;
    getChoiceTableData();
};
//#endregion
//#region ====================== 属性选项CRUD ======================
//#region ====================== 获取、删除表格数据 ======================
const choiceTableLoading = ref(false);
const choiceTableData = ref([]);
const isChoiceTableDrag = ref(false);
const nextChoiceValue = computed(() => {
    if (arrayIsEmpty(choiceTableData.value)) {
        return '0';
    } else {
        const lastItem = choiceTableData.value.at(-1);
        if (integerRegex.test(lastItem.Choice)) {
            return Number(lastItem.Choice) + 1 + '';
        } else {
            return '';
        }
    }
});
const getChoiceTableData = async () => {
    choiceTableLoading.value = true;
    const res = await GetAllPropChoice({ PropID: currentProp.value.ID }, props.request).finally(() => {
        choiceTableLoading.value = false;
    });
    if (res?.Code === 0) {
        choiceTableData.value = res.Data || [];
    } else {
        ElMessage.error('获取属性选项失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
const deleteCurrentChoiceRow = (row: any) => {
    ElMessageBox.confirm(`确定删除属性选项:【${row.Name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const res = await DeleteAPropChoice(
            {
                ID: row.ID,
            },
            props.request
        );
        if (res?.Code === 0) {
            if (res.Data) {
                ElMessage.success('删除属性选项成功');
                getChoiceTableData();
            } else {
                ElMessage.error('删除属性选项失败');
            }
        } else {
            ElMessage.error('删除属性选项失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    });
};
//#endregion
 
const choiceQueryParams = ref({
    Name: '',
});
const { handleDragStatus: handleChoiceTableDrag, draggableTableRef: draggableChoiceTableRef } = useTableSort(
    choiceTableData,
    UpdatePropChoiceSorter,
    getChoiceTableData,
    undefined,
    false,
    props.request
);
 
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditChoiceDialog = ref(false);
const choiceDialogTitle = computed(() => {
    return isEditChoiceDialog.value ? '修改属性选项' : '添加属性选项';
});
const choiceDialogHeaderIcon = computed(() => {
    return isEditChoiceDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const choiceDialogFormValue = ref(null);
const choiceDialogIsShow = ref(false);
const choiceDialogFormRef = ref<FormInstance>(null);
 
const choiceDialogFormRules = ref<FormRules>({
    Name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
    Choice: [{ required: true, message: '请输入属性选项', trigger: 'blur' }],
    DefaultValue: [{ validator: defaultValueValidator, trigger: 'blur' }],
});
const openOperateChoiceDialog = (row?) => {
    if (row) {
        isEditChoiceDialog.value = true;
        const { ID, Name, Choice, Description } = row;
        choiceDialogFormValue.value = deepClone({ ID, Name, Choice, Description });
    } else {
        isEditChoiceDialog.value = false;
        choiceDialogFormValue.value = { PropID: currentProp.value.ID, Name: '', Choice: nextChoiceValue.value, Description: '' };
    }
    choiceDialogIsShow.value = true;
};
 
const closeChoiceDialog = () => {
    choiceDialogIsShow.value = false;
    choiceDialogFormRef.value.clearValidate();
};
 
const submitChoiceFormValue = async () => {
    const valid = await choiceDialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
 
    if (isEditChoiceDialog.value) {
        const res = await UpdateAPropChoice(choiceDialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getChoiceTableData();
                closeChoiceDialog();
                ElMessage.success('修改属性选项成功');
            } else {
                ElMessage.error('修改属性选项失败');
            }
        } else {
            ElMessage.error('修改属性选项失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    } else {
        const res = await InsertAPropChoice(choiceDialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getChoiceTableData();
                closeChoiceDialog();
                ElMessage.success('添加属性选项成功');
            } else {
                ElMessage.error('添加属性选项失败');
            }
        } else {
            ElMessage.error('添加属性选项失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    }
};
//#endregion
 
onMounted(() => {
    getSelectTreeData();
});
</script>
<style scoped lang="scss"></style>