import { fetchList, fetchMenuList, addRoleInfo, getRoleInfo, updateRoleInfo, deleteRoles } from '@/api/role'

const transaction = {
  namespaced: true,
  state: {
    total: '',
    roleList: [],
    menuList: []
  },

  mutations: {
    SET_LIST: (state, list) => {
      state.roleList = list
    },
    SET_TOTAL: (state, total) => {
      state.total = total
    },
    SET_MENU_LIST: (state, data) => {
      state.menuList = data
    }
  },

  actions: {
    // fetchList,获取社区列表
    FetchRoleList({ commit }, listQuery) {
      return new Promise((resolve, reject) => {
        fetchList(listQuery).then(response => {
          const data = response.data
          commit('SET_LIST', data.list)
          commit('SET_TOTAL', data.total)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    FetchMenuList({ commit }) {
      return new Promise((resolve, reject) => {
        fetchMenuList().then(response => {
          const data = response.data
          commit('SET_MENU_LIST', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    AddRoleInfo({ commit }, listQuery) {
      return new Promise((resolve, reject) => {
        addRoleInfo(listQuery).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetRoleInfo({ commit }, id) {
      return new Promise((resolve, reject) => {
        getRoleInfo(id).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    UpdateRoleInfo({ commit }, listQuery) {
      return new Promise((resolve, reject) => {
        updateRoleInfo(listQuery).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    DeleteRoles({ commit }, ids) {
      return new Promise((resolve, reject) => {
        deleteRoles(ids).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default transaction
