import { login, logout, getInfo, refreshToken } from 'src/api/login'
import { getToken, setToken, setExpiresIn, removeToken } from 'src/utils/auth'
import { encrypt } from 'src/utils/jsencrypt'
import {Dialog} from "vant";
const user = {
  state: {
    token: getToken(),
    name: '',
    id: '',
    avatar: '',
    roles: [],
    permissions: [],
    productInfo: []
  },

  mutations: {
    // 设置token
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    // 设置过期时间
    SET_EXPIRES_IN: (state, time) => {
      state.expires_in = time
    },
    // 设置用户名
    SET_NAME: (state, name) => {
      state.name = name
    },
    // 设置id
    SET_ID: (state, id) => {
      state.id = id
      console.log(id)
    },
    // 设置头像
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    // 设置角色
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    // 设置权限
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    // 设置产品信息
    SET_PRODUCTINFO: (state, productInfo) => {
      state.productInfo = productInfo
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = encrypt(userInfo.username.trim())
      const password = encrypt(userInfo.password)
      const code = userInfo.code
      const uuid = userInfo.uuid
      const version = userInfo.version
      const type = "PDA"

      return new Promise((resolve, reject) => {
        login(username, password, code, version, uuid,  type).then(res => {
          let data = res.data
          setToken(data.access_token)
          console.log("登陆成功")
          // console.log("登陆成功 token: " + data.access_token)
          commit('SET_TOKEN', data.access_token)
          setExpiresIn(data.expires_in)
          commit('SET_EXPIRES_IN', data.expires_in)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state },str) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
         if(str == undefined){
           const user = res.user
           // const avatar = user.avatar == "" ? require("assets/images/profile.jpg") : user.avatar;
           if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
             commit('SET_ROLES', res.roles)
             commit('SET_PERMISSIONS', res.permissions)
           } else {
             commit('SET_ROLES', ['ROLE_DEFAULT'])
           }
           commit('SET_NAME', user.userName)
           commit('SET_ID', user.userId)
           // commit('SET_AVATAR', avatar)
           resolve(res)
         }else {
           commit('SET_ROLES',[])
         }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 刷新token
    RefreshToken({commit, state}) {
      return new Promise((resolve, reject) => {
        refreshToken(state.token).then(res => {
          setExpiresIn(res.data)
          commit('SET_EXPIRES_IN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

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

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

export default user
