import dataViewApi from '../../../../api/dataView';

/**
 * 缓存信息
 * */
export default {
    namespaced: true,
    state: {
        // 数据视图
        dataViews: {},
        // 数据结构
        schemas: {}
    },
    getters: {
        get: (state, getters) => code => {
            return state.dataViews[code];
        },
        getSchema: (state, getters) => model => {
            return state.schemas[model];
        }
    },
    actions: {
        /**
         * @description 加载数据视图
         * @param {Object} state vuex state
         * @param {*} code 代码
         */
        async load({ state }, code) {
            if (!(code in state.dataViews)) {
                let res = await dataViewApi.getByCode(code);
                state.dataViews[code] = res;

                return res;
            } else {
                return state.dataViews[code];
            }
        },
        /**
         * @description 加载数据结构
         * @param {Object} state vuex state
         * @param {*} model 模型
         */
        async loadSchema({ state }, model) {
            if (!(model in state.schemas)) {
                let res = await dataViewApi.getSchemaByModel(model);
                state.schemas[model] = res;

                return res;
            } else {
                return state.schemas[model];
            }
        },
        /**
         * @description 重置数据视图
         * @param {Object} state vuex state
         * @param {*} code 代码
         */
        async reset({ state }, code) {
            let res = await dataViewApi.reset(code);
            state.dataViews[code] = res;

            return res;
        },
        /**
         * @description 保存数据视图
         * @param {Object} state vuex state
         * @param {*} code 代码
         * @param {*} columns 字段
         */
        async save({ state }, { code, columns }) {
            let res = await dataViewApi.save(code, columns);
            state.dataViews[code] = res;

            return res;
        },
        /**
         * @description 保存用户数据视图
         * @param {Object} state vuex state
         * @param {*} code 代码
         * @param {*} columns 字段
         */
        async saveUserView({ state }, { code, columns }) {
            let res = await dataViewApi.saveUserView(code, columns);
            state.dataViews[code] = res;

            return res;
        },
        /**
         * @description 保存筛选字段
         * @param {Object} state vuex state
         * @param {*} code 代码
         * @param {*} columns 字段
         */
        async saveFilterColumn({ state }, { code, columns }) {
            let res = await dataViewApi.saveFilterColumn(code, columns);
            state.dataViews[code] = res;

            return res;
        },
        /**
         * @description 保存数据视图设置
         * @param {Object} state vuex state
         * @param {*} dataView 数据视图
         */
        async saveSetting({ state }, dataView) {
            let res = await dataViewApi.saveSetting(dataView);
            state.dataViews[dataView.code] = res;

            return res;
        }
    }
};
