import _ from 'lodash'

/**
 * 子表赋值 是否超标
 * @author fengjiansheng
 * @data 2021-08-22
 */
export default {
    execute: function () {
        console.log("s1 SetIsOverStandard")
        if (arguments && arguments[0] && arguments[0].length === 3 && arguments[1]) {
            // TODO 参数校验
            let context = arguments[1]
            let targetSubField = arguments[0][0];
            let tmpField = targetSubField.FieldCode
            let comp1 = arguments[0][1].getResult().Value;
            let comp2 = arguments[0][2].getResult().Value;
            let subTableObject = context.getTableType(targetSubField.FieldTableCode)
            let oldIndex = context.getSubFocus()
            let focusData = context.getAllSubData()[subTableObject.title][oldIndex].getData()
            if ((!_.isEmpty(comp1) || _.isNumber(comp1) ) && ( _.isEmpty(comp2) || (_.isNumber(comp2))) && (_.isNumber(_.toNumber(comp1)) && (_.isNumber(_.toNumber(comp2))))) {
                let param = {
                    "limit": 1,
                    "offset": 0,
                    "pagination": true,
                    "tableName": "MD_SF",
                    "code": _.toNumber(comp1) > _.toNumber(comp2) ? "1" : "0",
                    "queryDataStructure": "ALL"
                };
                this.getMDObject(param, (data) => {
                    let newIndex = context.getSubFocus();
                    if (oldIndex !== newIndex) {
                        context.setSubFocus(oldIndex)
                    }
                    focusData[tmpField]._field_old_value_ = focusData[tmpField].value = data.rows[0]
                    if (oldIndex !== newIndex) {
                        context.setSubFocus(newIndex)
                    }
                    context.refreshSubDataRow(subTableObject.title)
                }, (resp) => {
                    GAMS.Common.messagePrompt("子表赋值：是否超标执行失败，请联系管理员")
                    console.error("子表赋值：是否超标执行失败" + resp)
                });
            } else {
                focusData[tmpField]._field_old_value_ = focusData[tmpField].value = null;
                context.refreshSubDataRow(subTableObject.title);
            }
        }

    },
    getMDObject(param, callBack, errorCallBack) {
        GMS.$http.post("/baseData/data/list", param).then(data => {
            callBack(data.data);
        }).catch((resp) => {
            errorCallBack(resp);
        });
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Void;
    }
}
