
shr.defineClass("shr.custom.WaterPowerMeterReadingEdit", shr.framework.Edit, {
    initalizeDOM: function () {
        var _self = this;
        shr.custom.WaterPowerMeterReadingEdit.superClass.initalizeDOM.call(this);
        _self.getLastMonth();
        _self.changeAction()
        // $('#curMonthWater').shrTextField('disable');//或者设为enable  disable
        // $('#curMonthElecDegrees').shrTextField('disable');//或者设为enable  disable
        var calcInterval = $('#calcInterval').shrCheckbox('getValue');//是否取消自动计算当月水电
            if (calcInterval != true) {
             _self.getThisMonthWaterPowerTonsAction();
                $('#curMonthWater').shrTextField('disable');//或者设为enable  disable
                $('#curMonthElecDegrees').shrTextField('disable');//或者设为enable  disable
            }
    },
    /**
     * 保存
     */
    saveAction: function (event) {
        var _self = this;
        if (_self.validate() && _self.verify()) {
            if (_self.getThisMonthWaterPowerTonsAction()) {
                _self.doSave(event, 'save');
            };
        } else {
            if (_self != top) {// in iframe
                shr.setIframeHeight(window.name);
            }

        }
    },
    /**
     * 
     * @returns 获取上个月月份
     */
    getLastMonth() {
        if (this.getOperateState().toUpperCase() == "ADDNEW") {
            const currentDate = new Date();
            const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1);
            $("#years").shrDateTimePicker('setValue', lastMonth);
        }

    },
    /**
     * 监听事件
     */
    changeAction() {
        var _self = this;
        $('#dormitory').shrPromptBox('option', {
            onchange: function (e, value) {
                var data = value.current || [];
                var number = data.number;
                $("#dormNumber").shrTextField('setValue', number);
            }
        });
        //用水总吨数监听事件
        $('#totalWaterTons').change(function () {
            _self.getThisMonthWaterPowerTonsAction();

        });
        //用电总度数监听事件
        $('#totalElecDegrees').change(function () {
            _self.getThisMonthWaterPowerTonsAction();
            // var years = $("#years").val(); //年月
            // var totalWaterTons = $("#totalWaterTons").val();//用水总吨数
            // var totalElecDegrees = $("#totalElecDegrees").val(); //用电总度数
            // var dormNumber = $("#dormNumber").val();//宿舍编码
            // var calcInterval = $('#calcInterval').shrCheckbox('getValue');//是否取消自动计算当月水电
            // if (calcInterval == false) {

            //     if (years == null || dormNumber == "") {
            //         shr.showError({
            //             message: "请先填写宿舍！！"
            //         });
            //         return;
            //     }
            //     _self.remoteCall({
            //         type: "post",
            //         method: "getThisMonthWaterPowerTons",
            //         param: {
            //             years: years,
            //             totalWaterTons: totalWaterTons,
            //             totalElecDegrees: totalElecDegrees,
            //             dormNumber: dormNumber,
            //             calcInterval: calcInterval,
            //         },
            //         success: function (data) {
            //             // $("#curMonthWater").shrTextField('setValue', data.curMonthWater);
            //             $("#curMonthElecDegrees").shrTextField('setValue', data.curMonthElecDegrees);
            //             console.log(data);
            //         }, error: function (response) {
            //             shr.showError({
            //                 message: response
            //             });
            //         }
            //     });
            // } else {
            //     $("#curMonthElecDegrees").shrTextField('setValue', null);
            // }
        });
      
        //是否取消自动计算当月水电
        $('#calcInterval').change(function () {
            $('#curMonthWater').shrTextField('enable');//或者设为enable  disable
            $('#curMonthElecDegrees').shrTextField('enable');//或者设为enable  disable
            var calcInterval = $('#calcInterval').shrCheckbox('getValue');//是否取消自动计算当月水电
            if (calcInterval == true) {
                $("#curMonthWater").shrTextField('setValue', null);
                $("#curMonthElecDegrees").shrTextField('setValue', null);

            } else {
                _self.getThisMonthWaterPowerTonsAction();
                $('#curMonthWater').shrTextField('disable');//或者设为enable  disable
                $('#curMonthElecDegrees').shrTextField('disable');//或者设为enable  disable
            }
        });
      
    },
    getThisMonthWaterPowerTonsAction() {
        var years = $("#years").val(); //年月
        var totalWaterTons = $("#totalWaterTons").val();//用水总吨数
        var totalElecDegrees = $("#totalElecDegrees").val(); //用电总度数
        var dormNumber = $("#dormNumber").val();//宿舍编码
        var calcInterval = $('#calcInterval').shrCheckbox('getValue');//是否取消自动计算当月水电
        if (calcInterval == false) {
            // if (years == null || dormNumber == "") {
            //     shr.showError({
            //         message: "请先填写宿舍！！"
            //     });
            //     return;
            // }
            _self.remoteCall({
                type: "post",
                method: "getThisMonthWaterPowerTons",
                param: {
                    years: years,
                    totalWaterTons: totalWaterTons,
                    totalElecDegrees: totalElecDegrees,
                    dormNumber: dormNumber,
                    calcInterval: calcInterval,
                },
                async: false, // 设置为同步请求
                success: function (data) {
                    $("#curMonthWater").shrTextField('setValue', data.curMonthWater);
                    $("#curMonthElecDegrees").shrTextField('setValue', data.curMonthElecDegrees);
                    console.log(data);
                }, error: function (response) {
                    shr.showError({
                        message: response
                    });
                }
            });
        } 
        return true;
    }
});

