import { loginByUsername, logout, getUserInfo,verifyCode  } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    activeSys: '',
    introduction: '',
    roles: [],
    perms: [],
    setting: {
      articlePlatform: []
    },
    isIndexNoticeLoaded:false//首页信息是否加载
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_NOTICE_LOADED:(state,isLoaded)=>{
      state.isIndexNoticeLoaded=isLoaded
    },
    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_FRAID: (state, fraId) => {
      state.fraId = fraId
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMS: (state, perms) => {
      state.perms = perms
    },
    SET_ACTIVESYS: (state, perms) => {
      state.activeSys = perms
    }
  },

  actions: {
    verifyCode({commit}) {
      return new Promise((resolve, reject) => {
        verifyCode().then(response => {
          const token = response.headers['session_id']
          commit('SET_TOKEN', token)
          setToken(token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        loginByUsername(username, userInfo.password,userInfo.verifyCode).then(response => {
          let errorno = response.data.errorno;

            const token = response.data.data.token;
            window.localStorage.setItem('userInfo', JSON.stringify(response.data.data.adminInfo));
            commit('SET_TOKEN', token)
            commit('SET_ACTIVESYS', response.data.data.activeSys)
            setToken(token,1)
            resolve()

        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          const data = response.data.data

          // if (data.perms && data.perms.length > 0) { // 验证返回的perms是否是一个非空数组
          //   commit('SET_PERMS', data.perms)
          // } else {
          //   reject('getInfo: perms must be a non-null array !')
          // }

          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_FRAID', data.fraId)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve(response)
        }).catch(error => {
          //清理用户数据
          removeToken();

          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(() => {
          console.log(1);
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMS', [])
          removeToken()
          resolve()
        //  location.reload() // 为了重新实例化vue-router对象 避免bug
        }).catch(error => {
          reject(error)
        })
      })
    },

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

export default user
