import _ from 'lodash'

/**
 * 报销单的支付明细表的收款账户省份和收款账户地区赋值
 * @author raojiaguan
 * @data 2021年8月24日18:07:32
 */
export default {
    execute: function () {
        console.log("s1 SetInternalAccommodation")
        if (arguments && arguments[0] && arguments[0].length > 1 && arguments[1]) {
            // TODO 参数校验
            let context = arguments[1]
            let targetSubField = arguments[0][0];
            let targetSubField2=arguments[0][1];
            let targetSubField3=arguments[0][2].getResult().Value;
            let subTableObject = context.getTableType(targetSubField.FieldTableCode)
            let oldIndex = context.getSubFocus()
            let focusData = context.getAllSubData()[subTableObject.title][oldIndex].getData()
            if (_.isString(targetSubField3.skzhsf) && targetSubField3.skzhsf.length > 0 && targetSubField3.skzhds &&   targetSubField3.skzhds.length > 0) {
                if(!(_.isEqual(focusData[arguments[0][2].FieldCode]._field_old_value_ ,focusData[arguments[0][2].FieldCode].value ))){
                    let param = {
                        "code": targetSubField3.skzhsf,
                        "tableName" :"MD_SKZHSF",
                        "queryDataStructure": "ALL",
                        "limit": 1,
                        "offset": 0,
                        "pagination": true
                    }
                    this.getMDObject(param, (data) => {
                        let newIndex = context.getSubFocus();
                        if (oldIndex !== newIndex) {
                            context.setSubFocus(oldIndex)
                        }
                        focusData[arguments[0][2].FieldCode]._field_old_value_ = focusData[arguments[0][2].FieldCode].value
                        // focusData.ZJ._field_old_value_ = focusData.ZJ.value
                        // focusData.RZRQ._field_old_value_ = focusData.RZRQ.value
                        // focusData.LDRQ._field_old_value_ = focusData.LDRQ.value
//                      targetSubField.dynamicNodeFactory.methodJson.SetFieldData('', linkId, data.cSontent, '')
                        focusData[targetSubField.FieldCode]._field_old_value_ = focusData[targetSubField.FieldCode].value = data.rows[0]
                        if (oldIndex !== newIndex) {
                            context.setSubFocus(newIndex)
                        }
                        context.refreshSubDataRow(subTableObject.title)
                    }, (resp) => {
                        GAMS.Common.messagePrompt("境内住宿标准金额获取接口请求失败，请联系管理员")
                        console.error("获取境内住宿标准公式直接失败"+resp)
                    });

                    let param1 = {
                        "code": targetSubField3.skzhds,
                        "tableName" :"MD_SKZHDS",
                        "queryDataStructure": "ALL",
                        "limit": 1,
                        "offset": 0,
                        "pagination": true
                    }
                    this.getMDObject(param1, (data) => {
                        let newIndex1 = context.getSubFocus();
                        if (oldIndex !== newIndex1) {
                            context.setSubFocus(oldIndex)
                        }
                        focusData[arguments[0][2].FieldCode]._field_old_value_ = focusData[arguments[0][2].FieldCode].value
                        focusData[targetSubField2.FieldCode]._field_old_value_ = focusData[targetSubField2.FieldCode].value = data.rows[0]
                        if (oldIndex !== newIndex1) {
                            context.setSubFocus(newIndex1)
                        }
                        context.refreshSubDataRow(subTableObject.title)
                    }, (resp) => {
                        GAMS.Common.messagePrompt("境内住宿标准金额获取接口请求失败，请联系管理员")
                        console.error("获取境内住宿标准公式直接失败"+resp)
                    });
                }
          
                }
            }else{
                focusData[targetSubField.FieldCode]._field_old_value_ = focusData[targetSubField.FieldCode].value = null
                focusData[targetSubField2.FieldCode]._field_old_value_ = focusData[targetSubField2.FieldCode].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;
    }
}
