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
(function (vc) {
    vc.extends({
        data: {
            roleAInfo: {
                all: false,
                button: false,
                header: false
            },
            pgId: '', // 保存角色编码
            privilegeData: [], // 保存权限数据
            pName: '全部', // 保存权限名称,默认为"全部"
            buttonPrivileges: [], // 保存按钮权限
            labelPrivileges: [], // 保存标签权限
            dataPrivileges: [], // 保存数据权限
        },
        _initMethod: function () {
            // 获取URL参数
            let pId = '';
            let pgId = '';
            let communityId = '';
            let type = '';
            let pName = '全部'; // 默认值为"全部"
            
            // 优先从 hash 中获取参数(单页应用使用 hash 路由)
            const hash = location.hash;
            const search = location.search;
            
            // 先尝试从 hash 中获取
            if (hash && hash.indexOf('?') !== -1) {
                const hashParams = hash.substring(hash.indexOf('?') + 1);
                const params = hashParams.split('&');
                for (let i = 0; i < params.length; i++) {
                    const param = params[i].split('=');
                    const key = param[0];
                    const value = decodeURIComponent(param[1] || '');
                    if (key === 'pId') {
                        pId = value;
                    } else if (key === 'pgId') {
                        pgId = value;
                    } else if (key === 'communityId') {
                        communityId = value;
                    } else if (key === 'type') {
                        type = value;
                    } else if (key === 'pName') {
                        pName = value;
                    }
                }
            }
            
            // 如果 hash 中没有,尝试从 search 中获取
            if (!pId) {
                pId = vc.getParam('pId') || '';
            }
            if (!pgId) {
                pgId = vc.getParam('pgId') || '';
            }
            if (!communityId) {
                communityId = vc.getParam('communityId') || '';
            }
            if (!type) {
                type = vc.getParam('type') || '';
            }
            if (pName === '全部') {
                pName = vc.getParam('pName') || '全部';
            }
            
            // 如果缺少必要参数,使用默认值或从当前社区获取
            if (!communityId) {
                communityId = vc.getCurrentCommunity().communityId;
            }
            
            // 保存 pgId、pId 和 pName 到 data 中
            vc.component.pgId = pgId;
            vc.component.pId = pId;
            vc.component.pName = pName;
            
            // 更新页面标题
            const titleElement = document.querySelector('.role-a-container h3');
            if (titleElement) {
                titleElement.textContent = pName;
            }
            
            // 调用接口
            if (pgId && communityId) {
                let param = {
                    params: {
                        pgId: pgId,
                        communityId: communityId,
                        pId: pId,
                        type: 'all'
                    }
                };
                
                vc.http.apiGet('/query.privilegeGroup.noAddPrivilege',
                    param,
                    function (json) {
                        // 接口调用成功处理
                        try {
                            // 判断 json 的类型,如果已经是对象则直接使用,否则进行解析
                            let data = typeof json === 'string' ? JSON.parse(json) : json;
                            vc.component.privilegeData = data;
                            vc.component.buttonPrivileges = data.filter(item => item.type === '按钮');
                            vc.component.labelPrivileges = data.filter(item => item.type === '标签');
                            vc.component.dataPrivileges = data.filter(item => item.type === '数据');
                            vc.component.renderPrivileges(data);
                            vc.component.initCheckboxEvents();
                        } catch (e) {
                            console.error('解析数据失败:', e);
                            console.error('原始数据:', json);
                            console.error('数据类型:', typeof json);
                            vc.toast('数据解析失败');
                        }
                    },
                    function () {
                        console.log('请求失败处理');
                        vc.toast('加载数据失败');
                    }
                );
            } else {
                console.warn('缺少必要参数,无法调用接口');
            }
        },
        methods: {
            goBack: function () {
                vc.goBack();
            },
            // 渲染权限复选框
            renderPrivileges: function (data) {
                const buttonContainer = document.getElementById('button-container');
                const labelContainer = document.getElementById('label-container');
                const dataContainer = document.getElementById('data-container');
                
                // 清空容器
                if (buttonContainer) buttonContainer.innerHTML = '';
                if (labelContainer) labelContainer.innerHTML = '';
                if (dataContainer) dataContainer.innerHTML = '';
                
                // 遍历数据,根据 type 创建对应的复选框
                data.forEach(function (item) {
                    const checkboxDiv = document.createElement('div');
                    checkboxDiv.className = 'role-a-checkbox';
                    checkboxDiv.style.gap = '10px';
                    checkboxDiv.style.display = 'flex';
                    checkboxDiv.style.alignItems = 'center';
                    
                    const label = document.createElement('label');
                    label.textContent = item.pName;
                    label.style.paddingTop = '8px';
                    
                    const checkbox = document.createElement('input');
                    checkbox.type = 'checkbox';
                    checkbox.name = 'privilege';
                    checkbox.value = item.pId;
                    checkbox.setAttribute('data-pid', item.pId);
                    checkbox.setAttribute('data-type', item.type);
                    checkbox.checked = true;
                    
                    checkboxDiv.appendChild(checkbox);
                    checkboxDiv.appendChild(label);
                    
                    // 根据 type 添加到对应容器
                    if (item.type === '按钮') {
                        if (buttonContainer) {
                            buttonContainer.appendChild(checkboxDiv);
                        }
                    } else if (item.type === '标签') {
                        if (labelContainer) {
                            labelContainer.appendChild(checkboxDiv);
                        }
                    } else if (item.type === '数据') {
                        if (dataContainer) {
                            dataContainer.appendChild(checkboxDiv);
                        }
                    }
                });
            },
            // 初始化复选框事件
            initCheckboxEvents: function () {
                const self = this;
                
                // 全部复选框
                const allCheckbox = document.getElementById('all');
                if (allCheckbox) {
                    allCheckbox.addEventListener('change', function () {
                        const isChecked = this.checked;
                        // 选中所有复选框
                        const allCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
                        allCheckboxes.forEach(function (cb) {
                            cb.checked = isChecked;
                        });
                        // 同时更新按钮和数据区域的"全部"复选框
                        const buttonAll = document.getElementById('button-all');
                        const labelAll = document.getElementById('label-all');
                        const dataAll = document.getElementById('data-all');
                        if (buttonAll) buttonAll.checked = isChecked;
                        if (labelAll) labelAll.checked = isChecked;
                        if (dataAll) dataAll.checked = isChecked;
                    });
                }
                
                // 按钮区域全部复选框
                const buttonAllCheckbox = document.getElementById('button-all');
                if (buttonAllCheckbox) {
                    buttonAllCheckbox.addEventListener('change', function () {
                        const isChecked = this.checked;
                        // 选中按钮区域所有复选框
                        const buttonCheckboxes = document.querySelectorAll('#button-container input[type="checkbox"][name="privilege"]');
                        buttonCheckboxes.forEach(function (cb) {
                            cb.checked = isChecked;
                        });
                        // 检查是否需要更新"全部"复选框
                        self.updateAllCheckbox();
                    });
                }
 
                // 标签区域全部复选框
                const labelAllCheckbox = document.getElementById('label-all');
                if (labelAllCheckbox) {
                    labelAllCheckbox.addEventListener('change', function () {
                        const isChecked = this.checked;
                        // 选中标签区域所有复选框
                        const labelCheckboxes = document.querySelectorAll('#label-container input[type="checkbox"][name="privilege"]');
                        labelCheckboxes.forEach(function (cb) {
                            cb.checked = isChecked;
                        });
                        // 检查是否需要更新"全部"复选框
                        self.updateAllCheckbox();
                    });
                }
 
                // 数据区域全部复选框
                const dataAllCheckbox = document.getElementById('data-all');
                if (dataAllCheckbox) {
                    dataAllCheckbox.addEventListener('change', function () {
                        const isChecked = this.checked;
                        // 选中数据区域所有复选框
                        const dataCheckboxes = document.querySelectorAll('#data-container input[type="checkbox"][name="privilege"]');
                        dataCheckboxes.forEach(function (cb) {
                            cb.checked = isChecked;
                        });
                        // 检查是否需要更新"全部"复选框
                        self.updateAllCheckbox();
                    });
                }
                
                // 监听所有权限复选框的变化,更新"全部"复选框状态
                const privilegeCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
                privilegeCheckboxes.forEach(function (checkbox) {
                    checkbox.addEventListener('change', function () {
                        self.updateAllCheckbox();
                        self.updateSectionAllCheckbox();
                    });
                });
                
                // 保存按钮事件
                const saveButton = document.getElementById('save');
                if (saveButton) {
                    saveButton.addEventListener('click', function () {
                        self.savePrivileges();
                    });
                }
                
                // 重置按钮事件
                const resetButton = document.getElementById('reset');
                if (resetButton) {
                    resetButton.addEventListener('click', function () {
                        // 选中所有权限复选框
                        const allCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
                        allCheckboxes.forEach(function (cb) {
                            cb.checked = true;
                        });
                        // 选中所有"全部"复选框
                        const allCheckbox = document.getElementById('all');
                        const buttonAll = document.getElementById('button-all');
                        const labelAll = document.getElementById('label-all');
                        const dataAll = document.getElementById('data-all');
                        if (allCheckbox) allCheckbox.checked = true;
                        if (buttonAll) buttonAll.checked = true;
                        if (labelAll) labelAll.checked = true;
                        if (dataAll) dataAll.checked = true;
                    });
                }
            },
            // 更新"全部"复选框状态
            updateAllCheckbox: function () {
                const allCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
                const allChecked = Array.from(allCheckboxes).every(function (cb) {
                    return cb.checked;
                });
                const allCheckbox = document.getElementById('all');
                if (allCheckbox) {
                    allCheckbox.checked = allChecked;
                }
            },
            // 更新区域"全部"复选框状态
            updateSectionAllCheckbox: function () {
                // 更新按钮区域
                const buttonCheckboxes = document.querySelectorAll('#button-container input[type="checkbox"][name="privilege"]');
                const buttonAllChecked = buttonCheckboxes.length > 0 && Array.from(buttonCheckboxes).every(function (cb) {
                    return cb.checked;
                });
                const buttonAll = document.getElementById('button-all');
                if (buttonAll) {
                    buttonAll.checked = buttonAllChecked;
                }
                
                // 更新标签区域
                const labelCheckboxes = document.querySelectorAll('#label-container input[type="checkbox"][name="privilege"]');
                const labelAllChecked = labelCheckboxes.length > 0 && Array.from(labelCheckboxes).every(function (cb) {
                    return cb.checked;
                });
                const labelAll = document.getElementById('label-all');
                if (labelAll) {
                    labelAll.checked = labelAllChecked;
                }
 
                // 更新数据区域
                const dataCheckboxes = document.querySelectorAll('#data-container input[type="checkbox"][name="privilege"]');
                const dataAllChecked = dataCheckboxes.length > 0 && Array.from(dataCheckboxes).every(function (cb) {
                    return cb.checked;
                });
                const dataAll = document.getElementById('data-all');
                if (dataAll) {
                    dataAll.checked = dataAllChecked;
                }
            },
            // 保存权限
            savePrivileges: function () {
                const self = this;
                // 获取所有选中的复选框
                const checkedCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]:checked');
                
                // 构建请求数据
                const pIds = [];
                checkedCheckboxes.forEach(function (checkbox) {
                    pIds.push({
                        pId: checkbox.value
                    });
                });
                
                const requestData = {
                    pgId: vc.component.pgId,
                    parentId: vc.component.pId,
                    pIds: pIds
                };
                
                // 调用保存接口
                vc.http.apiPost('/add.privilege.PrivilegeGroup',
                    requestData,
                    {},
                    function (json) {
                        try {
                            // 判断 json 的类型,如果已经是对象则直接使用,否则进行解析
                            let result = typeof json === 'string' ? JSON.parse(json) : json;
                            vc.toast('保存成功');
                            console.log('保存成功:', result);
                            // 保存成功后返回上一页
                            setTimeout(function () {
                                vc.goBack();
                            }, 1000);
                        } catch (e) {
                            console.error('解析响应失败:', e);
                            vc.toast('保存成功');
                            // 保存成功后返回上一页
                            setTimeout(function () {
                                vc.goBack();
                            }, 1000);
                        }
                    },
                    function (error) {
                        console.error('保存失败:', error);
                        vc.toast('保存失败');
                    }
                );
            }
        }
    });
})(window.vc);