import {
  getUsers,
} from "@/api/jurisdiction/user"
import {
  getRoles,
} from '@/api/jurisdiction/role'
import {
  roleByUser,
  saveUserRole,
  saveUserFond,
  fondByUser
} from '@/api/jurisdiction/userRole'
import {
  getFondWithPage
} from "@/api/system-management/allAdministration"
import {
  getButtonList
} from "@/api/user"
import {
  loading,
  loadingClose,
  message,
  clearObjValue,
  sortDG
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableDataOne: [],
    tableDataTwo: [],
    tableDataThree: [],
    EchoList: [],
    showButtons: {
      bc: false,
    }
  }
}
const state = getDefaultState()
const mutations = {
  GET_USERS(state, data) {
    state.tableDataOne = data.list
  },
  GET_ROLES(state, data) {
    state.tableDataTwo = data.list
  },
  FOND_PAGE(state, data) {
    state.tableDataThree = data.list
  },
  ROLE_BY_ESER(state, data) {
    state.EchoList = data.list
  },
  FOND_BY_USER(state, data) {
    state.EchoList = data.list
  },
  SAVE(state, data) {
    message('success', '保存成功', 3000)
  },
  SAVE_FOND(state, data) {
    message('success', '保存成功', 3000)
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async getUsers({
    commit,
    dispatch
  }, data) {
    await getUsers(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 roleByUser({
    commit,
    dispatch
  }, data) {
    await roleByUser(data).then(res => {
      try {
        commit('ROLE_BY_ESER', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async fondByUser({
    commit,
    dispatch
  }, data) {
    await fondByUser(data).then(res => {
      try {
        commit('FOND_BY_USER', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveUserRole({
    commit,
    dispatch
  }, data) {
    await saveUserRole(data).then(res => {
      try {
        commit('SAVE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveUserFond({
    commit,
    dispatch
  }, data) {
    await saveUserFond(data).then(res => {
      try {
        commit('SAVE_FOND', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getFondWithPage({
    commit,
    dispatch
  }, data) {
    await getFondWithPage(data).then(res => {
      try {
        commit('FOND_PAGE', 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
}
