import { httpServer } from "@/components/httpServer/httpServer.js";
export const user = {
    namespaced: true,
    state: {
        userInfo: {
            uuid: "",
            nickname: "",
            headImg: "",
            authority: "",
        },
        typeList:[],
        planList:[],
        agentId:0,
        token: "",
    },
    mutations: {
        setUserInfo(state, userInfo) {
          state.userInfo = userInfo
        },
        SetAgentId(state, agentId){
          state.agentId = agentId
        },
        setToken(state, token) {
          state.token = token
        },
        setTypeList(state, typeList) {
           state.typeList = typeList
        },
        setPlanList(state, planList){
          state.planList = planList
        },
        LoginOut(state) {
            state.userInfo = {}
            state.token = ""
            sessionStorage.clear()
            router.push({ name: '/', replace: true })
            window.location.reload()
        },
        ResetUserInfo(state, userInfo = {}) {
            state.userInfo = {...state.userInfo,
                ...userInfo
            }
        }
    },
    actions: {
        async loadBaseInfo({ commit, router }) {
          let res = await httpServer("User.baseInfo", {})
          if( res.code != 0 && res.code != 200){
            router.push( '/')
            return true
          }
          localStorage.uid = res.data.uid
          localStorage.token  = res.data.token
          commit('setUserInfo', res.data)
          commit('setToken', res.data.token)
          return res.data
        },
        async loadTypeList({ commit, router }) {
          let res = await httpServer("Course.getTypeList", {})
          commit('setTypeList', res.data||[])
          return true
        },
        async loadPlanList({ commit, router }) {
          let res = await httpServer("Train.getPlanList", {})
          commit('setPlanList', res.data||[])
          return true
        },
        async saveBaseInfo({ commit }, userInfo) {
          commit('ResetUserInfo', userInfo)
        },
        async setAgentId({ commit }, agentId) {
          commit('SetAgentId', agentId)
        },
        async doLogout({ commit }, userInfo) {
          commit('setUserInfo', {})
          commit('setToken', "")
          localStorage.nickname = "";
          localStorage.token = "";
          localStorage.headImg = "";
          localStorage.uid = "";
          return true
        },
        async doLogin({ commit }, param ) {
          console.log("loginStart", res )
          let res = await httpServer("Auth.LoginStudy", param )
          console.log("loginOk", res )
          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
        },
        planList(state) {
          return state.planList
        },
        token(state) {
            return state.token
        },
        agentId(state){
          return state.agentId
        }

    }
}
