import {
    selectListTree,
  } from "@/api/jurisdiction/mune"
  import {
    getRoles,
  } from '@/api/jurisdiction/role'
  import {
    getMenuByRoleId,
    UserBymenu
  } from '@/api/jurisdiction/roleMenu'
  import {
    getButtonList
  } from "@/api/user"
  import {
    loading,
    loadingClose,
    message,
    clearObjValue,
    sortDG
  } from '@/utils/common'
  const getDefaultState = () => {
    return {
      tableDataOne: [],
      tableDataTwo: [],
      EchoList: [],
      showButtons: {
        bc:false
      }
    }
  }
  const state = getDefaultState()
  const mutations = {
    GET_USERS(state, data) {
      state.tableDataTwo = data.list
    },
    GET_ROLES(state, data) {
      state.tableDataOne = data.list
    },
    ROLE_BY_ESER(state, data) {
      state.EchoList = data.menus
  
    },
    SAVE(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('GET_USERS', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    async getRoles({
      commit,
      dispatch
    }, data) {
      await getRoles(data).then(res => {
        try {
          commit('GET_ROLES', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    async getMenuByRoleId({
      commit,
      dispatch
    }, data) {
      await getMenuByRoleId(data).then(res => {
        try {
          commit('ROLE_BY_ESER', res.body)
        } catch (err) {
          throw new Error(err)
        }
      })
    },
    async UserBymenu({
      commit,
      dispatch
    }, data) {
      await UserBymenu(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
  }
  