import _ from 'lodash'

/**
 * 子表赋值 境内住宿标准
 * @author fengjiansheng
 * @data 2021-08-22
 */
export default {
    execute: function () {
        console.log("s1 SetInternalAccommodation")
        if (arguments && arguments[0] && arguments[0].length >= 5 && arguments[1]) {
            // TODO 参数校验
            let context = arguments[1]
            let targetSubField = arguments[0][0];
            let internalStandardBaseDataCode = arguments[0][1].getResult().Value;
            let staffCode = arguments[0][2].getResult().Value;
            let startTime = arguments[0][3].getResult().Value;
            let endTime = arguments[0][4].getResult().Value;
            let subTableObject = context.getTableType(targetSubField.FieldTableCode)
            let oldIndex = context.getSubFocus()
            let focusData = context.getAllSubData()[subTableObject.title][oldIndex].getData()
            if (_.isString(internalStandardBaseDataCode) && internalStandardBaseDataCode.length > 0 && _.isString(staffCode) && staffCode.length > 0 && _.isNumber(startTime) && startTime > 0 && _.isNumber(endTime) && endTime > 0 && startTime <= endTime) {
                if (!(_.isEqual(focusData.CCDQ._field_old_value_, focusData.CCDQ.value) && _.isEqual(focusData.ZJ._field_old_value_, focusData.ZJ.value) && _.isEqual(focusData.RZRQ._field_old_value_, focusData.RZRQ.value) && _.isEqual(focusData.LDRQ._field_old_value_, focusData.LDRQ.value))) {
                    let param = {
                        "code": internalStandardBaseDataCode,
                        "staffCode": staffCode,
                        "startTime": startTime,
                        "endTime": endTime
                    }
                    this.getMDObject(param, (data) => {
                        let newIndex = context.getSubFocus();
                        if (oldIndex !== newIndex) {
                            context.setSubFocus(oldIndex)
                        }
                        focusData.CCDQ._field_old_value_ = focusData.CCDQ.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
                        focusData.ZSFYBZ._field_old_value_ = focusData.ZSFYBZ.value = data.content
                        if (oldIndex !== newIndex) {
                            context.setSubFocus(newIndex)
                        }
                        context.refreshSubDataRow(subTableObject.title)
                    }, (resp) => {
                        GAMS.Common.messagePrompt("境内住宿标准金额获取接口请求失败，请联系管理员")
                        console.error("获取境内住宿标准公式直接失败"+resp)
                    });
                }
            }else{
                focusData.ZSFYBZ._field_old_value_ = focusData.ZSFYBZ.value = null
                context.refreshSubDataRow(subTableObject.title)
            }
        }
    },
    getMDObject(param, callBack, errorCallBack) {
        GMS.$http.post("/rbcfunc/baseData/getInternalAccommodationStandardAmount", param).then(data => {
            callBack(data.data);
        }).catch((resp) => {
            errorCallBack(resp);
        });
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Void;
    }
}
