<template>
  <div class="taskCenter">
    <template v-if="batchIframeUrl">
      <hi-taskcenter
        ref="hiTask"
        @openfunc="openfunc"
        @openmonitor="openmonitorFn"
        :batchProcessFn="batchProcessFn"
        :batchProcessFunc="batchProcessFuncFn"
        :syncBatch="syncBatch"
        :pageSize.sync="pageSize"
        style="
          background-color: #fff;
          padding-top: 10px;
          box-sizing: border-box;
        "
      >
      </hi-taskcenter>
    </template>
    <template v-else>
      <hi-taskcenter
        ref="hiTask"
        @openfunc="openfunc"
        @openmonitor="openmonitorFn"
        :batchProcessFunc="batchProcessFuncFn"
        :syncBatch="syncBatch"
        :pageSize.sync="pageSize"
        style="
          background-color: #fff;
          padding-top: 10px;
          box-sizing: border-box;
        "
      >
      </hi-taskcenter>
    </template>
  </div>
</template>
<script>
import "hi-taskcenter/hi-taskcenter.css";
import HiTaskcenter from "hi-taskcenter";
import { getToken } from "@main/utils/auth";
export default {
  components: {
    HiTaskcenter,
  },
  data() {
    return {
      // 同步请求批处理
      syncBatch: HIVUI_SETTING.syncBatch,
      // 保存打开的批处理窗口对象
      batchWindow: null,
      batchTitle: "",
      batchIframeUrl: "",
      // 缓存批处理参数
      batchParams: {
        method: "",
        batchData: [],
      },

      pageSize: 10,
    };
  },
  computed: {
    batchTitleDialog() {
      return "任务批处理" + this.batchTitle;
    },
  },

  watch: {
    pageSize(newVal) {
      localStorage.setItem("key-pageSize", newVal);
    },
  },
  mounted() {
    let me = this;
    let electron = top.window.electron;
    if (top.window.customSysCofig.batchIframeUrl && electron) {
      me.batchIframeUrl = top.window.customSysCofig.batchIframeUrl;
    }
    document.addEventListener("iframeActiving", function (e) {
      //console.log(e.detail);
      if (top.window.__refreshTaskcenter) {
        top.window.__refreshTaskcenter = false;
        me.refreshTasks();
      }
    });

    window.addEventListener("message", me.handleIframeMsg);

    if (electron) {
      electron.ipcRenderer.on("main-to-taskcenter-refresh", (e, data) => {
        console.log("刷新任务列表", e, data);
        // 刷新任务列表
        me.refreshTasks();
        me.clearAllSelect();
      });
    }

    const pageSize = localStorage.getItem("key-pageSize") || 10;
    me.pageSize = parseInt(pageSize);
    me.$refs.hiTask.handleSizeChange(me.pageSize);
    me.refreshTasks();
  },
  beforeDestroy() {
    window.removeEventListener("message", this.handleIframeMsg);
    // 销毁时关闭批处理窗口
    if (this.batchWindow && !this.batchWindow.closed) {
      this.batchWindow.close();
    }
  },
  methods: {
    openfunc(__data1, __data2, __data3) {
      if (__data2.furl) {
        if (
          top.window.customSysCofig.taskcenterCustomeropen &&
          typeof top.window.customSysCofig.taskcenterCustomeropen == "function"
        ) {
          top.window.customSysCofig.taskcenterCustomeropen.call(this, __data2);
        } else {
          top.window.open(__data2.furl);
        }
      } else {
        top.window.SysPage.newPage(
          __data2.fpdisplayname,
          location.origin +
            (window._global ? "" : window.HIVUI_SETTING.serverName) +
            "/" +
            __data2.fmodelpath,
          {
            fmodelpath: __data2.fmodelpath,
            version: __data2.fversion,
            ftaskguid: __data2.ftaskguid,
            flowstate: __data2.flowstate,
            forderguid: __data2.forderguid,
            fnumber: __data2.fordernumber,
            fbzid: __data2.fbzid,
            pn: window.HIVUI_SETTING.projectName,
          }
        );
      }
    },
    openmonitorFn(_this, __data2) {
      //流程监控
      if (__data2 && __data2.furl) {
        if (
          top.window.customSysCofig.taskcenterCustomeropenmonitor &&
          typeof top.window.customSysCofig.taskcenterCustomeropenmonitor ==
            "function"
        ) {
          top.window.customSysCofig.taskcenterCustomeropenmonitor.call(
            this,
            _this,
            __data2
          );
        }
      }
    },
    // 批处理
    batchProcessFuncFn(config) {
      if (config.row && config.row.furl) {
        if (
          top.window.customSysCofig.taskcenterbatchProcess &&
          typeof top.window.customSysCofig.taskcenterbatchProcess == "function"
        ) {
          return top.window.customSysCofig.taskcenterbatchProcess.call(
            this,
            this.$refs.hiTask,
            config
          );
        }
      } else {
        return this.$refs.hiTask.processFlow(config);
      }
    },

    // 刷新任务
    refreshTasks() {
      this.$refs.hiTask.taskschemeItemClick({
        value: "untreated",
        label: this.$t("hivuiMain_taskcenter_untreated"),
        sel: true,
      });
    },
    // 接收子页面回调
    handleIframeMsg(e) {
      console.log("接收子页面回调", e);
      const data = e.data;
      // 子页面加载完成就绪，发送批量数据
      if (data.type === "childReady") {
        this.sendDataToBatchWindow();
        return;
      }
      const hiTask = this.$refs.hiTask;
      if (data.type === "batchFinish" && hiTask) {
        // 刷新任务列表
        this.refreshTasks();
        this.clearAllSelect();
      }
    },

    clearAllSelect() {
      const hiTask = this.$refs.hiTask;
      if (hiTask) {
        hiTask.setCheckAll(false);
      }
    },
    // 批处理入口：window.open打开新窗口
    batchProcessFn(method, batchData) {
      console.log("打开新窗口", method, batchData);
      // this.clearAllSelect();
      this.batchTitle = method == "flowOut" ? "（流转）" : "（回退）";
      this.batchParams = {
        method,
        batchData,
      };

      // 如果已有窗口且未关闭，聚焦窗口；否则打开新窗口
      if (this.batchWindow && !this.batchWindow.closed) {
        this.batchWindow.focus();
      } else {
        // 打开批处理页面
        this.batchWindow = window.open(
          this.batchIframeUrl,
          "taskBatchProcess",
          "width=900,height=600,scrollbars=yes"
        );
      }
    },
    // 向批处理窗口发送数据
    sendDataToBatchWindow() {
      console.log("向批处理窗口发送数据");
      if (!this.batchWindow || this.batchWindow.closed) return;
      this.batchWindow.postMessage(
        {
          type: "refreshTable",
          serverUrl: window.HIVUI_SETTING.serverUrl,
          pn: window.HIVUI_SETTING.projectName,
          method: this.batchParams.method,
          list: this.batchParams.batchData,
          token: getToken(),
        },
        "*"
      );
    },
  },
};
</script>

<style scoped>
.taskCenter {
  height: 100%;
}
</style>