import { getInfo, login, logout, getIndex } from '@/api/login/login.js'
import { getToken, removeToken, setToken, setAccessToken, removeAccessToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import LogoImg from '@/assets/logo.png'

const user = {
  state: {
    token: getToken(),
    supplierName:"",
    name: '',
    avatar: '',
    roles: [],
    menus: [],
    user: [],
    indexInfo: []
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SUPPLIER_NAME: (state, supplierName) => {
      state.supplierName = supplierName
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    LOGIN_NAME: (state, loginName) => {
      state.loginName = loginName
    },
    USER_ID: (state, userId) => {
      state.userId = userId
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MENU: (state, menus) => {
      state.menus = menus
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_INDEXINFO: (state, indexInfo) => {
      state.indexInfo = indexInfo
    }
  },
  actions: {
    // user login
    login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const { data } = response
          // console.log('login', response)
          // commit('SET_TOKEN', data.session)
          setToken(data.session)
          setAccessToken(data.accessToken)
          resolve()
          var userAgent = navigator.userAgent
          if (userAgent.indexOf('DnyERPBrowser') > 0) {
            dyERP.sendLogin(Number(data.userId), data.userName, data.userAccount, (data.deptId).toString(), data.deptName, data.accessToken, data.supplierCode.toString(), data.dataSourceId)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // get user info
    getInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getIndex().then(response => {
          // console.log("getIndex", response.data)
          const data = {
            // avatar: LogoImg,
            introduction: 'I am a super administrator',
            menus: response.data.menus,
            name: response.data.user.userName,
            supplierName:response.data.supplierName,
            loginName: response.data.user.loginName,
            userId: response.data.user.id,
            roles: ['admin'],
            user: response.data.user,
            indexInfo: response.data
          }

          // console.log('data', data)

          if (!data) {
            reject('Verification failed, please Login again.')
          }

          const { roles, name,supplierName, loginName, avatar, menus, user, userId, indexInfo } = data

          // roles must be a non-empty array
          if (!roles || roles.length <= 0) {
            reject('getInfo: roles must be a non-null array!')
          }
          commit('SET_ROLES', roles)
          commit('SUPPLIER_NAME', supplierName)
          commit('SET_NAME', name)
          commit('LOGIN_NAME', loginName)
          commit('USER_ID', userId)
          commit('SET_AVATAR', avatar)
          commit('SET_MENU', menus)
          commit('SET_USER', user)
          commit('SET_INDEXINFO', indexInfo)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出登录
    logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        removeAccessToken()
        window.location.reload()
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   removeToken()
        //   resetRouter()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 删除token
    resetToken({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      })
    }
  }
}

export default user
