import {
  getData,
  getthemeClass,
  getInfo,
  editTheme,
  addTheme,
  getTree,
  getTree2,
  delTheme,
  audit
} 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,
    },
    themeClass: [],
    ruleForm: {
      title: "",
      subjectId: "",
      state: "",
      subjectName: "",
      id: "",
    },
    treeData: [],
    showButtons: {
      add: false,
      del: false,
      set: false,
      examine: false,
      edit: false
    },
  }
}
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_INFO(state, data) {
    for (let key in data) {
      for (let index in state.ruleForm) {
        if (key === index) {
          state.ruleForm[index] = data[key]
        }
      }
    }
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  },
  GET_TREE(state, data) {
    state.treeData = data.list
  }
}
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 getInfo({
    commit,
    dispatch
  }, data) {
    await getInfo(data).then(res => {
      try {
        commit('GET_INFO', res)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async editTheme({
    commit,
    dispatch
  }, data) {
    await editTheme(data).then(res => {
      try {} catch (err) {
        throw new Error(err)
      }
    })
  },
  async addTheme({
    commit,
    dispatch
  }, data) {
    await addTheme(data).then(res => {
      try {} 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 delTheme({
    commit,
    dispatch
  }, data) {
    await delTheme(data).then(res => {
      try {} catch (err) {
        throw new Error(err)
      }
    })
  },
  async audit({
    commit,
    dispatch
  }, data) {
    await audit(data).then(res => {
      try {} 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
}
