import {
    selectListTree,
    updateTree,
    deleteTree,
    saveTree
  } from '@/api/jurisdiction/mune'
  import {
    message
  } from '@/utils/common'
  import {
    getButtonList
  } from "@/api/user"
  const getDefaultState = () => {
    return {
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      tableData: [],
      tableLoading: false,
      addStatus: false,
      showButtons: {
        add: false,
        fAdd: false,
        update: false,
        del: false,
      }
    }
  }
  const state = getDefaultState()
  const mutations = {
    TABLE_LIST(state, data) {
      state.tableData = data.list
      state.page.total = data.total
      state.tableLoading = false
    },
    DELECT(state, data) {
      message('success', '删除成功', 3000)
    },
    SAVE(state, data) {
      message('success', '新增成功', 3000)
    },
    UPDATA(state, data) {
      message('success', '修改成功', 3000)
    },
    GET_BUTTON_LIST(state, data) {
      data.forEach(item => {
        state.showButtons[item.key] = true
      })
    }
  }
  const actions = {
    async selectListTree({
      commit,
      dispatch
    }, data) {
      await selectListTree(data).then(res => {
        try {
          commit('TABLE_LIST', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    async deleteTree({
      commit,
      dispatch
    }, data) {
      await deleteTree(data).then(res => {
        try {
          commit('DELECT', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    async updateTree({
      commit,
      dispatch
    }, data) {
      await updateTree(data).then(res => {
        try {
          commit('UPDATA', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    async saveTree({
      commit,
      dispatch
    }, data) {
      await saveTree(data).then(res => {
        try {
          commit('SAVE', 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
  }
  