// import { login, logout, getInfo } from '@/api/login';
import { login, _resetPwd } from '@/api/login'
import { TokenKey, getToken, setToken, removeToken, setLocal, setCookie, setSession, getSession, removeSession } from '@/utils/auth'
import { _list, _view, _export, _add, _adds, _up, _ups, _del } from '@/api/app'
import useAppStore from '@store/modules/app'

// import md5 from 'js-md5'
// import Cookies from 'js-cookie'

const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken(),
    roles: getSession('roles') || [],
    permissions: [],
    name: getSession('nickname') || '',
    account: getSession('user_account') || '',
    nickname: getSession('nickname') || '',
    accountid: getSession('accountid') || '',
    accountlx: getSession('accountlx') || '',
    agentsid: getSession('agentsid') || '',
    userData: getSession('userData') || '',
  }),
  actions: {
    // 登录
    login(userInfo) {
      const account = userInfo.account.trim()
      // const pwd = md5(userInfo.pwd)
      const pwd = userInfo.pwd
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise<void>((resolve, reject) => {
        login({ account, pwd, code, uuid })
          .then(({ Data, Status }) => {
            setToken(Data.token)
            this.token = Data.token
            console.log(Data, 'Data')
            setSession('tokenTimeout', Data.timeout) // token过期时间
            setSession('user_account', Data.account) // 用户账号
            setSession('nickname', Data.nickname) // 用户昵称
            setSession('accountid', Data.accountid) // 用户id
            setSession('accountlx', Data.accountlx) // 用户类型
            setSession('agentsid', Data.agentsid) // 代理商id
            setSession('roles', Data.grouplist ? Data.grouplist.split(',') : []) // 用户角色
            setSession('popedom', window.btoa(window.btoa(Data.popedom))) // 用户权限
            setSession('userData', JSON.stringify(Data)) // 用户权限
            this.name = Data.nickname
            this.account = Data.account
            this.nickname = Data.nickname
            this.accountid = Data.accountid
            this.accountlx = Data.accountlx
            this.agentsid = Data.agentsid
            this.roles = Data.grouplist ? Data.grouplist.split(',') : []
            // this.permissions = window.btoa(window.btoa(Data.popedom))
            resolve()
            setTimeout(() => {
              useAppStore().getOrganizationView()
            }, 2000);
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 退出系统
    logOut() {
      return new Promise<void>((resolve, reject) => {
        this.token = ''
        this.roles = []
        this.permissions = []
        removeSession(TokenKey)
        removeSession('visitedTags')
        removeToken()
        resolve()
      })
    },
    // 重置密码
    resetPwd(data) {
      return new Promise((resolve, reject) => {
        _resetPwd(data)
          .then((res) => {
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
  },
})

export default useUserStore
