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(), // zheshi zhi,getToken()zhegenigaishiduoshao jiushiduoshao
    name: Cookies.get('name'),
    cname: Cookies.get('cname'),
    roles: [],
    id: Cookies.get('id'),
    account: Cookies.get('account')
  },

  mutations: {
    SET_TOKEN: (state, token) => { // zheshi fangfa
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_CNAME: (state, cname) => {
      state.cname = cname
    },
    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).then(response => {
          let data = response.data
          if (data.length === 0 || data.result === '0') {
            Message.error('账号或密码错误，请重新登录')
            this.$router.push({ path: '/login' })
          } else if (data.length === 1) { // 只有一个角色
            setToken(data[0].角色代码)
            Cookies.set('id', data[0].用户代码, { expires: 1 })
            Cookies.set('name', data[0].用户名称, { expires: 1 })
            Cookies.set('cname', data[0].姓名, { expires: 1 })
            commit('SET_TOKEN', data[0].角色代码)
            commit('SET_NAME', data[0].用户名称)
            commit('SET_CNAME', data[0].姓名)
            commit('SET_ID', data[0].用户代码)
            commit('SET_account', data[0].account, { 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(res[0].角色代码)
              Cookies.set('id', res[0].用户代码, { expires: 1 })
              Cookies.set('name', res[0].用户名称, { expires: 1 })
              Cookies.set('cname', data[0].姓名, { expires: 1 })
              commit('SET_TOKEN', res[0].角色代码)
              commit('SET_NAME', res[0].用户名称)
              commit('SET_CNAME', data[0].姓名)
              commit('SET_ID', res[0].用户代码)
              commit('SET_account', res[0].account, { expires: 1 })
            }
            data = res
            resolve({ data, commit })
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }, type) {
      console.log(state.token)
      console.log(state.name)
      console.log(state.id)
      console.log(state.cname)
      return new Promise((resolve, reject) => {
        getInfo(state.token, type).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('cname')
        Cookies.remove('id')
        Cookies.remove('account')
        Cookies.remove('type')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
