import { defineStore } from "pinia";
import { store } from "@/stores";
import https from "@/https";
import socketApi from "@/socket/socketApi";
import storage, { actions } from "@/utils/storage";
import http from "@/https";

export const useBaseStore = defineStore("base", {
  state() {
    return {
      shareLinkUrl: <any>"",
      deferredPrompt: <any>{},
      // 添加主屏幕
      showAddToHomeVisible: false,
      globalConfig: <any>{},
      // 转盘配置
      turntableConfig: <any>{},
      // 抽奖结果
      turntableResult: <any>{},
      // 转盘帮助列表
      helpList: <Array<any>>[],
      // 帮助列表总数
      helpTotal: 0,
      // 是否刷新转盘数据
      refreshTurntable: false,
      // 页面可视高度
      innerHeight: 0,
      innerWidth: 0,
      // 群组
      groupList: <any>[],
    };
  },
  actions: {
    async setShareLinkUrl() {
      const res = await https.getShareLink();
      if (res.code != 0) {
        this.shareLinkUrl = "https://18n25.com/";
      }
      if (res.data.url) {
        this.shareLinkUrl = res.data.url;
      } else {
        this.shareLinkUrl = "https://18n25.com/";
      }
    },
    setDeferredPrompt(e: any) {
      this.deferredPrompt = e;
    },
    setShowAddToHomeVisible(status: boolean) {
      this.showAddToHomeVisible = status;
    },
    // 平台配置
    async setGlobalConfig() {
      const res: ResultData = await https.getPlatformConfig();
      this.globalConfig = res.data;
      console.log(this.globalConfig, "globalConfig");
      return { data: res.data };
    },
    // 获取转盘配置
    async getHelpTurntable() {
      let res;
      const token = storage.getStorage(actions.USER_TOEKN);
      if (token) {
        res = await https.getHelpTurntable({ recordSize: 100 });
        console.log(res, "------登录后转盘配置--------");
      } else {
        res = await https.getHelpTurntable({ recordSize: 100 });
        console.log(res, "------未登录转盘配置--------");
      }
      if (res.code !== 0) {
        return;
      }
      this.turntableConfig = res.data;
    },

    // 获取抽奖结果
    async getHelpTurntableResult() {
      const res = await http.getHelpTurntableResult();
      this.turntableResult = res.data;
    },
    // 清除转盘信息
    clearTurntableInfo() {
      this.turntableResult = {};
      this.helpList = [];
      this.helpTotal = 0;
    },
    //动态刷新转盘数据
    setRefreshTurntable(status: boolean) {
      this.refreshTurntable = status;
    },
    // 获取转盘帮助列表
    async getHelpTurntable2Log(data: any) {
      // const res = await http.getHelpTurntable2Log(data);
      // if (res.code !== 0) {
      //   return { state: false, total: 0, msg: res.txt, data: [] };
      // }
      // if (res.data.help2Peoples) {
      //   this.helpTotal = res.data.help2Peoples.total;
      //   return {
      //     state: true,
      //     total: res.data.help2Peoples.total,
      //     msg: res.txt,
      //     data: res.data.help2Peoples.rows,
      //   };
      // }
    },
    // 刷新帮助列表
    async refreshHelpList(data: any) {
      // const res = await this.getHelpTurntable2Log(data);
      // if (res?.state) {
      //   this.helpList = res.data;
      // }
    },
    // 加载更多列表
    async loadMoreHelpList(data: any) {
      // const res = await this.getHelpTurntable2Log(data);
      // if (res?.state) {
      //   const _list = res.data;
      //   _list.forEach((item: any) => {
      //     this.helpList.push(item);
      //   });
      // }
    },
    // 设置页面可视
    setInnerInfo(height: number, width: number) {
      this.innerHeight = height;
      this.innerWidth = width;
    },
    // 设置群组
    async getCustomerGroup() {
      const res = await https.getCustomerGroup();
      console.log(res, "?");
      if (res.code !== 0) return;
      this.groupList = res.data;
    },
  },
});

// Need to be used outside the setup
export const useBaseStoreWithOut = () => {
  return useBaseStore(store);
};
