import { login, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import Cookies from 'js-cookie'
import { Message } from 'element-ui'
const user = {
  state: {
    token: getToken(),
    name: Cookies.get('name'),
    roles: [],
    id: Cookies.get('id'),
    account: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_account: (state, account) => {
      state.account = account
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password, userInfo.station).then(response => {
          let data = response.data
          if (data[0].result === 0) {
            Message.error(data[0].msg)
            this.$router.push({ path: '/login' })
          }
          if (data.length === 0 || data.result === '0') {
            Message.error('账号或密码错误，请重新登录')
            this.$router.push({ path: '/login' })
          } else if (data.length === 1) { // 只有一个角色
            if (data[0][userInfo.station.label] !== 1) {
              Message.error('该账号无此工位操作权限，请获取权限后重试！')
              this.$router.push({ path: '/login' })
              return
            }
            setToken(data[0].角色代码)
            Cookies.set('id', data[0].人员编号, { expires: 1 })
            Cookies.set('name', data[0].姓名, { expires: 1 })
            // commit('SET_TOKEN', data[0].角色代码)
            commit('SET_NAME', data[0].姓名)
            commit('SET_ID', data[0].人员编号)
            commit('SET_account', data[0].考勤编号, { expires: 1 })
            resolve({ data, commit })
          } else { // 多个角色
            var res = []
            var resRole = []
            data.forEach(item => {
              resRole.includes(item.Roles_Function) ? '' : (res.push(item), resRole.push(item.Roles_Function))
            })
            if (res.length === 1) {
              setToken(data[0].角色代码)
              Cookies.set('id', data[0].人员编号, { expires: 1 })
              Cookies.set('name', data[0].姓名, { expires: 1 })
              // commit('SET_TOKEN', data[0].角色代码)
              commit('SET_NAME', data[0].姓名)
              commit('SET_ID', data[0].人员编号)
              commit('SET_account', data[0].考勤编号, { expires: 1 })
              resolve({ data, commit })
            }
            data = res
            resolve({ data, commit })
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          commit('SET_ROLES', 'admin')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    //  登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        Cookies.remove('name')
        Cookies.remove('id')
        Cookies.remove('account')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
