import { login, logout, getInfo, getBizPerm } from '@/api/login'
import { getToken, setToken, removeToken, removeCurrentSys } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '', // 姓名
    deptId: '', // 部门编号
    deptName: '', // 部门名称
    roleList: [], // 角色编号集合
    roleNames: [], // 角色名称集合
    roleTips: [], // 角色标签集合
    wellTypes: [], // 支持闸井类型
    deviceTypes: [], // 支持设备类型
    communications: [], // 支持通讯方式
    area: '' // 默认地区
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ROLELIST: (state, roleList) => {
      state.roleList = roleList
    },
    SET_ROLENAMES: (state, roleNames) => {
      state.roleNames = roleNames
    },
    SET_ROLETIPS: (state, roleTips) => {
      state.roleTips = roleTips
    },
    SET_DEPTID: (state, deptId) => {
      state.deptId = deptId
    },
    SET_DEPTNAME: (state, deptName) => {
      state.deptName = deptName
    },
    SET_WELLTYPES: (state, wellTypes) => {
      state.wellTypes = wellTypes
    },
    SET_DEVICETYPES: (state, deviceTypes) => {
      state.deviceTypes = deviceTypes
    },
    SET_COMMUNICATIONS: (state, communications) => {
      state.communications = communications
    },
    SET_AREA: (state, area) => {
      state.area = area
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.code === 200) {
            const data = response.data
            setToken(data.token)
            commit('SET_TOKEN', data.token)
            resolve()
          } else {
            reject(response.message)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data
          if (data.roleList && data.roleList.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLELIST', data.roleList)
            commit('SET_ROLENAMES', data.roleNames)
            commit('SET_ROLETIPS', data.roleTips)
          } else {
            reject('该用户无角色')
          }
          if (data.deptId) {
            commit('SET_DEPTID', data.deptId)
            commit('SET_DEPTNAME', data.deptName)
          } else {
            reject('该用户无组织机构')
          }
          commit('SET_NAME', data.name)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
        // getBizPerm().then(response => {
        //   const data = response.data
        //   if (data.wellTypes && data.wellTypes.length > 0) { // 验证返回的wellTypes是否是一个非空数组
        //     commit('SET_WELLTYPES', data.wellTypes)
        //   } else {
        //     reject('该用户不支持任何闸井类型')
        //   }
        //   if (data.deviceTypes && data.deviceTypes.length > 0) {
        //     commit('SET_DEVICETYPES', data.deviceTypes)
        //   } else {
        //     reject('该用户不支持任何设备类型')
        //   }
        //   if (data.communications && data.communications.length > 0) {
        //     commit('SET_COMMUNICATIONS', data.communications)
        //   } else {
        //     reject('该用户不支持任何通讯方式')
        //   }
        //   commit('SET_AREA', data.area)
        //   resolve(response)
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },
    // 获取用户业务权限
    GetBizPerm({ commit, state }) {
      return new Promise((resolve, reject) => {
        getBizPerm(state.token).then(response => {
          const data = response.data
          if (data.wellTypes && data.wellTypes.length > 0) { // 验证返回的wellTypes是否是一个非空数组
            commit('SET_WELLTYPES', data.wellTypes)
          } else {
            reject('该用户不支持任何闸井类型')
          }
          if (data.deviceTypes && data.deviceTypes.length > 0) {
            commit('SET_DEVICETYPES', data.deviceTypes)
          } else {
            reject('该用户不支持任何设备类型')
          }
          if (data.communications && data.communications.length > 0) {
            commit('SET_COMMUNICATIONS', data.communications)
          } else {
            reject('该用户不支持任何通讯方式')
          }
          commit('SET_AREA', data.area)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          removeCurrentSys()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        removeCurrentSys()
        resolve()
      })
    }
  }
}

export default user
