import axios from "axios";

import router from "@/router";
import Layout from "@/layout/index.vue";

// 声明变量
const state = {
  collapse: false,
  activeIndex: 1,
  username: "",
  password: "",
  // api: "/carstop/carbook", // 线上
  api: "/hotelReservation", // 测试
  islogin: 1,
  hasRoutes: false,
  level: 1,
  // statRoutes: [
  //   {
  //     path: "/",
  //     name: "Layout",
  //     redirect: "/login",
  //     alwaysShow: true,
  //     component: Layout,
  //     children: [
  //       // {
  //       //     path: 'home',
  //       //     name: 'Home',
  //       //     meta: {
  //       //         isAuth: true,
  //       //         title: '主页',
  //       //         keepAlive: true //缓存组件
  //       //     },
  //       //     component: () => import('@/views/home'),
  //       // },、

  //       {
  //         path: "roomStatus",
  //         name: "roomStatus",
  //         meta: { isAuth: true, title: "房态管理" },
  //         component: () => import("@/views/roomStatus"),
  //       },
  //       {
  //         path: "roomType ",
  //         name: "roomType",
  //         meta: { isAuth: true, title: "房型管理" },
  //         component: () => import("@/views/roomType"),
  //       },
  //       {
  //         path: "roomPrice",
  //         name: "roomPrice",
  //         meta: { isAuth: true, title: "房价管理" },
  //         component: () => import("@/views/roomPrice"),
  //       },
  //       {
  //         path: "order",
  //         name: "order",
  //         meta: { isAuth: true, title: "订单管理" },
  //         component: () => import("@/views/order"),
  //       },
  //       {
  //         path: "class",
  //         name: "class",
  //         meta: { isAuth: true, title: "课表管理" },
  //         component: () => import("@/views/class"),
  //       },
  //       {
  //         path: "process",
  //         name: "process",
  //         meta: { isAuth: true, title: "流程管理" },
  //         component: () => import("@/views/process"),
  //       },
  //       {
  //         path: "statement",
  //         name: "statement",
  //         meta: { isAuth: true, title: "统计报表" },
  //         component: () => import("@/views/statement"),
  //       },
  //       {
  //         path: "doorLock",
  //         name: "doorLock",
  //         meta: { isAuth: true, title: "门锁管理" },
  //         component: () => import("@/views/doorLock"),
  //       },
  //       {
  //         path: "meter",
  //         name: "meter",
  //         meta: { isAuth: true, title: "水电表管理" },
  //         component: () => import("@/views/meter"),
  //       },
  //       {
  //         path: "system",
  //         name: "system",
  //         meta: { isAuth: true, title: "系统通知" },
  //         component: () => import("@/views/system"),
  //       },
  //       {
  //         path: "home/ICcard",
  //         name: "icCard",
  //         meta: { isAuth: true, title: "IC卡管理" },
  //         component: () => import("@/views/icCard"),
  //       },
  //       {
  //         path: "home/fingerprint",
  //         name: "fingerprint",
  //         meta: { isAuth: true, title: "指纹管理" },
  //         component: () => import("@/views/fingerprint"),
  //       },
  //     ],
  //   },
  // ],
  statRoutes: [
    {
      path: "/hotel",
      name: "hotel",
      component: Layout,
      meta: { isAuth: true, title: "公寓", icons: "" },
      children: [
        {
          path: "roomStatus",
          name: "roomStatus",
          meta: { isAuth: true, title: "房态管理" },
          component: () => import("@/views/roomStatus/roomStatus.vue"),
        },
        {
          path: "order",
          name: "order",
          meta: { isAuth: true, title: "订单管理" },
          component: () => import("@/views/order/order.vue"),
        },

        {
          path: "statement",
          name: "statement",
          meta: { isAuth: true, title: "统计报表" },
          component: () => import("@/views/statement/statement.vue"),
        },
        {
          path: "account",
          name: "account",
          meta: { isAuth: true, title: "账号管理" },
          component: () => import("@/views/account/account.vue"),
        },
        {
          path: "system",
          name: "system",
          meta: { isAuth: true, title: "系统设置" },
          component: () => import("@/views/system/system.vue"),
        },
        // {
        //   path: "icCard",
        //   name: "icCard",
        //   meta: { isAuth: true, title: "IC卡管理" },
        //   component: () => import("@/views/icCard/icCard.vue"),
        // },
        // {
        //   path: "fingerprint",
        //   name: "fingerprint",
        //   meta: { isAuth: true, title: "指纹管理" },
        //   component: () => import("@/views/fingerprint/fingerprint.vue"),
        // },
      ],
    },
    // 房型管理
    {
      path: "/hotel",
      component: Layout,
      meta: { isAuth: true, title: "房型管理", icons: "roomType" },
      children: [
        {
          path: "roomType",
          name: "roomType",
          meta: { isAuth: true, title: "房型管理" },
          component: () => import("@/views/roomType/roomType.vue"),
        },
        {
          path: "roomPrice",
          name: "roomPrice",
          meta: { isAuth: true, title: "房价管理" },
          component: () => import("@/views/roomPrice/roomPrice.vue"),
        },
      ],
    },
    // 授权管理
    {
      path: "/hotel",
      component: Layout,
      meta: { isAuth: true, title: "授权管理", icons: "class" },
      children: [
        {
          path: "class",
          name: "class",
          meta: { isAuth: true, title: "课表管理" },
          component: () => import("@/views/class/class.vue"),
        },
        {
          path: "process",
          name: "process",
          meta: { isAuth: true, title: "流程管理" },
          component: () => import("@/views/process/process.vue"),
        },
      ],
    },
    // 设备管理
    {
      path: "/hotel",
      component: Layout,
      meta: { isAuth: true, title: "设备管理", icons: "doorLock" },
      children: [
        {
          path: "doorLock",
          name: "doorLock",
          meta: { isAuth: true, title: "门锁管理" },
          component: () => import("@/views/doorLock/doorLock.vue"),
        },
        {
          path: "meter",
          name: "meter",
          meta: { isAuth: true, title: "水电表管理" },
          component: () => import("@/views/meter/meter.vue"),
        },
      ],
    },
    // 组织架构
    {
      path: "/hotel",
      component: Layout,
      meta: { isAuth: true, title: "组织架构", icons: "user" },
      children: [
        {
          path: "user",
          name: "user",
          meta: { isAuth: true, title: "用户管理" },
          component: () => import("@/views/user/user.vue"),
        },
        {
          path: "roles",
          name: "roles",
          meta: { isAuth: true, title: "角色管理" },
          component: () => import("@/views/roles/roles.vue"),
        },
      ],
    },
  ],
};

// 修改变量（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);
        // 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,
};
