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

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_TokenCode: (state, tokenCode) => {
      state.tokenCode = tokenCode
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_RolesList: (state, roles) => {
      state.rolesList = roles
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_userId: (state, userId) => {
      state.userId = userId
    },
    SET_departmentId: (state, departmentId) => {
      state.departmentId = departmentId
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response.data
          if (data.length === 0 || data.result === '0') {
            Message.error('账号或密码错误，请重新登录')
            this.$router.push({ path: '/login' })
          } else {
            setToken(data[0].角色编号)
            Cookies.set('id', data[0].人员编号, { expires: 1 })
            Cookies.set('name', data[0].姓名, { expires: 1 })
            Cookies.set('userId', data[0].考勤编号, { expires: 1 })
            Cookies.set('session_id', data[0].session_id)
            Cookies.set('departmentId', data[0].考核部门编号)
            commit('SET_TOKEN', data[0].角色编号)
            commit('SET_NAME', data[0].姓名)
            commit('SET_ID', data[0].人员编号)
            commit('SET_userId', data[0].考勤编号)
            commit('SET_departmentId', data[0].考核部门编号)
            resolve()
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetRoleInfo({ commit, getters }, userInfo) {
      return new Promise((resolve, reject) => {
        getRoleInfo(getters.id).then(response => {
          let TokenCode = 3
          if (response.data.length > 0) {
            TokenCode = response.data[0]['当前权限']
          }
          // commit('SET_RolesList', response.data)
          // const currentRole = getters.token
          //
          // for (let i = 0; i < response.data.length; i++) {
          //   const level1_role_id = response.data[i]['一级角色id']
          //   const level2_role_id = response.data[i]['二级角色id']
          //   const level3_role_id = response.data[i]['三级角色id']
          //   if (level1_role_id === currentRole) {
          //     TokenCode = 1
          //     break
          //   }
          //   if (level2_role_id === currentRole) {
          //     TokenCode = 2
          //     break
          //   }
          //   if (level3_role_id === currentRole) {
          //     TokenCode = 3
          //     break
          //   }
          // }
          Cookies.set('tokenCode', TokenCode, { expires: 1 })
          commit('SET_TokenCode', TokenCode)
          setTokenCode(TokenCode)
          resolve(response)
        }).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', '')
        commit('SET_TokenCode', '')
        Cookies.remove('name')
        Cookies.remove('id')
        Cookies.remove('userId')
        Cookies.remove('departmentId')
        Cookies.remove('tokenCode')
        removeToken()
        removeTokenCode()
        resolve()
      })
    }
  }
}

export default user
