
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) {
                $('#curMonthWater').shrTextField('disable');//或者设为enable  disable
                $('#curMonthElecDegrees').shrTextField('disable');//或者设为enable  disable
                $('#waterBaseRead').shrTextField('option','required',true); 
                $('#elecBaseRead').shrTextField('option','required',true); 
            }else{
                $('#waterBaseRead').shrTextField('option','required',false); 
                $('#elecBaseRead').shrTextField('option','required',false); 
            }
    },
    /**
     * 保存
     */
    saveAction: function (event) {
        var _self = this;
        if (_self.validate() && _self.verify()) {
            //计算本次用水电数
               _self.calHydropower();
                _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);
        }

    },


 

    /**
    *  计算当月水电
    * 20251125
    */
    calHydropower(){
         var calcInterval = $('#calcInterval').shrCheckbox('getValue');//是否取消自动计算当月水电
            if (calcInterval == 'false' || calcInterval == false) {
                 var totalWaterTons = $("#totalWaterTons").val(); //本次水表读数(吨)
                 var totalElecDegrees = $("#totalElecDegrees").val(); //本次电表读数
                 var waterBaseRead = $("#waterBaseRead").val(); //水基准读数
                 var elecBaseRead = $("#elecBaseRead").val(); //电基准读数
               // 直接转换（省略封装函数，适合确定变量是数字字符串的场景）
                const totalWaterTonsNum = Number(totalWaterTons) || 0;
                const waterBaseReadNum = Number(waterBaseRead) || 0;
                const totalElecDegreesNum = Number(totalElecDegrees) || 0;
                const elecBaseReadNum = Number(elecBaseRead) || 0;
                
                // 计算逻辑不变
                const waterVal = totalWaterTonsNum - waterBaseReadNum >= 0 
                  ? totalWaterTonsNum - waterBaseReadNum 
                  : totalWaterTonsNum + 100000 - waterBaseReadNum;
                
                const elecVal = totalElecDegreesNum - elecBaseReadNum >= 0 
                  ? totalElecDegreesNum - elecBaseReadNum 
                  : totalElecDegreesNum + 100000 - elecBaseReadNum;
                $('#curMonthWater').shrTextField('setValue',waterVal );//或者设为enable  disable
                //当月电度数
                $('#curMonthElecDegrees').shrTextField('setValue',elecVal );//或者设为enable  disable
                
            }  

        
    },

    
    /**
     * 监听事件
     */
    changeAction() {
        var _self = this;
        $('#waterBaseRead').change(function () {
                 //计算本次用水电数
               _self.calHydropower();
        });

        $('#elecBaseRead').change(function () {
                 //计算本次用水电数
               _self.calHydropower();
        });

        $('#dormitory').shrPromptBox('option', {
            onchange: function (e, value) {
                var data = value.current || [];
                var number = data.number;
                $("#dormNumber").shrTextField('setValue', number);
                _self.getLastWaterAndEleVal(data);
            }
        });
        //用水总吨数监听事件
        $('#totalWaterTons').change(function () {
                 //计算本次用水电数
               _self.calHydropower();
           
        });
        //用电总度数监听事件
        $('#totalElecDegrees').change(function () {
                 //计算本次用水电数
               _self.calHydropower();
           
        });

        _self.getField("years").shrDateTimePicker("onChange",function(){
				_self.getLastWaterAndEleVal();
		});

        
        //是否取消自动计算当月水电
        $('#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);
                 $('#waterBaseRead').shrTextField('option','required',false); 
                $('#elecBaseRead').shrTextField('option','required',false); 
            } else {
                 //计算本次用水电数
               _self.calHydropower();
                $('#curMonthWater').shrTextField('disable');//或者设为enable  disable
                $('#curMonthElecDegrees').shrTextField('disable');//或者设为enable  disable
               $('#waterBaseRead').shrTextField('option', 'required', true);
                $('#elecBaseRead').shrTextField('option','required',true); 
            }
        });
      
    },


    //作废
    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: "getLastMWaterPower",
                param: {
                    years: years,
                    dormNumber: dormNumber,
                },
                async: false, // 设置为同步请求
                success: function (data) {
                    $("#lastMeterDate").shrTextField('setValue', data.createDate);
                    $("#lastWaterVal").shrTextField('setValue', data.water);
                    $("#lastEleVal").shrTextField('setValue', data.power);
                    console.log(data);
                }, error: function (response) {
                    shr.showError({
                        message: response
                    });
                }
            });
        } 
        return true;
    }





    ,getLastWaterAndEleVal(data) {
        var years = $("#years").val(); //年月
        var dormNumber =  "";
        if(data == null || data == undefined){
             dormNumber = $("#dormitory").shrPromptBox('getValue').id;//宿舍编码
        }else{
            dormNumber = data.id;
        }
       
            if (years == null || dormNumber == "") {
                shr.showError({
                    message: "请先填写宿舍！"
                });
                return;
            }
            _self.remoteCall({
                type: "post",
                method: "getLastMWaterPower",
                param: {
                    years: years,
                    dormNumber: dormNumber
                  
                },
                async: false, // 设置为同步请求
                success: function (data) {
                    if(data){
                        $("#waterBaseRead").shrTextField('setValue', data.water);
                        $("#elecBaseRead").shrTextField('setValue', data.power);
                         $("#lastMeterDate").shrDateTimePicker('setValue', data.createDate);
                    $("#lastWaterVal").shrTextField('setValue', data.water);
                    $("#lastEleVal").shrTextField('setValue', data.power);
                    }
                    console.log(data);
                }, error: function (response) {
                    shr.showError({
                        message: response
                    });
                }
            });
         },
         
   
});

