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
/**
 费用明细表
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 15;
    var $that = {};
    vc.extends({
        data: {
            costDetailInfo: {
                costDetails: [],
                communityList: [],
                paginationInfo: {
                    currentPage: 1,
                    rows: DEFAULT_ROWS,
                    total: 1,
                    dataCount: 0,
                    pageList: []
                },
                jumpPage: 1,
                conditions: {
                    date: '',
                    communityName: '',
                    communityCode: '',
                    communityId: vc.getCurrentCommunity().communityId,
                    flowNumber: '',
                    projectCode: '',
                    year: '',
                    projectName: '',
                    _communityName: vc.getCurrentCommunity().name
                }
            }
        },
        _initMethod: function () {
            $that = vc.component;
            $that.costDetailInfo.conditions.communityId = vc.getCurrentCommunity().communityId;
            $that.costDetailInfo.conditions._communityName = vc.getCurrentCommunity().name;
            $that._loadCommunityList();
            $that._initDate();
        },
        _initEvent: function () {
            vc.on('costDetail', 'listCostDetail', function (_param) {
                $that._listCostDetails(DEFAULT_PAGE, DEFAULT_ROWS);
            });
        },
        methods: {
            _initDate: function () {
                // 年份输入框不需要特殊初始化,使用 v-model 双向绑定即可
            },
            _loadCommunityList: function () {
                // 获取所有小区列表
                var communityInfos = vc.getCommunitys();
                if (communityInfos && Array.isArray(communityInfos) && communityInfos.length > 0) {
                    $that.costDetailInfo.communityList = communityInfos;
                    // 默认选择第一个小区
                    $that.costDetailInfo.conditions.communityName = communityInfos[0].name;
                } else {
                    // 如果获取不到列表,使用当前小区
                    var currentCommunity = vc.getCurrentCommunity();
                    if (currentCommunity && currentCommunity.name) {
                        $that.costDetailInfo.communityList = [currentCommunity];
                        $that.costDetailInfo.conditions.communityName = currentCommunity.name;
                    }
                }
                // 加载数据
                $that._listCostDetails(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _listCostDetails: function (_page, _rows) {
                // 使用选中的小区名称,如果没有选中则使用当前小区名称
                var selectedCommunityName = $that.costDetailInfo.conditions.communityName || vc.getCurrentCommunity().name;
                var params = {
                    communityName: selectedCommunityName,
                    page: _page,
                    row: _rows
                };
                
                // 添加可选查询参数
                if ($that.costDetailInfo.conditions.flowNumber) {
                    params.flowNumber = $that.costDetailInfo.conditions.flowNumber;
                }
                if ($that.costDetailInfo.conditions.projectCode) {
                    params.projectCode = $that.costDetailInfo.conditions.projectCode;
                }
                if ($that.costDetailInfo.conditions.year) {
                    params.year = parseInt($that.costDetailInfo.conditions.year);
                }
                if ($that.costDetailInfo.conditions.projectName) {
                    params.projectName = $that.costDetailInfo.conditions.projectName;
                }
                
                var param = {
                    params: params
                };
                
                vc.http.apiGet('/maintenancePayment/queryMaintenancePayment',
                    param,
                    function (json, res) {
                        try {
                            var _json = JSON.parse(json);
                            
                            if (_json.code === 0 && _json.data) {
                                var records = Array.isArray(_json.data) ? _json.data : [];
                                
                                var mappedRecords = records.map(function(item) {
                                    var dateStr = '';
                                    if (item.date) {
                                        dateStr = item.date.substring(0, 7);
                                    } else if (item.year && item.month) {
                                        var monthStr = item.month < 10 ? '0' + item.month : String(item.month);
                                        dateStr = item.year + '-' + monthStr;
                                    }
                                    
                                    return {
                                        flowCode: item.flowNumber || '--',
                                        communityCode: item.projectCode || '--',
                                        communityName: item.projectName || '--',
                                        date: dateStr,
                                        projectContent: item.projectContent || '--',
                                        managementAmount: item.managementOfficeAmount || '--',
                                        managementStamped: item.managementOfficeSeal === '是' ? '1' : '0',
                                        committeeAmount: item.ownersCommitteeAmount || '--',
                                        appraisalAmount: item.auditAmount || '--',
                                        committeeStamped: item.ownersCommitteeSeal === '是' ? '1' : '0',
                                        approvalDepartment: item.reportDepartment || '--',
                                        fundTypeLevel1: item.fundTypeLevel1 || '--',
                                        fundTypeLevel2: item.fundTypeLevel2 || '--',
                                        buildingType: item.buildingOrAll || '--',
                                        maintenanceType: item.maintenanceType || '--',
                                        costDetailId: item.id,
                                        _originalData: item
                                    };
                                });
                                
                                var total = _json.total || 0;
                                var totalPages = _json.records || 1;
                                
                                $that.costDetailInfo.paginationInfo.dataCount = total;
                                $that.costDetailInfo.paginationInfo.total = totalPages;
                                $that.costDetailInfo.costDetails = mappedRecords;
                                $that.costDetailInfo.paginationInfo.currentPage = _page;
                                $that._freshPageList();
                            } else {
                                console.error('接口返回错误:', _json.msg || '未知错误', _json);
                                vc.toast(_json.msg || '查询失败');
                                $that.costDetailInfo.costDetails = [];
                                $that.costDetailInfo.paginationInfo.dataCount = 0;
                                $that.costDetailInfo.paginationInfo.total = 1;
                                $that._freshPageList();
                            }
                        } catch (e) {
                            vc.toast('数据解析失败');
                            $that.costDetailInfo.costDetails = [];
                            $that.costDetailInfo.paginationInfo.dataCount = 0;
                            $that.costDetailInfo.paginationInfo.total = 1;
                            $that._freshPageList();
                        }
                    },
                    function (errInfo, error) {
                        vc.toast('请求失败,请检查网络连接');
                        $that.costDetailInfo.costDetails = [];
                        $that.costDetailInfo.paginationInfo.dataCount = 0;
                        $that.costDetailInfo.paginationInfo.total = 1;
                        $that._freshPageList();
                    }
                );
            },
            _freshPageList: function () {
                var currentPage = $that.costDetailInfo.paginationInfo.currentPage;
                var total = $that.costDetailInfo.paginationInfo.total;
                var pageList = [];
                
                if (total <= 7) {
                    for (var i = 1; i <= total; i++) {
                        pageList.push({
                            page: i,
                            pageView: i,
                            currentPage: i == currentPage
                        });
                    }
                } else {
                    if (currentPage <= 4) {
                        for (var i = 1; i <= 5; i++) {
                            pageList.push({
                                page: i,
                                pageView: i,
                                currentPage: i == currentPage
                            });
                        }
                        pageList.push({
                            page: 0,
                            pageView: '...',
                            currentPage: false
                        });
                        pageList.push({
                            page: total,
                            pageView: total,
                            currentPage: false
                        });
                    } else if (currentPage >= total - 3) {
                        pageList.push({
                            page: 1,
                            pageView: 1,
                            currentPage: false
                        });
                        pageList.push({
                            page: 0,
                            pageView: '...',
                            currentPage: false
                        });
                        for (var i = total - 4; i <= total; i++) {
                            pageList.push({
                                page: i,
                                pageView: i,
                                currentPage: i == currentPage
                            });
                        }
                    } else {
                        pageList.push({
                            page: 1,
                            pageView: 1,
                            currentPage: false
                        });
                        pageList.push({
                            page: 0,
                            pageView: '...',
                            currentPage: false
                        });
                        for (var i = currentPage - 1; i <= currentPage + 1; i++) {
                            pageList.push({
                                page: i,
                                pageView: i,
                                currentPage: i == currentPage
                            });
                        }
                        pageList.push({
                            page: 0,
                            pageView: '...',
                            currentPage: false
                        });
                        pageList.push({
                            page: total,
                            pageView: total,
                            currentPage: false
                        });
                    }
                }
                
                $that.costDetailInfo.paginationInfo.pageList = pageList;
            },
            _queryCostDetails: function () {
                $that._listCostDetails(DEFAULT_PAGE, $that.costDetailInfo.paginationInfo.rows);
            },
            _resetQuery: function () {
                // 重置时恢复为第一个小区
                if ($that.costDetailInfo.communityList && $that.costDetailInfo.communityList.length > 0) {
                    $that.costDetailInfo.conditions.communityName = $that.costDetailInfo.communityList[0].name;
                } else {
                    $that.costDetailInfo.conditions.communityName = '';
                }
                $that.costDetailInfo.conditions.communityCode = '';
                $that.costDetailInfo.conditions.flowNumber = '';
                $that.costDetailInfo.conditions.projectCode = '';
                $that.costDetailInfo.conditions.year = '';
                $that.costDetailInfo.conditions.projectName = '';
                $('.queryDate').val('');
                $that._listCostDetails(DEFAULT_PAGE, $that.costDetailInfo.paginationInfo.rows);
            },
            _changePageSize: function () {
                $that._listCostDetails(DEFAULT_PAGE, $that.costDetailInfo.paginationInfo.rows);
            },
            _goToPage: function (_page) {
                if (!_page || _page < 1 || _page > $that.costDetailInfo.paginationInfo.total) {
                    return;
                }
                $that._listCostDetails(_page, $that.costDetailInfo.paginationInfo.rows);
            },
            _jumpToPage: function () {
                var page = parseInt($that.costDetailInfo.jumpPage);
                if (isNaN(page) || page < 1) {
                    page = 1;
                }
                if (page > $that.costDetailInfo.paginationInfo.total) {
                    page = $that.costDetailInfo.paginationInfo.total;
                }
                $that._listCostDetails(page, $that.costDetailInfo.paginationInfo.rows);
            },
            _downloadTemplate: function () {
                // 下载模板
                vc.toast('模板下载功能');
            },
            _importCostDetail: function () {
                // 打开本地文件
                vc.toast('费用导入功能');
            },
            _addCostDetail: function () {
                // 跳转到添加页面
                vc.jumpToPage('/#/pages/property/costDetail/add');
            },
            _viewCostDetail: function (_item) {
                // 查看详情
                vc.jumpToPage('/#/pages/property/costDetail/detail?costDetailId=' + _item.costDetailId);
            },
            _viewMore: function (_item) {
                // 查看更多
                vc.jumpToPage('/#/pages/property/costDetail/more?costDetailId=' + _item.costDetailId);
            },
            _editCostDetail: function (_item) {
                // 编辑
                vc.jumpToPage('/#/pages/property/costDetail/edit?costDetailId=' + _item.costDetailId);
            },
            _deleteCostDetail: function (_item) {
                if (!_item || !_item.costDetailId) {
                    vc.toast('删除失败:缺少必要的数据');
                    return;
                }
                if (typeof vc.confirm === 'function') {
                    vc.confirm('确定要删除这条费用明细吗?', function () {
                        var param = {
                            id: _item.costDetailId,
                            communityId: vc.getCurrentCommunity().communityId
                        };
                        vc.http.apiPost('/maintenancePayment/deleteMaintenancePayment',
                            JSON.stringify(param), {
                                headers: {
                                    'Content-Type': 'application/json'
                                }
                            },
                            function (json, res) {
                                try {
                                    var _json = JSON.parse(json);
                                    if (_json.code === 0 || _json.code === '0') {
                                        vc.toast(_json.msg || '删除成功');
                                        $that._listCostDetails($that.costDetailInfo.paginationInfo.currentPage, $that.costDetailInfo.paginationInfo.rows);
                                    } else {
                                        vc.toast(_json.msg || '删除失败');
                                    }
                                } catch (e) {
                                    console.error('删除响应解析失败:', e);
                                    vc.toast('删除失败,请重试');
                                }
                            },
                            function (errInfo, error) {
                                console.error('删除请求失败:', errInfo, error);
                                vc.toast(errInfo || '删除失败,请检查网络连接');
                            }
                        );
                    });
                } else {
                    // 如果 vc.confirm 不存在,使用 window.confirm 作为后备方案
                    if (window.confirm('确定要删除这条费用明细吗?')) {
                        var param = {
                            id: _item.costDetailId,
                            communityId: vc.getCurrentCommunity().communityId
                        };
                        vc.http.apiPost('/maintenancePayment/deleteMaintenancePayment',
                            JSON.stringify(param), {
                                headers: {
                                    'Content-Type': 'application/json'
                                }
                            },
                            function (json, res) {
                                try {
                                    var _json = JSON.parse(json);
                                    if (_json.code === 0 || _json.code === '0') {
                                        vc.toast(_json.msg || '删除成功');
                                        $that._listCostDetails($that.costDetailInfo.paginationInfo.currentPage, $that.costDetailInfo.paginationInfo.rows);
                                    } else {
                                        vc.toast(_json.msg || '删除失败');
                                    }
                                } catch (e) {
                                    console.error('删除响应解析失败:', e);
                                    vc.toast('删除失败,请重试');
                                }
                            },
                            function (errInfo, error) {
                                console.error('删除请求失败:', errInfo, error);
                                vc.toast(errInfo || '删除失败,请检查网络连接');
                            }
                        );
                    }
                }
            }
        }
    });
})(window.vc);