import {
  getUser
} from "@/api/archiveUtilize/compilationResearch/taskAllocation"
import {
  getData,
  getthemeClass,
  getTree
} from "@/api/archiveUtilize/compilationResearch/editingTopics"
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableData: [],
    loading: false,
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    showButtons: {
      distribution: false,
      complete: false,
    },
    userList: [],
    themeClass: [],
    treeData: []
  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
    state.loading = false
  },
  GET_THEMELIST(state, data) {
    state.themeClass = data
  },
  GET_TREE(state, data) {
    state.treeData = data.list
  },
  GET_USER(state, data) {
    state.userList = data.users
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async getData({
    commit,
    dispatch
  }, data) {
    await getData(data).then(res => {
      try {
        commit('GET_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getthemeClass({
    commit,
    dispatch
  }, data) {
    await getthemeClass(data).then(res => {
      try {
        commit('GET_THEMELIST', res)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getTree({
    commit,
    dispatch
  }, data) {
    await getTree(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getUser({
    commit,
    dispatch
  }, data) {
    await getUser(data).then(res => {
      try {
        commit('GET_USER', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getButtonList({
    commit
  }, params) {
    await getButtonList(params).then(res => {
      commit('GET_BUTTON_LIST', res.body.list)
    })
  }

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