import { defineStore } from "pinia";
import {
  login,
  shopStaffInfo,
  shopInfo_detail,
  logout,
  shopExtendDetail,
  shopPagePermissionMine,
} from "@/api/account.js";
import useStorage from "@/utils/useStorage";
import { useSocket } from "@/store/socket.js";
import { useRoute, useRouter } from "vue-router";

export const useUser = defineStore("user", {
  state: () => ({
    userInfo: useStorage.get("userInfo"),
    shopInfo: useStorage.get("shopInfo"),
    token: useStorage.get("token"),
    loginType: useStorage.get("loginType") || 0,
    menus: [
      {
        label: "收银",
        path: "/",
        state: 0,
        icon: "ShoppingCartFull",
      },
    ],
    menuList: [
      {
        label: "台桌",
        path: "/table",
        icon: "Reading",
      },
      {
        label: "团购",
        path: "/group_buy",
        icon: "Handbag",
      },
      {
        label: "订单",
        path: "/order",
        icon: "Tickets",
      },
      {
        label: "会员",
        path: "/member",
        icon: "User",
      },
      {
        label: "排队",
        path: "/queue",
        icon: "Timer",
      },
    ],
    route: null,
    router: null,
  }),
  actions: {
    // 登录
    userlogin(param) {
      return login(param).then(async (res) => {
        this.token = res.tokenInfo.tokenValue;
        useStorage.set("token", this.token);

        const logo = shopExtendDetail({ autoKey: "ticket_logo" });
        useStorage.set("shopInfo", {
          ...res.shopInfo,
          ticketLogo: logo ? logo.value : "",
        });
        this.shopInfo = useStorage.get("shopInfo");
        useStorage.set("loginType", param.loginType);
        this.shopPagePermissionMineAjax(param.loginType);
        return await this.shopStaffInfo();
      });
    },
    // 初始化路由信息
    initRoute() {
      this.route = useRoute();
      this.router = useRouter();
    },
    // 获取当前员工已拥有页面路径
    async shopPagePermissionMineAjax(loginType = useStorage.get("loginType")) {
      try {
        if (this.route == null) {
          this.initRoute();
        }
        if (loginType == 0) {
          this.menus[0].state = 1;
          this.menus.push(...this.menuList);
        } else {
          const res = await shopPagePermissionMine();

          let pathFlag = true;
          res.map((item) => {
            if (item.path == "/") {
              this.menus[0].state = 1;
            }

            if (this.route.path != item.path) {
              pathFlag = false;
            }

            this.menuList.map((menu) => {
              if (item.path === menu.path) {
                this.menus.push(menu);
              }
            });
          });

          console.log("当前路由===", this.route);
          if (!pathFlag) {
            this.router.push("/");
          }
        }
      } catch (error) {
        console.log("获取当前员工已拥有页面路径失败===", error);
      }
    },
    // 获取用户信息
    async shopStaffInfo() {
      try {
        const res = await shopStaffInfo();
        useStorage.set("userInfo", res);
        this.userInfo = useStorage.get("userInfo");
      } catch (error) {
        console.log(error);
      }
    },
    // 更新店铺信息
    async getShopInfo() {
      try {
        const res = await shopInfo_detail();
        // 获取标签小票的logo
        const logo = await shopExtendDetail({ autoKey: "ticket_logo" });
        useStorage.set("shopInfo", {
          ...res,
          ticketLogo: logo ? logo.value : "",
        });
        this.shopInfo = useStorage.get("shopInfo");
      } catch (error) {
        console.log(error);
      }
    },
    // 退出登录
    async logout() {
      try {
        const socket = useSocket();
        await logout();
        socket.close();

        useStorage.del("userInfo");
        useStorage.del("shopInfo");
        useStorage.del("token");
        useStorage.del("douyin");
        useStorage.del("categoryIndex");
        useStorage.del("tableCode");

        this.userInfo = {};
        this.shopInfo = {};
        this.token = "";
        this.menus = [
          {
            label: "收银",
            path: "/",
            state: 0,
            icon: "ShoppingCartFull",
          },
        ];
      } catch (error) {
        console.log(error);
      }
    },
  },
});
