// import { user_info, login } from '@/api/user'
import { eduGet, getUser } from "@/api/user";
import { getToken, removeToken, setToken } from "@/utils/auth";

// import { Message } from 'element-ui'
// import { loginOut } from '@/api/user';

const getDefaultState = () => {
  return {
    token: getToken(),
    id: "",
    roles: [],
    name: "",
    username: "",
    avatar: "",
    is_init: false,
    userinfo: {},
    // 应用权限
    fromL: {
      basics: {
        title: "",
        brief: "",
        logo: "",
      },
      //桌面  管理平台首页  管理平台侧边栏
      desktop: {
        list: [],
      },
      admin: {
        index: {
          list: [],
        },
        sidebar: {
          list: [],
        },
      },
    },
    // 应用标识
    appSign:''

  };
};

const state = getDefaultState();
const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_ID: (state, id) => {
    state.id = id;
  },
  SET_FROM: (state, from) => {
    state.fromL = from;
  },
  SET_AppSIGN: (state, sign) => {
    state.appSign = sign;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_USERNAME: (state, name) => {
    state.username = name;
  },
  SET_IS_INIT: (state, is_init) => {
    state.is_init = is_init;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_USERINFO: (state, userinfo) => {
    state.userinfo = userinfo;
  },
};

const actions = {
  login({ commit }) { //, dispatch
    return new Promise((resolve, reject) => {
      eduGet()
        .then((response) => {
          console.log(response);
          var _user = response.data[0][0];
          // const allowedIds = [
          //   "0abcb118-9110-11eb-80ad-005056b86db5",
          //   "b7ccc834-aae5-11ea-a8d4-52540005ab01",
          //   "a77e9404-efec-11e9-96f9-028edca3b798",
          //   "0e8d50c3-50b5-2f1c-9f1a-bd346a0bf817",
          //   "5943e08c-b7d4-11ed-8d51-005056b86db5"
          // ];
          // if (allowedIds.includes(_user.userid)) {
            commit("SET_ID", _user.userid);
            commit("SET_TOKEN", _user.userid);
            setToken(_user.userid);
            resolve(_user.userid);
            
          // } else if(_user.userid){
          //   loginOut()
          //   .then(async () => {
          //     dispatch('user/logout', null, { root: true });
          //   })
          //   .catch(err => {
          //     console.error(err);
          //   });
          //   Message({
          //     message: "该用户ID不允许登录",
          //     type: "error"
          //   });
          //   reject("该用户ID不允许登录");
          // }
        })
        .catch((error) => {
          // var _user = { userid: "6c56ec0e-2c74-11ef-bee5-005056b86db5" };
          // commit("SET_ID", _user.userid);
          // commit("SET_TOKEN", _user.userid);
          // setToken(_user.userid);
          // resolve(_user.userid);
          reject(error);
        });
    });
  },
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      // getToken()
      commit("SET_ID", state.token);

      getUser({ userid: state.token })
        .then((response) => {
          console.log(response);
          // const { data } = response
          const data = response.data[0][0];
          if (!data) {
            reject("验证失败，请重新登录。");
          }
          // console.log(response)
          commit("SET_USERINFO", data);
          resolve(data);
          // resolve()
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  logout({ commit }) {
    return new Promise((resolve) => {
      commit("SET_TOKEN", "");
      commit("SET_ROLES", []);
      removeToken();
      commit("RESET_STATE");
      resolve();
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      commit("SET_TOKEN", "");
      commit("SET_ROLES", []);
      removeToken();
      resolve();
    });
  },
};

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