import _ from 'lodash'

/**
 * 子表赋值 境内住宿标准合计
 * @author fengjiansheng
 * @data 2021-08-22
 */
export default {
    execute: function () {
        console.log("s1 SetInternalAccommodationTotal")
        if (arguments && arguments[0] && arguments[0].length >= 7 && 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 numOfPeople = arguments[0][5].getResult().Value;
            let isCheckInOnFirstDayEarliestBaseDataCode
            let isCheckOutOnLastDayLatestBaseDataCode
            isCheckInOnFirstDayEarliestBaseDataCode = arguments[0][6].getResult().Value
            if (arguments[0].length <= 7) {
                isCheckOutOnLastDayLatestBaseDataCode = '0'
            } else {
                isCheckOutOnLastDayLatestBaseDataCode = arguments[0][7].getResult().Value
            }
            if (_.isEmpty(isCheckInOnFirstDayEarliestBaseDataCode) || _.isEqual(isCheckInOnFirstDayEarliestBaseDataCode, "")) {
                isCheckInOnFirstDayEarliestBaseDataCode = "0";
            }
            if (_.isEmpty(isCheckOutOnLastDayLatestBaseDataCode) || _.isEqual(isCheckOutOnLastDayLatestBaseDataCode, "")) {
                isCheckOutOnLastDayLatestBaseDataCode = "0";
            }
            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 && _.isNumber(_.toNumber(numOfPeople)) && numOfPeople > 0) {
                let postRequestFlag = false
                if (arguments[0].length <=7 ) {
                    postRequestFlag = !(_.isEqual(focusData.SRLCRZ._field_old_value_, focusData.SRLCRZ.value) && _.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) && _.isEqual(focusData.RS._field_old_value_, focusData.RS.value))
                } else {
                    postRequestFlag = !(_.isEqual(focusData.WRYCTF._field_old_value_, focusData.WRYCTF.value) && _.isEqual(focusData.SRLCRZ._field_old_value_, focusData.SRLCRZ.value) && _.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) && _.isEqual(focusData.RS._field_old_value_, focusData.RS.value))
                }
                if(postRequestFlag){
                    let param = {
                        "code": internalStandardBaseDataCode,
                        "staffCode": staffCode,
                        "startTime": startTime,
                        "endTime": endTime,
                        "isCheckInOnFirstDayEarliestBaseDataCode": isCheckInOnFirstDayEarliestBaseDataCode,
                        "isCheckOutOnLastDayLatestBaseDataCode":isCheckOutOnLastDayLatestBaseDataCode
                    }
                    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.RS._field_old_value_ = focusData.RS.value
                        if (arguments[0].length >= 7) {
                            focusData.SRLCRZ._field_old_value_ = focusData.SRLCRZ.value;
                        }
                        if (arguments[0].length >= 8) {
                            focusData.WRYCTF._field_old_value_ = focusData.WRYCTF.value
                        }
                        targetSubField.dynamicNodeFactory.methodJson.SetFieldData('', targetSubField.FieldTableCode + '.' + targetSubField.FieldCode, data.content * numOfPeople, '')
//                        focusData.ZSFYBZHJ._field_old_value_ = focusData.ZSFYBZHJ.value = data.content * numOfPeople
                        if (oldIndex !== newIndex) {
                            context.setSubFocus(newIndex)
                        }
                        context.refreshSubDataRow(subTableObject.title)
                    }, (resp) => {
                        GAMS.Common.messagePrompt("境内住宿标准金额合计获取接口请求失败，请联系管理员")
                        console.error("获取境内住宿标准金额合计公式失败" + resp)
                    });
                }
            } else {
                focusData.ZSFYBZHJ._field_old_value_ = focusData.ZSFYBZHJ.value = null;
                context.refreshSubDataRow(subTableObject.title);
            }
        }
    },
    getMDObject(param, callBack, errorCallBack) {
        GMS.$http.post("/rbcfunc/baseData/getTotalInternalAccommodationStandardAmount", param).then(data => {
            callBack(data.data);
        }).catch((resp) => {
            errorCallBack(resp);
        });
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Void;
    }
}
