import {
  httpServer
} from "@/components/httpServer/httpServer.js";
import router from '@/router/index'
export const user = {
  namespaced: true,
  state: {
    userInfo: {
      uuid: "",
      nickname: "",
      headImg: "",
      authority: "",
    },
    notices: {},
    typeList: [],
    promiseList: [],
    token: "",
  },
  mutations: {
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
      console.log("setUserInfo", userInfo)
    },
    setNotice(state, courseId) {
      state.notices[courseId] = true;
    },
    setPromiseList(state, list){
      state.promiseList = list;
    },
    setToken(state, token) {
      state.token = token
    },
    setTypeList(state, typeList) {
      state.typeList = typeList
    },
    LoginOut(state) {
      state.userInfo = {}
      state.token = ""
      localStorage.uid = ""
      localStorage.token = ""
      sessionStorage.clear()
      if (location.pathname != '/') {
        router.push('/')
      }
    },
    ResetUserInfo(state, userInfo = {}) {
      state.userInfo = { ...state.userInfo,
        ...userInfo
      }
      console.log("ResetUserInfo", userInfo)
    }
  },
  actions: {
    async loadBaseInfo({
      commit,
      router
    }) {
      let res = await httpServer("User.baseInfo", {})
      if (res.code != 0 && res.code != 200) {
        console.log("errLogout", res.code, res.msg)
        commit('LoginOut', {})
        return false
      }
      localStorage.uid = res.data.uid
      localStorage.token = res.data.token
      commit('setUserInfo', res.data)
      commit('setToken', res.data.token)
      return res.data
    },
    async loadAllPromise({
      commit,
      router
    }) {
      let res = await httpServer("Course.GetAllCoursePromise", {})
      commit('setPromiseList', res.data.list || [])
      return true
    },
    async loadTypeList({
      commit,
      router
    }) {
      let res = await httpServer("Course.getTypeList", {})
      commit('setTypeList', res.data || [])
      return true
    },


    async saveBaseInfo({
      commit
    }, userInfo) {
      commit('ResetUserInfo', userInfo)
    },
    setNotice({
      commit
    }, courseId) {
      commit('setNotice', courseId)
    },
    async doLogout({
      commit
    }, userInfo) {
      // let res = await httpServer("Auth.Logout", {} )
      commit('LoginOut', {})
      return true
    },
    async doLogin({
      commit
    }, param) {
      let res = await httpServer("Auth.LoginStudy", param)
      if (res.code == 200) {
        commit('setUserInfo', res.data)
        commit('setToken', res.data.token)
        localStorage.nickname = res.data.nickname;
        localStorage.token = res.data.token;
        localStorage.headImg = res.data.headImg;
        localStorage.uid = res.data.uid;
        return res.data
      }
      return false
    }
  },
  getters: {
    userInfo(state) {
      return state.userInfo
    },
    typeList(state) {
      return state.typeList
    },
    promiseList(state){
      return state.promiseList
    },
    token(state) {
      return state.token
    },
    notices(state) {
      return state.notices
    },

  }
}
