import { login, logOut, getInfo, sendCode, getPermission } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { employeesList, userRoleById, addUser, selectUser, upDate, upDateStatus, userVerifyAll, userVerifyAudit, taUserHas, taUserHasOwner } from '@/api/user'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    userData: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_USER_DATA: (state, userData) => {
      state.userData = userData
    }
  },

  actions: {
    // 登录
    Login({ commit }, data) {
      const username = data.username.trim()
      return new Promise((resolve, reject) => {
        login(username, data.password, data.communityId).then(response => {
          console.log('login: ', response)
          if (response.code === '0') {
            const data = response.data
            setToken(data.token)
            commit('SET_TOKEN', data.token)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data
          // if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
          //   commit('SET_ROLES', data.roles)
          // } else {
          //   reject('getInfo: roles must be a non-null array !')
          // }
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.userName)
          commit('SET_AVATAR', data.loginName)
          commit('SET_USER_DATA', data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取此用户的权限集合
    GetPermission({ commit, state }) {
      return new Promise((resolve, reject) => {
        getPermission().then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 发送验证码
    SendCode({ commit, state }, userName) {
      return new Promise((resolve, reject) => {
        sendCode(userName).then(response => {
          // const data = response.message
          // resolve(data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

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

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    // 根据条件查询 employeesList
    EmployeesList({ commit }, listQuery) {
      return new Promise((resolve, reject) => {
        employeesList(listQuery).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 查询角色 UserRoleById
    UserRoleById({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        userRoleById(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 添加成员角色
    AddUser({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        addUser(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取成员信息
    SelectUser({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        selectUser(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 修改成员信息
    UpDate({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        upDate(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 修改成员状态(停用或者启用)
    UpDateStatus({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        upDateStatus(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    UserVerifyAll({ commit }, data) { // 审核用户列表
      return new Promise((resolve, reject) => {
        userVerifyAll(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    UserVerifyAudit({ commit }, data) { // 审核用户
      return new Promise((resolve, reject) => {
        userVerifyAudit(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    TaUserHas({ commit }, data) { // 校验移动端手机号是否注册
      return new Promise((resolve, reject) => {
        taUserHas(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    TaUserHasOwner({ commit }, data) { // 根据 期/栋/单元/楼层/户号 校验是否有户主
      return new Promise((resolve, reject) => {
        taUserHasOwner(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
