<template>
    <div style="margin:20px;">
        当前流程审批人：{{currentTaskAssigneeNames.replaceAll(",","、")}}
    </div>
    <div style="margin:20px;">
        节点审批人：
        <div v-for="(assignees,taskKey) in currentTaskAssignees" :key="taskKey">
            <span>{{assignees[0].taskName}}{{currentTaskInfo?.taskDefinitionKey==taskKey?'（当前审批节点）':''}}：</span>
            <span v-for="(assignee,index) in assignees" :key="index">
                {{assignee.assigneeNameStr?(assignee.assigneeNameStr?.replaceAll(",","/") + (index<assignees.length-1?'、':'')):('')}}
            </span>
        </div>
    </div>
  <!--  流程信息 -->
  <div class="flow-record-box">
    <div id="bpmnCanvas" class="canvas" ref="bpmnCanvas"></div>
  </div>

  <div class="fixed-bottom">
    <ZoomInOrOut @in="zoomViewport(false)" @out="zoomViewport(true)" />
  </div>
</template>
<script lang="ts" setup>
  import CustomModeler from '/@bpmn/modeler';
  import { ZoomInOrOut } from '/@/components/ModalPanel';
  import { getFinishedTask } from '/@/api/workflow/task';
  import { ref, reactive, onMounted } from 'vue';

  const props = withDefaults(
    defineProps<{
      xml: string;
      processId: string;
      currentTaskAssignees: any;
        currentTaskAssigneeNames: string;
        currentTaskInfo:any;
    }>(),
    {
      xml: '',
      processId: '',
        currentTaskAssigneeNames:'无',
        currentTaskAssignees: {},
        currentTaskInfo:{}
    },
  );
  const bpmnCanvas = ref();
  let data: {
    bpmnViewer: any;
    zoom: number;
    xmlString: string;
  } = reactive({
    bpmnViewer: null,
    zoom: 1,
    xmlString: '',
  });
  onMounted(() => {
    data.xmlString = props.xml;
    if (data.xmlString) initBpmnModeler();
  });

  async function initBpmnModeler() {
    data.bpmnViewer = await new CustomModeler({
      container: bpmnCanvas.value,
      additionalModules: [
        {
          labelEditingProvider: ['value', ''], //禁用节点编辑
          paletteProvider: ['value', ''], //禁用/清空左侧工具栏
          contextPadProvider: ['value', ''], //禁用图形菜单
          bendpoints: ['value', {}], //禁用连线拖动
          move: ['value', ''], //禁用单个图形拖动
        },
      ],
    });
    await redrawing();
    if (props.processId) {
      let res = await getFinishedTask(props.processId);
      setColors(
        res.finishedNodes ? res.finishedNodes : [],
        res.currentNodes ? res.currentNodes : [],
      );
    }
  }
  async function redrawing() {
    try {
      await data.bpmnViewer.importXML(data.xmlString);
      let canvas = data.bpmnViewer.get('canvas');
      canvas.zoom('fit-viewport', 'auto');
    } catch (err) {
      console.log('err: ', err);
    }
  }
  function setColors(finishedIds: Array<string>, currentIds: Array<string>) {
    // finishedIds 完成的节点id
    // currentIds 进行中节点id
    let modeling = data.bpmnViewer.get('modeling');
    const elementRegistry = data.bpmnViewer.get('elementRegistry');
    if (finishedIds.length > 0) {
      finishedIds.forEach((it) => {
        let Event = elementRegistry.get(it);

        modeling.setColor(Event, {
          stroke: 'green',
          fill: 'white',
        });
      });
    }
    if (currentIds.length > 0) {
      currentIds.forEach((it) => {
        let Event = elementRegistry.get(it);
        modeling.setColor(Event, {
          stroke: '#409eff',
          fill: 'white',
        });
      });
    }
  }
  function zoomViewport(zoomIn = true) {
    data.zoom = data.bpmnViewer.get('canvas').zoom();
    data.zoom += zoomIn ? 0.1 : -0.1;
    data.bpmnViewer.get('canvas').zoom(data.zoom);
  }
</script>
<style lang="less">
  @import '/@/assets/style/bpmn-js/diagram-js.css';
  @import '/@/assets/style/bpmn-js/bpmn-font/css/bpmn.css';
  @import '/@/assets/style/bpmn-js/bpmn-font/css/bpmn-codes.css';
  @import '/@/assets/style/bpmn-js/bpmn-font/css/bpmn-embedded.css';

  .bjs-powered-by {
    display: none !important;
  }

  .flow-record-box {
    width: 100%;
    height: 80vh;
    position: relative;
    margin-top: 50px;
  }

  /* 画布 */
  .canvas {
    width: 100%;
    height: 100%;
  }

  /* 按钮（放大 缩小 清除） */
  .fixed-bottom {
    position: absolute;
    top: 110px;
    font-size: 30px;
    left: 40%;
    display: flex;
  }

  .flow-record-box{
    // 选择节点的蓝框
    .djs-outline{
       display: none;
    }
    // 编辑节点的按钮
    .djs-overlay-container{
       display: none;
    }
  }
</style>
