import { getToken, clearStorage } from '../../utils/auth';
import { resetRouter } from '../../router';
import { getBaseInfo } from '@/api/baseInfo'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  serviceType: '',
  companyLogo: '',
  baseInfo: {}
};

const mutations = {
  // 设置基础信息
  SET_BASE_INFO: (state,baseInfo) => {
    state.baseInfo = {...baseInfo};
  },
  SET_TOKEN: (context, token) => {
    context.token = token;
  },
  SET_LOGO: (context, logo) => {
    context.companyLogo = logo;
  },
  SET_ROLES: (context, roles) => {
    context.roles = roles;
  },
  SET_SERVICE_TYPE: (context, serviceType) => {
    context.serviceType = serviceType;
    localStorage.setItem('set_service_type', serviceType)
  },
};

const actions = {
  logout({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '');
      commit('SET_LOGO', '');
      commit('SET_ROLES', []);
      commit('SET_SERVICE_TYPE', '');
      clearStorage();
      resetRouter();
      resolve();
    });
  },
  serviceType({ commit }, view) {
    commit('SET_SERVICE_TYPE', view)
  },
  async getBaseInfo({commit}){
    // debugger
    let baseInfo = await getBaseInfo();
    commit('SET_BASE_INFO',baseInfo)
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
