import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    isLogin: false,
    isSetPwd: 0, // 0 未设置 1 已设置
    userInfo: {},
    authInfo: {}, // 个人认证信息
    companyAuthInfo: {}, // 企业认证信息
  },
  getters: {
  },
  mutations: {
    updateIsLogin(state: any, payLoad: any) {
      state.isLogin = payLoad;
    },
    updateUserInfo(state: any, payLoad: any) {
      state.userInfo  = payLoad;
    },
    updateIsSetPwd(state: any, payLoad: any) {
      state.isSetPwd = payLoad;
    },
    updateAuthInfo(state: any, payload: any) {
      state.authInfo = payload;
    },
    updateCompanyAuthInfo(state: any, payload: any) {
      state.companyAuthInfo = payload;
    },
  },
  actions: {
    getUserInfo({ commit }) {
        Vue.prototype.$api.user.getUserInfo().then((res: any) => {
          commit('updateUserInfo', res);
          commit('updateIsLogin', true);
        }).catch((err: any) => {
          commit('updateUserInfo', {});
          commit('updateIsLogin', false);
        });
    },
    isSetPwd({ commit , state}) {
      const { phone, email } = state.userInfo;
      Vue.prototype.$api.user.isSetPwd({phone , email}).then((res: any) => {
        commit('updateIsSetPwd', res.is_set_pwd);
      }).catch((err: any) => {
        commit('updateIsSetPwd', 0);
      });
    },
    setPwd(context: any) {
      Vue.prototype.$api.user.setPwd();
    },
    getAuthInfo({ commit }) {
      Vue.prototype.$api.auth.get().then((res: any) => {
        commit('updateAuthInfo', res);
      });
    },
    getCompanyAuthInfo({ commit }) {
      Vue.prototype.$api.auth.info().then((res: any) => {
        commit('updateCompanyAuthInfo', res);
      });
    },
  },
  modules: {
  },
});
