import Vue from 'vue' import axios from 'axios' export default { namespaced: true, state: { groupList: [], clientList: [], tabChain: [], activeTab: 0 }, mutations: { setGroupList: (state, list) => { state.groupList = list }, setClientList: (state, list) => { state.clientList = list }, setActiveTab: (state, index) => { state.activeTab = index }, deleteFromTabChain: (state, { index, count }) => { state.tabChain.splice(index, count) }, setTab: (state, { index, item }) => { if (state.tabChain.length > index && state.tabChain[index].id === item.id) { Object.keys(item).forEach(key => { Vue.set(state.tabChain[index], key, item[key]) }) } else { state.tabChain = state.tabChain.slice(0, index) state.tabChain.push(item) } }, resetStore (state) { state.groupList = [] state.clientList = [] state.home = null state.groupChain = [] state.client = null state.activeTab = 0 } }, actions: { loadLists (context) { Promise.all([axios.get('/api/groups/getList'), axios.get('/api/clients/getList')]).then(res => { context.commit('setGroupList', res[0].data.map(x => ({ value: x.id, text: x.name || x.id }))) context.commit('setClientList', res[1].data.map(x => ({ value: x.id, text: x.name || x.id }))) }) }, loadHome (context) { Promise.all([axios.get('/api/groups/getTopLevel'), axios.get('/api/clients/getTopLevel')]).then(res => { context.commit('setTab', { index: 0, item: { tabType: 'home', groups: res[0].data, clients: res[1].data } }) }) }, loadGroup (context, { id, tabIndex, switchTab }) { axios.get('/api/groups/getGroup?id=' + id).then(res => { res.data.tabType = 'group' context.commit('setTab', { index: tabIndex, item: res.data }) if (switchTab) context.commit('setActiveTab', tabIndex) }) }, loadClient (context, { id, tabIndex, switchTab }) { axios.get('/api/clients/getClient?id=' + id).then(res => { res.data.tabType = 'client' context.commit('setTab', { index: tabIndex, item: res.data }) if (switchTab) context.commit('setActiveTab', tabIndex) }) }, reload (context) { context.dispatch('loadLists') context.state.tabChain.forEach((item, index) => { if (item.tabType === 'home') context.dispatch('loadHome') else if (item.tabType === 'group') context.dispatch('loadGroup', { id: item.id, tabIndex: index }) else if (item.tabType === 'client') context.dispatch('loadClient', { id: item.id, tabIndex: index }) }) }, saveGroup (context, { id, info, parentIds, tabIndex }) { axios.post('/api/groups/saveInfo', { id, info, parentIds }).then(res => { if (parentIds && tabIndex > 1 && !parentIds.includes(context.state.tabChain[tabIndex - 1].id)) { context.commit('deleteFromTabChain', { index: 1, count: tabIndex - 1 }) context.commit('setActiveTab', 1) } context.dispatch('reload') }) }, saveClient (context, { id, info, groupIds, tabIndex }) { axios.post('/api/clients/saveInfo', { id, info, groupIds }).then(res => { if (groupIds && tabIndex > 1 && !groupIds.includes(context.state.tabChain[tabIndex - 1].id)) { context.commit('deleteFromTabChain', { index: 1, count: tabIndex - 1 }) context.commit('setActiveTab', 1) } context.dispatch('reload') }) } } }