import { login, userinfo } from "@/apis/user";
import {
  getToken,
  setToken,
  removeToken,
  getUser,
  setUser,
  removeUser,
  getLevel,
  setLevel,
  removeLevel,
  getBtn,
  setBtn,
  removeBtn,
} from "@/utils/auth";
const state = {
  token: getToken(),
  name: getUser(),
  level: getLevel(),
  btnList: getBtn(),
};

const mutations = {
  SET_TOKEN: (state, token) => {
    if (token) {
      setToken(token);
    } else {
      removeToken();
    }
    state.token = token;
  },
  SET_NAME: (state, name) => {
    if (name) {
      setUser(name);
    } else {
      removeUser();
    }
    state.name = name;
  },
  SET_LEVEL: (state, level) => {
    if (level) {
      setLevel(level);
    } else {
      removeLevel();
    }
    state.level = level;
  },
  SET_BTN: (state, Btn) => {
    if (Btn.length > 0) {
      setBtn(JSON.stringify(Btn));
    } else {
      removeBtn();
    }
    state.btnList = Btn;
  },
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password })
        .then((response) => {
          const { code, data } = response;
          const { token } = data;
          if (code === 0) {
            commit("SET_TOKEN", token);
          }
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  async loginByToken({commit}, token) {
    commit('SET_TOKEN',token)
  },
  UserInfor({ commit }, {}) {
    return new Promise(async (resolve, reject) => {
      userinfo({})
        .then((response) => {
          const { code, data } = response;
          const { nickname, level } = data;
          if (code === 0) {
            commit("SET_NAME", nickname);
            commit("SET_LEVEL", level);
            const Btn = setBtnList(level);
            // console.log(Btn);
            commit("SET_BTN", Btn);
            resolve("success");
          } else {
            resolve("err");
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      commit("SET_TOKEN", "");
      commit("SET_NAME", "");
      commit("SET_LEVEL", "");

      // reset visited views and cached views
      // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
      dispatch("tagsView/delAllViews", null, { root: true });
      resolve();
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      commit("SET_TOKEN", "");
      commit("SET_NAME", "");
      commit("SET_LEVEL", "");
      resolve();
    });
  },
};
function setBtnList(level) {
  let btn = [];
  const str = level ?? "0";
  switch (str + "") {
    case "0":
      btn = [];
      break;
    case "2":
      btn = ["001", "003"];
      break;
    case "3":
      btn = ["001", "003"];
      break;
    case "1":
      btn = ["001", "002", "003"];
      break;
  }
  console.log(btn);
  return btn;
}

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