import { logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserMkt } from '@/api/article'
import Cookies from 'js-cookie'
const user = {
  state: {
    id: '',
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    mkt: '',
    isvisit: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_MKT: (state, mkt) => {
      state.mkt = mkt
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ISVISIT: (state, isvisit) => {
      state.isvisit = isvisit
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', userInfo.username) // 居然设置的是name
        Cookies.set('Username', userInfo.username)
        setToken(userInfo.token)
        resolve()
        // 调用接口/api/login.js调用mock
        // loginByUsername(username, userInfo.password).then(response => {
        //   console.log(response.data)
        //   // const data = response.data
        //   commit('SET_TOKEN', userInfo.username) // 居然设置的是name
        //   setToken(userInfo.token)
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        // 就是这里！！！权限问题 去掉mock的引用 直接复制此处传入 username 调用后台getrole
        if (!sessionStorage.getItem('source') || sessionStorage.getItem('source') !== 'sso') {
          if (Cookies.get('Username') === undefined || Cookies.get('Username') === 'undefined') {
            location.reload()
          }
        }
        const returnRoles = { roles: [], token: null, introduction: null, avatar: null, name: null }
        getUserMkt().then(response => {
          if (!response.data) { // 由于mockjs 不支持自定义状态码只能这样hack
            reject('error')
          }
          returnRoles.roles[0] = response.data.role
          returnRoles.avatar = 'https://yuesuo.yueguanjia.com/resume/img/logo300.png'
          returnRoles.name = response.data.name
          returnRoles.introduction = response.data.id
          returnRoles.token = '666'
          returnRoles.isvisit = response.data.isvisit
          returnRoles.mkt = response.data.city
          returnRoles.id = response.data.id
          // ------------------------------------------------
          commit('SET_ROLES', returnRoles.roles)
          commit('SET_NAME', returnRoles.name)
          commit('SET_AVATAR', returnRoles.avatar)
          commit('SET_INTRODUCTION', returnRoles.introduction)
          commit('SET_MKT', response.data.city)
          commit('SET_ISVISIT', response.data.isvisit)
          commit('SET_ID', response.data.id)
          resolve(returnRoles)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          window.sessionStorage.removeItem('source')
          window.sessionStorage.removeItem('headers')
          window.localStorage.removeItem('token')
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

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

    // 动态修改权限
    ChangeRoles({ commit }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve()
        })
      })
    }
  }
}

export default user
