export default {
    // 根据接待对象类别和宴请地点级别来获取对应宴请标准
    execute: function(){
        // debugger
        if(arguments && arguments[0] && arguments[0].length > 1 && arguments[1]){
            // bill
            let context = arguments[1]
            let allCode = []

            let flag = 0

            // 第一个为待赋值的字段，从第二个开始取,最后一个为要获取到的数据所在表名和字段
            for (let i = 1; i < arguments[0].length-2; i=i+2) {
                let targetField = arguments[0][i]
                let tableName = targetField.FieldTableCode
                let targetFieldCode = targetField.FieldCode
                let codeName = arguments[0][i+1].StaticValue
                let tableObj = context.getTableType(tableName)
                if (tableObj.type == "master") {
                    let masterData = context.getMasterData(tableObj.title)
                    let field = {
                        name: '',
                        code: ''
                    }
                    field.name = codeName
                    let fieldRelative = masterData.getValue(targetFieldCode)
                    if (fieldRelative && fieldRelative.objectcode) {
                        field.code = fieldRelative.objectcode
                        allCode.push(field)
                        flag = flag + 1
                    }
                } else if (tableObj.type == "sub") {
                    let index = context.getSubFocus(tableName)
                    let subData = context.getSubData(tableObj.title)
                    if(subData instanceof Array){
                        let field = {
                            name: '',
                            code: ''
                        }
                        field.name = codeName
                        let fieldRelative = subData[index].getValue(targetFieldCode)
                        if (fieldRelative && fieldRelative.objectcode) {
                            field.code = fieldRelative.objectcode
                            allCode.push(field)
                            flag = flag + 1
                        }
                    }
                }


                if (i == arguments[0].length-3) {
                    // 第一个字段，待赋值的字段
                    let resultTargetField = arguments[0][0]
                    // XX.XX
                    let linkId = resultTargetField.FieldTableCode + '.' + resultTargetField.FieldCode

                    let len = arguments[0].length - 1
                    // 获取的字段值
                    let mdFieldName = arguments[0][len].StaticValue.split(",")[0] 
                    // 基础数据表名
                    let mdTableName = arguments[0][len].StaticValue.split(",")[1]  
                    
                    if (flag > 1 && arguments[0].length >= 5) {
                        let refreshTableObj = context.getTableType(resultTargetField.FieldTableCode)
                        if (refreshTableObj.type == "sub") {
                            let len = context.getSubData(refreshTableObj.title).length
                            let allData = []
                            for (let i = 0; i < len; i++) {
                                let obj = context.getAllSubData()[refreshTableObj.title][i].getData()
                                allData[i] = obj
                            }
                            let index1 = context.getSubFocus(resultTargetField.FieldTableCode)
          
                            this.getMDObject(allCode,mdTableName,function(data){
                                let index2 = context.getSubFocus(resultTargetField.FieldTableCode)
                                for(const key in allData[index1]) {
                                    if (allData[index1][key]. _field_old_value_ && allData[index1][key]. _field_old_value_ !== 0 && allData[index1][key]. _field_old_value_ !== '') {
                                        allData[index1][key].value  = allData[index1][key]. _field_old_value_ 
                                        if (key == resultTargetField.FieldCode) {
                                            let data = context.getSubData(refreshTableObj.title)[index1]
                                            data.setValue(key, allData[index1][key]. _field_old_value_)
                                            context.refreshSubDataRow(refreshTableObj.title)
                                        }
                                    }
                                }
                                if (index2 == index1) {
                                    let bz = data.rows[0][mdFieldName]
                                    if(resultTargetField&&resultTargetField.dynamicNodeFactory&&resultTargetField.dynamicNodeFactory.methodJson){
                                        resultTargetField.dynamicNodeFactory.methodJson.SetFieldData('',linkId,bz,'')
                                    }
                                }
                            },function(){
                                GAMS.Common.messagePrompt('当前信息未配置')
                            });
                        }
                    }else{
                        return null
                    }
                }
            }
        }
    },
    // 获取基础数据
    getMDObject(allCode, mdTableName,callback,errorCallBack) {
        if (allCode && mdTableName) {
            let param = {
                tableName: mdTableName,
                queryDataStructure: "ALL" //返回的接口类型 
            }
            // 待查询字段
            for (let i = 0; i < allCode.length; i++) {
                param[allCode[i].name] = allCode[i].code
            }

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