import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo } from '@/apis/longin'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    refresh_token: storage.get(constant.refresh_token),
    username: storage.get(constant.username),
    password: storage.get(constant.password),
    name: storage.get(constant.name),
    avatar: storage.get(constant.avatar),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions),
    dept: storage.get(constant.dept)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_username: (state, username) => {
      state.username = username
      storage.set(constant.username, username)
    },
    SET_password: (state, password) => {
      state.password = password
      storage.set(constant.password, password)
    },
    SET_refresh_token: (state, refresh_token) => {
      state.refresh_token = refresh_token
      storage.set(constant.refresh_token, refresh_token)
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_WXCJ: (state, wxcj) => {
      state.wxcj = wxcj
      storage.set(constant.wxcj, wxcj)
    },
    SET_DEPT: (state, dept) => {
      state.dept = dept
      storage.set(constant.dept, dept)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      storage.set(constant.roles, roles)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.permissions, permissions)
    },
    SET_RFIDCONFIG: (state, rfidparameter) => {
      state.rfidparameter = rfidparameter
      storage.set(constant.rfidparameter, rfidparameter)
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then((res) => {
            setToken(res.data.access_token)
            commit('SET_TOKEN', res.data.access_token)
            commit('SET_refresh_token', res.data.refresh_token)
            commit('SET_username', username)
            commit('SET_password', password)
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res) => {
            const user = res.data
            const avatar =
              user == null || user.avatar == '' || user.avatar == null
                ? require('@/static/avatar-zfsiZCFH.gif')
                : import.meta.env.VITE_APP_BASE_PRE + user.avatar
            const username = user == null || user.userName == '' || user.userName == null ? '' : user.userName
            if (user.roles && user.roles.length > 0) {
              commit('SET_ROLES', user.roles)
              commit('SET_PERMISSIONS', user.permissions)
            } else {
              commit('SET_ROLES', ['ROLE_DEFAULT'])
            }

            commit('SET_DEPT', user.dept)
            commit('SET_NAME', username)
            commit('SET_AVATAR', avatar)
            resolve(res)
          })
          .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()
            storage.clean()
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  }
}

export default user
