import { loginByUsername, logout } from "@/api/login";
// import { removeKeyToken, setKeyToken } from "@/auth/auth";
// import { omit } from "lodash-es";
import { LOGIN_BY_USERNAME, LOG_OUT, FED_LOG_OUT } from "../action-types";
import { setToken } from "@/auth/auth";

const user = {
  state: {
    loginName: "",
    name: "",
  },

  mutations: {
    SET_USER: (state, user) => {
      // state.user = user;
      state = Object.assign(state, user);
    },
    SET_SMS_SEND_DATE: (state, date) => {
      state = Object.assign(state, { smsSendDate: date });
    },
  },

  actions: {
    // 用户名登录
    [LOGIN_BY_USERNAME]({ commit }, userInfo) {
      return loginByUsername(userInfo).then((response) => {
        const data = response.data.data;
        commit("SET_USER", { ...data.account, roleCodes: data.roleCodes });
        setToken(data.accessToken);
      });
    },

    // 登出
    [LOG_OUT]({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_USER", null);
            // removeKeyToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 前端 登出
    [FED_LOG_OUT]({ commit }) {
      return new Promise((resolve) => {
        commit("SET_USER", null);
        // removeKeyToken();
        resolve();
      });
    },
  },
};

export default user;
