import Api from "../service/Api";

export default {
    methods: {
        loading(text) {
            return this.$loading({
                lock: true,
                text,
                spinner: 'el-icon-loading',
                background: 'rgba(200, 200, 200, 0.3)',
            });
        },
        //获取公共组件开关
        // async ejectOitWarning(tblnam) {
        //     let stbVo = {tbl: tblnam};
        //     let params = {stbVo: stbVo};
        //     let rtnmsg = await Api.post(`/${this.moduleRouter()}/codetable/configOpen`, params);
        //     if (rtnmsg.respCode === SUCCESS) {
        //         return rtnmsg.data.stbVoList[0].srt;
        //     }
        // },
        //弹出oit的warning
        async ejectOitWarning(objtyp, extkey, path) {
            let params = {objtyp: objtyp, extkey: extkey, path: path};
            let rtnmsg = await Api.post(`/${this.moduleRouter()}/oit/oitcheck`, params);
            if (rtnmsg.respCode === SUCCESS) {
                this.handleShowWarning(rtnmsg);
            }
        },
        handleShowWarning(response) {
            if (response.warnList && response.warnList.length) {
                let curWarnList = [];
                if (this.root) {
                    curWarnList = this.root.model.warnList || []
                }
                curWarnList = this.model.warnList || [];
                response.warnList.map((resItem) => {
                    if (resItem.message) {
                        let hasFlag = curWarnList.some((item) => {
                            return item.key === resItem.key
                        });
                        if (hasFlag) {
                            curWarnList.forEach((item) => {
                                if (item.key === resItem.key) {
                                    if (item.message !== resItem.message) {
                                        Object.assign(item, resItem)
                                    }

                                }
                            })
                        } else {
                            curWarnList.push(resItem)
                        }
                    } else {
                        let ind = curWarnList.findIndex((item) => {
                            return item.key === resItem.key
                        });
                        if (ind > -1) {
                            curWarnList.splice(ind, 1)
                        }
                    }
                });
                if (this.root) {
                    this.$set(this.root.model, 'warnList', curWarnList)
                }
                this.$set(this.model, 'warnList', curWarnList);
                // 判断是否存在未读的warning,不存在则不弹出弹框,存在即弹出
                let isHasNoRead = false;
                if (curWarnList && curWarnList.length > 0) {
                    isHasNoRead = curWarnList.some((item) => {
                        return item.readFlag === 'N'
                    })
                }
                if (isHasNoRead) {
                    this.$store.commit('updateWarningVisible', true)
                }
            }
        },
        getCodeLabel(value, param) {
            let codeTable = [];
            if (Array.isArray(param)) {
                codeTable = [...param];
            } else if (typeof param === 'string') {
                // codeTable = this.codes[param];
                codeTable = this.getObjectProperty(param, this.codes);
            }
            if (!codeTable) {
                return value;// 如果没有找到对应的码表,返回原值
            }
            const item = codeTable.find(item => item.value === value);
            return item ? item.label : value;// 如果找到对应项则返回label,否则返回原值
        },
        getObjectProperty(str, obj) {
            if (typeof str !== 'string' || typeof obj !== 'object' || obj === null) {
                return undefined;// 如果参数类型不正确,返回undefined
            }
            if (str.includes('.')) {
                // 分割字符串以获取属性名
                var properties = str.split('.');
                let currentObj = obj;
                // 遍历属性名并深入对象
                for (let i = 0; i < properties.length; i++) {
                    if (currentObj.hasOwnProperty(properties[i])) {
                        currentObj = currentObj[properties[i]];
                    } else {
                        return undefined;// 如果属性不存在,返回undefined
                    }
                }
                return currentObj;// 返回找到的属性值
            } else {
                // 如果字符串不包含`.`,直接返回对象的属性
                return obj[str];
            }
        }
    },
    computed: {
        isInfo() {
            let trnName = this.root ? this.root.trnName : this.trnName;
            if (trnName && trnName.endsWith("inf")) {
                return true
            }
            return false
        }
    }
};