// 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: {},
  };
};

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_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", //cs1111
						"6c56ec0e-2c74-11ef-bee5-005056b86db5",
            "3efa8590-1719-11ef-ade9-005056b86db5",
            "d248c21c-50fc-11ed-8c78-005056b86db5", //李毓竹
            "d248c8cf-50fc-11ed-8c78-005056b86db5", //程鹏
            "d248cbe2-50fc-11ed-8c78-005056b86db5", //兰薇
            "b7bd1863-2f87-11ef-bf55-005056b86db5", //黄丽惠
            "bfbe1913-2f87-11ef-bf55-005056b86db5",//彭晓彬
            "c52be2c7-9819-11ef-9b30-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);
        });
    });
  },
  // get user info
  // getInfo({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getUser()
  //       .then(response => {
  //         const { data } = response

  //         if (!data) {
  //           reject('验证失败，请重新登录。')
  //         }
  //         console.log(response)

  //         const { id, roles, is_init, cn, avatar, username } = response.data
  //         // roles must be a non-empty array
  //         if (!roles || roles.length <= 0) {
  //           reject('getInfo: 角色必须为非空数组！')
  //         }
  //         commit('SET_NAME', cn)
  //         commit('SET_USERNAME', username)
  //         commit('SET_ROLES', roles)
  //         commit('SET_ID', id)
  //         commit('SET_AVATAR', avatar)
  //         commit('SET_IS_INIT', is_init)
  //         resolve(data)
  //       })
  //       .catch(error => {
  //         reject(error)
  //       })
  //   })
  // },

  // user logout
  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,
};
