// pythonBridge.js
import { ref, onMounted } from 'vue';
const bridge = ref(null);
// 初始化 QWebChannel
const initBridge = () => {
  if (/localhost/.test(window.location.href)) return
  new QWebChannel(qt.webChannelTransport, (channel) => {
    bridge.value = channel.objects.bridge;
  });
};
export function usePythonBridge() {




  // 调用 Python 方法
  const fetchDataConfig = (data) => {
    return new Promise((resolve, reject) => {
      if (bridge.value) {
        bridge.value.fetchDataConfig(data, function (result) {
          resolve(result);
        });
      }


    });
  };

  // 查询获取主播的数据
  const fetchDataCount = () => {
    return new Promise((resolve, reject) => {
      if (bridge.value) {
        bridge.value.fetchDataCount(function (result) {
          resolve(result);
        });
      }
    });
  };

  // 打开tk后台
  const loginTikTok = () => {
    if (bridge.value) {
      bridge.value.loginTikTok(function (result) {

      });
    }

  };

  // 登录tk后台
  const loginBackStage = (data) => {
    if (bridge.value) {
      if (data.index == 0) {
        bridge.value.loginBackStage(JSON.stringify(data));
      } else if (data.index == 1) {
        bridge.value.loginBackStageCopy(JSON.stringify(data));
      }

    }
  };

  //跳转到主播页面
  const givePyAnchorId = (id) => {

    if (bridge.value) {
      bridge.value.givePyAnchorId(id, function (result) {

      });
    }
  };

  //查询登录状态
  const backStageloginStatus = () => {
    return new Promise((resolve, reject) => {
      if (bridge.value) {
        bridge.value.backStageloginStatus(function (result) {
          resolve(result);
        });
      }
    });

  };
  //查询登录状态
  const backStageloginStatusCopy = () => {
    return new Promise((resolve, reject) => {
      if (bridge.value) {
        bridge.value.backStageloginStatusCopy(function (result) {
          resolve(result);
        });
      }
    });

  };

  //导出表格
  const exportToExcel = (data) => {
    if (bridge.value) {
      bridge.value.exportToExcel(JSON.stringify(data));
    }

  };


  // 在组件挂载时初始化桥接
  onMounted(initBridge);

  return {
    fetchDataConfig,
    fetchDataCount,
    loginBackStage,
    loginTikTok,
    givePyAnchorId,
    backStageloginStatus,
    backStageloginStatusCopy,
    exportToExcel
  };
}