import axios from "axios";

//store/modules/user.js

// 声明变量
const state = {
  collapse: false,
  activeIndex: 1,
  username: "",
  password: "",
  // api: "/carstop/carbook", // 线上
  api: "/hotelReservation", // 测试
  islogin: 1,
  hasRoutes: false,
  level: 1,
};

// 修改变量（state不能直接赋值修改，只能通过mutations）
const mutations = {
  menuClose: (state, value) => {
    state.collapse = value;
  },
  indexUp: (state, value) => {
    state.activeIndex = value;
  },
  userName: (state, value) => {
    state.username = value;
  },
  passWord: (state, value) => {
    state.password = value;
  },
  SET_ROUTES_STATE: (state, value) => {
    state.hasRoutes = value;
  },
  LEVEL: (state, value) => {
    state.level = value;
    console.log("设置level", value);
  },
};
// mutations的值由actions传入(异步)
const actions = {
  newsAsync: async ({ commit, state }) => {
    let data = new FormData();
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/usermesUnRead.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
      data: data,
    });
    console.log(res, "系统消息");

    commit("newsUp", res.data.u_count);
    sessionStorage.setItem("newsNum", res.data.u_count);
    console.log("vuex", res.data.u_count);
  },
  // 系统扫码时间
  login: async ({ commit, state }, data) => {
    return new Promise(async (resolve, reject) => {
      let res = await axios({
        method: "get",
        url: state.api + "/mhotel/managerAdminloginAdmin.action",
        params: data,
      });

      console.log(res, "登录账号");
      if (res.data.code == 200) {
        sessionStorage.setItem("name", data.password);

        sessionStorage.setItem("token", res.data.data.managerId);
        sessionStorage.setItem("roomId", res.data.data.id);
        sessionStorage.setItem("hotelName", res.data.data.hotelName);
        sessionStorage.setItem("hotelTownship", res.data.data.hotelTownship);
        // 1 是  0 否
        if (res.data.isLogin == 1) {
          sessionStorage.setItem("isLogin", 1);
        } else {
          sessionStorage.setItem("isLogin", 0);
        }
        sessionStorage.setItem("username", res.data.data.adminName);
        commit("LEVEL", res.data.data.level);

        commit("userName", data.user);

        // commit("roles", asyncRouter);
        // sessionStorage.setItem("roles", JSON.stringify(asyncRouter));

        resolve("success");
      } else {
        resolve({ name: "reject", data: res });
      }
    });
  },
};

export default {
  state,
  mutations,
  actions,
};
