// 将主表字段值付给子表字段
export default {
    execute: function(){
        console.log("进入拼接字符串前端公式");
        if(arguments && arguments[0] && arguments[1]){
            //选择的字段
            let _fields = arguments[0], context = arguments[1],
            result = '',
            targetField = arguments[0][0],
            targetObj = context.getTableType(targetField.FieldTableCode), 
            tableObj = context.getTableType(arguments[0][1].StaticValue);
            if (tableObj.type == "sub") {
                let _data = context.getSubData(tableObj.title)
                if(_data instanceof Array) {
					let _oldIndex = context.getSubFocus();
                    for (let rowIndex=0; rowIndex<_data.length;rowIndex++) {
						let item = _data[rowIndex];
                        for (var i = 2; i < _fields.length; i++) {
                            let obj = _fields[i], feildArray = null, itemData = '', itemObj = null, itemLink = null
                            if( obj instanceof BillDataNode) {
                                feildArray = obj.FieldCode.split(".")
                                itemObj = context.getTableType(obj.FieldTableCode) // 待拼接的每一项的对象类型
                                itemLink = obj.FieldTableCode + "." + obj.FieldCode
                                if (feildArray && feildArray.length == 2) {
                                    itemData = itemObj.type == "sub" ? item.getValue(feildArray[0]) : obj.dynamicNodeFactory.methodJson.GetFieldData('', itemLink, '')
                                    if (itemData) {
                                        if (itemData[feildArray[1]]) {
                                            result += itemData[feildArray[1]]
                                        } else {
                                            result += itemData
                                        }
                                    }
                                } else {
                                    result += itemObj.type == "sub" ? item.getValue(obj.FieldCode) : obj.dynamicNodeFactory.methodJson.GetFieldData('', itemLink, '')
                                }
                            } else if (obj instanceof StaticDataNode) {
                                result += obj.StaticValue
                            } else if (obj instanceof FunctionNode) {
                                // result += await this.getFuncResult(obj)
                            }
                        }
                        if (targetObj.type == "sub") {
							context.setSubFocus(rowIndex);
                            item.setValue(targetField.FieldCode, result)
                            result = ''
                        } else {
                            result += (_fields.length > 3 ? '; ' : ', ')
                        }
                    }
                    context.setSubFocus(_oldIndex);
                    context.refreshSubDataRow(tableObj.title)
                }
                if (result.endsWith(', ') || result.endsWith('; ')) {
                    result = result.substring(0, result.length - 2)
                }
            }
            // return result
        }else{
            // return '';
        }
    },
    // 获取公式获取结果
    async getFuncResult(obj) {
        let funcResult = obj.getResult(), result = ''
        if(funcResult instanceof AbstractData) {
            await funcResult.Value.then(res => {
                result = res
            })
        }
        return result
    },
    getResultType:function(){
        return FMR.ConstDataTypes.General;
    }
}