import metaDataApi from '../api/metaDataApi'

const state = {
    metadata: [],
    metaDataOfTable: [],
    table: [],
    count: 0,
    isLoading: true
}

const mutations = {
    setMetaData(state, data) {
        state.metadata = data;
    },
    setMetaDataOfTable(state, data) {
        state.metaDataOfTable = data;
    },
    setTable(state, data) {
        state.table = data;
    },
    setCount(state, data) {
        state.count = data;
    },
    setLoading(state, status) {
        state.isLoading = status
    }
}

const actions = {
    queryAllByTableName({commit}, tableName) {
        metaDataApi.queryAllByTableName(tableName, (response) => {
            if (response.data.code === 200) {
                commit('setMetaDataOfTable', response.data.content);
            }
        })
    },
    queryAll({commit}) {
        metaDataApi.queryAll((response) => {
            if (response.data.code === 200) {
                commit('setMetaData', response.data.content);
            }
        })
    },
    queryPageable({commit}, pageData) {
        commit('setLoading', true);
        metaDataApi.queryPageable(pageData.data, pageData.index, pageData.size, (response) => {
            if (response.data.code === 200) {
                commit('setTable', response.data.content);
                commit('setCount', parseInt(response.data.message));
                commit('setLoading', false);
            } else {
                commit('setLoading', false);
            }
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}