import enumApi from '../../../../api/enum';
import modelApi from '../../../../api/model';
import userApi from '../../../../api/user';

/**
 * 缓存信息
 * */
export default {
    namespaced: true,
    state: {
        // 缓存
        caches: {},
        // 枚举字典信息
        dicts: {},
        // 枚举信息
        enums: {},
        // 用户姓名
        userNames: {}
    },
    getters: {},
    actions: {
        /**
         * @description 加载缓存数据
         * @param {Object} state vuex state
         * @param {*} type 类别
         */
        async loadCache({ state }, type) {
            if (!(type in state.caches)) {
                state.caches[type] = [];

                try {
                    let res = await modelApi.query(type, {});

                    state.caches = {
                        ...state.caches
                    };

                    state.caches[type] = res.data;
                } catch (err) {
                    delete state.caches[type];
                    throw err;
                }
            }
        },
        /**
         * @description 加载枚举字典数据
         * @param {Object} state vuex state
         * @param {*} type 类别
         */
        async loadDict({ state }, type) {
            if (!(type in state.dicts)) {
                state.dicts[type] = {};

                try {
                    let res = await modelApi.query('Dict', { type: type });
                    state.dicts = {
                        ...state.dicts
                    };

                    res.data.forEach(item => {
                        state.dicts[type][item.id] = item.displayName;
                    });
                } catch (err) {
                    delete state.dicts[type];
                    throw err;
                }
            }
        },
        /**
         * @description 加载枚举数据
         * @param {Object} state vuex state
         * @param {*} type 类别
         */
        async loadEnum({ state }, type) {
            if (!(type in state.enums)) {
                state.enums[type] = {};

                try {
                    let res = await enumApi.query(type);
                    state.enums = {
                        ...state.enums
                    };

                    res.forEach(item => {
                        if (item.Css == null) {
                            state.enums[type][item.id] = item.name;
                        } else {
                            state.enums[type][item.id] = '<i class="' + item.Css + '"></i> ' + item.name;
                        }
                    });
                } catch (err) {
                    delete state.enums[type];
                    throw err;
                }
            }
        },
        /**
         * @description 加载用户姓名数据
         * @param {Object} state vuex state
         * @param {*} id 唯一编号
         */
        async loadUserName({ state }, id) {
            if (id && !(id in state.userNames)) {
                state.userNames[id] = {};

                try {
                    let res = await userApi.get(id);

                    if (res) {
                        state.userNames = {
                            ...state.userNames
                        };

                        state.userNames[id] = res.name;
                    }
                } catch (err) {
                    delete state.userNames[id];
                    throw err;
                }
            }
        }
    }
};
