public/pages/property/costDetail/edit/edit.js
@@ -1,10 +1,245 @@
/**
 编辑费用明细
 **/
(function (vc) {
    var $that = {};
    vc.extends({
        data: {
            fundType: 'repair', // 默认选中维修资金
            fundSystemType: 'out' // 默认选中系统外
            costDetailInfo: {
                costDetailId: '',
                flowCode: '',
                date: '',
                communityName: '',
                communityCode: '',
                projectContent: '',
                managementAmount: '',
                managementStamped: '1',
                committeeAmount: '',
                appraisalAmount: '',
                committeeStamped: '1',
                approvalDepartment: '',
                fundTypeLevel1: '',
                fundTypeLevel2: '',
                buildingType: '1',
                maintenanceType: '',
                fundType: 'repair', // 默认选中维修资金
                fundSystemType: 'out' // 默认选中系统外
            }
        },
        _initMethod: function () {
            $that = vc.component;
            // 从URL获取costDetailId
            var costDetailId = vc.getParam('costDetailId');
            if (costDetailId) {
                $that.costDetailInfo.costDetailId = costDetailId;
                $that._loadCostDetail();
            } else {
                vc.toast('缺少费用明细ID');
                setTimeout(function() {
                    vc.goBack();
                }, 1500);
            }
        },
        methods: {
            _loadCostDetail: function () {
                // 查询所有数据,然后找到匹配的记录
                var params = {
                    communityName: vc.getCurrentCommunity().name,
                    page: 1,
                    row: 1000  // 查询足够多的数据以找到目标记录
                };
                var param = {
                    params: params
                };
                console.log('开始加载费用明细,ID:', $that.costDetailInfo.costDetailId);
                vc.http.apiGet('/maintenancePayment/queryMaintenancePayment',
                    param,
                    function (json, res) {
                        try {
                            var _json = JSON.parse(json);
                            console.log('API返回数据:', _json);
                            if (_json.code === 0 && _json.data) {
                                var records = Array.isArray(_json.data) ? _json.data : [];
                                console.log('查询到记录数:', records.length);
                                // 查找匹配的记录 - 支持字符串和数字类型的ID比较
                                var foundItem = null;
                                var targetId = String($that.costDetailInfo.costDetailId);
                                for (var i = 0; i < records.length; i++) {
                                    var recordId = String(records[i].id);
                                    console.log('比较ID: 目标=' + targetId + ', 记录=' + recordId);
                                    if (recordId === targetId || records[i].id == $that.costDetailInfo.costDetailId) {
                                        foundItem = records[i];
                                        console.log('找到匹配记录:', foundItem);
                                        break;
                                    }
                                }
                                if (foundItem) {
                                    $that._fillFormData(foundItem);
                                    console.log('表单数据已填充:', $that.costDetailInfo);
                                } else {
                                    console.error('未找到匹配的记录,目标ID:', $that.costDetailInfo.costDetailId);
                                    vc.toast('未找到该费用明细,ID: ' + $that.costDetailInfo.costDetailId);
                                    setTimeout(function() {
                                        vc.goBack();
                                    }, 1500);
                                }
                            } else {
                                console.error('API返回错误:', _json);
                                vc.toast('未找到该费用明细');
                                setTimeout(function() {
                                    vc.goBack();
                                }, 1500);
                            }
                        } catch (e) {
                            console.error('数据解析失败:', e);
                            vc.toast('数据解析失败: ' + e.message);
                        }
                    },
                    function (errInfo, error) {
                        console.error('请求失败:', errInfo, error);
                        vc.toast('请求失败,请检查网络连接');
                    }
                );
            },
            _fillFormData: function (item) {
                console.log('开始填充表单数据,原始数据:', item);
                // 填充表单数据
                var dateStr = '';
                if (item.date) {
                    dateStr = item.date.substring(0, 10); // 取日期部分 yyyy-mm-dd
                } else if (item.year && item.month) {
                    var monthStr = item.month < 10 ? '0' + item.month : String(item.month);
                    var dayStr = item.day ? (item.day < 10 ? '0' + item.day : String(item.day)) : '01';
                    dateStr = item.year + '-' + monthStr + '-' + dayStr;
                }
                // 使用Vue.set或者直接赋值来确保响应式更新
                vc.component.costDetailInfo.flowCode = item.flowNumber || '';
                vc.component.costDetailInfo.date = dateStr;
                vc.component.costDetailInfo.communityName = item.projectName || '';
                vc.component.costDetailInfo.communityCode = item.projectCode || '';
                vc.component.costDetailInfo.projectContent = item.projectContent || '';
                vc.component.costDetailInfo.managementAmount = item.managementOfficeAmount || '';
                vc.component.costDetailInfo.managementStamped = (item.managementOfficeSeal === '是' || item.managementOfficeSeal === '1') ? '1' : '0';
                vc.component.costDetailInfo.committeeAmount = item.ownersCommitteeAmount || '';
                vc.component.costDetailInfo.appraisalAmount = item.auditAmount || '';
                vc.component.costDetailInfo.committeeStamped = (item.ownersCommitteeSeal === '是' || item.ownersCommitteeSeal === '1') ? '1' : '0';
                vc.component.costDetailInfo.approvalDepartment = item.reportDepartment || '';
                vc.component.costDetailInfo.fundTypeLevel1 = item.fundTypeLevel1 || '';
                vc.component.costDetailInfo.fundTypeLevel2 = item.fundTypeLevel2 || '';
                // 处理buildingType字段
                var buildingType = item.buildingOrAll || '1';
                if (buildingType === '全体' || buildingType === '2' || buildingType === 2) {
                    vc.component.costDetailInfo.buildingType = '全体';
                } else {
                    vc.component.costDetailInfo.buildingType = '1';
                }
                vc.component.costDetailInfo.maintenanceType = item.maintenanceType || '';
                // 设置基金类型
                if (item.fundTypeLevel1) {
                    vc.component.costDetailInfo.fundType = (item.fundTypeLevel1 === '维修资金' || item.fundTypeLevel1 === '1') ? 'repair' : 'public';
                } else {
                    vc.component.costDetailInfo.fundType = 'repair'; // 默认值
                }
                if (item.fundTypeLevel2) {
                    vc.component.costDetailInfo.fundSystemType = (item.fundTypeLevel2 === '系统外' || item.fundTypeLevel2 === 'out') ? 'out' : 'in';
                } else {
                    vc.component.costDetailInfo.fundSystemType = 'out'; // 默认值
                }
                console.log('表单数据填充完成:', vc.component.costDetailInfo);
            },
            _saveCostDetail: function () {
                // 验证必填字段
                if (!$that.costDetailInfo.flowCode) {
                    vc.toast('请输入流转编码');
                    return;
                }
                if (!$that.costDetailInfo.date) {
                    vc.toast('请选择日期');
                    return;
                }
                if (!$that.costDetailInfo.communityName) {
                    vc.toast('请输入小区名称');
                    return;
                }
                if (!$that.costDetailInfo.communityCode) {
                    vc.toast('请输入小区编码');
                    return;
                }
                // 构建保存数据
                var saveData = {
                    id: $that.costDetailInfo.costDetailId,
                    flowNumber: $that.costDetailInfo.flowCode,
                    date: $that.costDetailInfo.date,
                    projectName: $that.costDetailInfo.communityName,
                    projectCode: $that.costDetailInfo.communityCode,
                    projectContent: $that.costDetailInfo.projectContent,
                    managementOfficeAmount: parseFloat($that.costDetailInfo.managementAmount) || 0,
                    managementOfficeSeal: $that.costDetailInfo.managementStamped === '1' ? '是' : '否',
                    ownersCommitteeAmount: parseFloat($that.costDetailInfo.committeeAmount) || 0,
                    auditAmount: parseFloat($that.costDetailInfo.appraisalAmount) || 0,
                    ownersCommitteeSeal: $that.costDetailInfo.committeeStamped === '1' ? '是' : '否',
                    reportDepartment: $that.costDetailInfo.approvalDepartment || '',
                    fundTypeLevel1: $that.costDetailInfo.fundType === 'repair' ? '维修资金' : '公共收益',
                    fundTypeLevel2: $that.costDetailInfo.fundType === 'public' ? ($that.costDetailInfo.fundSystemType === 'out' ? '系统外' : '系统内') : '',
                    buildingOrAll: $that.costDetailInfo.buildingType === '全体' ? '全体' : ($that.costDetailInfo.buildingType || '1'),
                    maintenanceType: $that.costDetailInfo.maintenanceType || '',
                    communityId: vc.getCurrentCommunity().communityId
                };
                // 解析日期
                if (saveData.date) {
                    var dateParts = saveData.date.split('-');
                    if (dateParts.length >= 2) {
                        saveData.year = parseInt(dateParts[0]);
                        saveData.month = parseInt(dateParts[1]);
                        if (dateParts.length >= 3) {
                            saveData.day = parseInt(dateParts[2]);
                        }
                    }
                }
                // 调用更新API
                vc.http.apiPost('/maintenancePayment/updateMaintenancePayment',
                    JSON.stringify(saveData), {
                        emulateJSON: true
                    },
                    function (json, res) {
                        try {
                            var _json = JSON.parse(json);
                            if (_json.code === 0) {
                                vc.toast('保存成功');
                                setTimeout(function() {
                                    vc.goBack();
                                }, 1000);
                            } else {
                                vc.toast(_json.msg || '保存失败');
                            }
                        } catch (e) {
                            vc.toast('保存失败');
                        }
                    },
                    function (errInfo, error) {
                        vc.toast('保存失败,请检查网络连接');
                    }
                );
            },
            _cancel: function () {
                vc.goBack();
            }
        }
    });
})(window.vc);