import _ from 'lodash'
import FormulaConstants from "@/formula/FormulaConstants";

/**
 * 子表赋值公式
 * 目前只拓展了一个部门经济分类子表赋值,后续可进行自由拓展。
 * @author fengjiansheng
 * @data 2021-09-13
 */
export default {
    execute: function () {
        console.log("s1 SetSubFieldValue")
        if (arguments && arguments[0] && arguments[0].length > 1 && arguments[1]) {
            // TODO 参数校验
            let context = arguments[1]
            let targetField = arguments[0][0]
            let targetObject = context.getTableType(targetField.FieldTableCode)
            let valueType = arguments[0][1]
            // TODO 入参校验
            if (valueType instanceof StaticDataNode && _.isNumber(valueType.StaticValue)) {
                valueType = valueType.StaticValue
            } else {
                console.error("SetSubFieldValue公式配置错误,第二个入参类型有误")
                return null
            }
            if (targetObject.type === "sub") {
                let subTableFocusOldIndex = context.getSubFocus()
                let subTableFocusData = context.getAllSubData()[targetObject.title][subTableFocusOldIndex].getData()
                let oldIndex = context.getSubFocus()
                switch (valueType) {
                    case  FormulaConstants.billFiled.BMJJFL: {
                        let comp1 = arguments[0][2].getResult().Value;
                        let comp2 = arguments[0][3].getResult().Value;
                        let comp3 = arguments[0][4].getResult().Value;
                        let comp4 = arguments[0][5].getResult().Value;
                        if ((!_.isEmpty(comp1) || _.isNumber(comp1)) && (!_.isEmpty(comp2) || _.isNumber(comp2)) && (!_.isEmpty(comp3) || (_.isNumber(comp3)) && (!_.isEmpty(comp4) || (_.isNumber(comp4))))) {
                            if (!_.isEmpty(comp3) &&  !(_.isEqual(subTableFocusData[arguments[0][4].FieldCode.split(".")[0]]._field_old_value_, subTableFocusData[arguments[0][4].FieldCode.split(".")[0]].value))) {
                                console.log("s2 SetSubField api")
                                this.getMDObject({
                                    "limit": 1,
                                    "offset": 0,
                                    "pagination": true,
                                    "tableName": comp2,
                                    "code": comp3,
                                    "queryDataStructure": "ALL"
                                }, data => {
                                    let param = {
                                        "limit": 1,
                                        "offset": 0,
                                        "pagination": true,
                                        "tableName": comp1,
                                        "code": data.rows[0][comp4].split("||")[0],
                                        "queryDataStructure": "ALL"
                                    };
                                    this.getMDObject(param, data => {
                                        let newIndex = context.getSubFocus();
                                        if (oldIndex !== newIndex) {
                                            context.setSubFocus(oldIndex)
                                        }
                                        console.log("s3 setValue")
                                        subTableFocusData[targetField.FieldCode]._field_old_value_ = subTableFocusData[targetField.FieldCode].value = data.rows[0]
                                        if (oldIndex !== newIndex) {
                                            context.setSubFocus(newIndex)
                                        }
                                        context.refreshSubDataRow(targetObject.title)
                                    }, resp => {
                                        console.error("子表赋值：SetSubFieldValue执行失败 resp:" + resp)
                                    });
                                }, resp => {
                                    console.error("子表赋值：SetSubFieldValue执行失败 resp:" + resp)
                                });
                            }
                        } else {
                            subTableFocusData[targetField.FieldCode]._field_old_value_ = subTableFocusData[targetField.FieldCode].value = null;
                            context.refreshSubDataRow(targetObject.title);
                        }
                        break
                    }
                    case FormulaConstants.billFiled.NULL: {
                        subTableFocusData[targetField.FieldCode]._field_old_value_ = subTableFocusData[targetField.FieldCode].value = null;
                        context.refreshSubDataRow(targetObject.title);
                        break
                    }
                    default : {
                        console.log("")
                    }
                }
            } else {
                console.error("SetSubFieldValue公式配置错误,暂不支持主表字段赋值")
                return null
            }
        }
    },
    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;
    }
}
