import Vue from 'vue'

const default_state = {
    head_list: [],
    action_list: [],
    table_list: [],
    head_data_map: {},
    table_data: [],
    page: {
        total: 0,
        size: 20,
        cur: 1,
    },

    table_props: {
        is_local_page: true,
        id: 'id',
        load: 'load_tree'
    },
    dialog_list:[],
    dialog_data:{}
};

const common_list = {
    state: JSON.parse(JSON.stringify(default_state)),
    mutations: {
        SET_HEAD_LIST: (state, head_list) => {
            state.head_list = head_list;
        },

        SET_ACTION_LIST: (state, action_list) => {
            state.action_list = action_list;
        },

        SET_TABLE_LIST: (state, table_list) => {
            state.table_list = table_list;
        },

        SET_HEAD_DATA_MAP: (state, head_data_map) => {
            state.head_data_map = head_data_map;
        },

        SET_DIALOG_LIST: (state, dialog_list) => {
            state.dialog_list = dialog_list;
        },

        SET_DIALOG_DATA: (state, dialog_data) => {
            state.dialog_data = dialog_data;
        },

        SET_TABLE_DATA: (state, table_data) => {
            state.table_data = table_data;
            if (state.table_props.is_local_page) {
                state.page.total = table_data.length
            }
        },

        SET_TOTAL: (state, total) => {
            state.page.total = total;
        },

        SET_PAGE_SIZE: (state, page_size) => {
            state.page.size = page_size;
        },

        SET_CUR_PAGE: (state, cur_page) => {
            state.page.cur = cur_page;
        },

        SET_IS_LOCAL_PAGE: (state, is_local_page) => {
            state.table_props.is_local_page = is_local_page;
        },

        REFRESH_STATE: (state) => {
            console.log("刷新了vuex");
            state = JSON.parse(JSON.stringify(default_state));
            console.log(state);
        },

    },
    actions: {
        set_head_list({commit}, item) {
            commit('SET_HEAD_LIST', item)
        },

        set_action_list({commit}, item) {
            commit('SET_ACTION_LIST', item)
        },


        set_table_list({commit}, item) {
            commit('SET_TABLE_LIST', item)
        },

        set_head_data_map({commit}, item) {
            commit('SET_HEAD_DATA_MAP', item)
        },

        set_table_data({commit}, item) {
            commit('SET_TABLE_DATA', item)
        },

        set_dialog_list({commit}, item) {
            commit('SET_DIALOG_LIST', item)
        },

        set_dialog_data({commit}, item) {
            commit('SET_DIALOG_DATA', item)
        },

        set_page_size({commit}, item) {
            commit('SET_PAGE_SIZE', item)
        },

        set_cur_page({commit}, item) {
            commit('SET_CUR_PAGE', item)
        },

        refresh_state({commit}) {
            commit('REFRESH_STATE');
        },
    },
};

export default common_list
