export const state = () => ({
  token: "",
  // email: "",
  userInfo: {},
  dialogVisible: false,
  compareList:[],
  jumpList:'contact'
});
export const mutations = {
  setToken(state, token) {
    state.token = token;
    if (this.$cookies.get('can-use-cookie')) this.$cookies.set("token", token, { maxAge: 2592000, path: '/' });
  },
  getToken(state) {
    if (this.$cookies.get('can-use-cookie')) state.token = this.$cookies.get("token");
  },
  setUserInfo(state, data) {
    state.userInfo = data;
    // state.email = data.email;
    if (this.$cookies.get('can-use-cookie')) this.$cookies.set("user-info", data,  { maxAge: 2592000, path: '/' });
  },
  clearUserInfo(state) {
    state.userInfo = {};
    // state.email = "";
    state.token = "";
    this.$cookies.remove("token");
    this.$cookies.remove("user-info");
  },
  openDialog(state) {
    state.dialogVisible = true;
  },
  closeDialog(state) {
    state.dialogVisible = false;
  },
  setCompareList(state,data) {
    state.compareList = data;
  },
  clearCompareList(state) {
    state.compareList = [];
  },
};
export const actions = {
  login({ commit, dispatch }, data) {
    return new Promise((resolve, reject) => {
      if (data) {
        this.$axios({
          url: "/uk-api/uk/login",
          method: "POST",
          data,
        }).then((res) => {
          commit("setToken", res.result.token);
          dispatch("getUserInfo");
          resolve(res);
        });
      }
    }).catch(() => {
      // reject(error);
    });
  },
  getUserInfo({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      this.$axios({
        url: "/uk-api/uk/member/detail",
        method: "GET",
      }).then((res) => {
        commit("setUserInfo", res.result);
        resolve(res);
        
        if(location.pathname.indexOf('contact')>-1){
          location.href = location.origin
        }
      });
    }).catch(() => {
      // reject(error);
    });
  },
  logout({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      //未有退出登录接口
      commit("clearUserInfo");
      resolve();
    }).catch(() => {
      // reject(error);
    });
  },
};
