<template>
  <div class="process-design" :style="'display: flex; height:' + height">
    <bpmn-process-designer
      v-model="xmlString"
      v-bind="controlForm"
      keyboard
      ref="processDesigner"
      :eventlist="[
        'element.click',
        'connection.added',
        'connection.removed',
        'connection.changed',
      ]"
      @element-click="elementClick"
      @init-finished="initModeler"
      @event="handlerEvent"
      @save="onSaveProcess"
    />

    <bmpn-process-penal
      :bpmn-modeler="modeler"
      :prefix="controlForm.prefix"
      class="process-panel"
      ref="bmpnProcessPenalRef"
    />
  </div>
</template>
<script setup>
import BmpnProcessPenal from "@/plugins/package/penal";
import BpmnProcessDesigner from "@/plugins/package/designer";
import "@/plugins/package/theme/index.scss";
// import vuePlugin from "@/plugins/package/highlight";
// import "highlight.js/styles/atom-one-dark-reasonable.css";
// import "highlight.js/styles/atom-one-dark-reasonable.css";
import CustomContentPadProvider from "@/plugins/package/designer/plugins/content-pad";
import CustomPaletteProvider from "@/plugins/package/designer/plugins/palette";
import { ref, nextTick, reactive, toRefs } from "vue";
import { ElMessageBox } from "element-plus";

const props = defineProps({
  bpmnXml: {
    type: String,
    required: true,
  },
  designerForm: {
    type: Object,
    required: true,
  },
});
const { bpmnXml, designerForm } = toRefs(props);
let element;
let msgInstance;
const modeler = ref(null);
const height = ref(document.documentElement.clientHeight - 94.5 + "px;");
const xmlString = ref(bpmnXml.value);
const emit = defineEmits(["save"]);
const data = reactive({
  controlForm: {
    processId: designerForm.value.processKey || "",
    processName: designerForm.value.processName || "",
    simulation: false,
    labelEditing: false,
    labelVisible: false,
    prefix: "flowable",
    headerButtonSize: "small",
    additionalModel: [CustomContentPadProvider, CustomPaletteProvider],
  },
});
const { controlForm } = toRefs(data);
const bmpnProcessPenalRef = ref();
function elementClick(elementArgv) {
  element = elementArgv;

  if (elementArgv.type != "bpmn:StartEvent") {
    goToFormSelect();
  }
}
function initModeler(modelerArgv) {
  setTimeout(() => {
    modeler.value = modelerArgv;
  }, 10);
}
function handlerEvent(eventName) {
  if (eventName === "connection-added") {
    goToFormSelect();
  }
}

function goToFormSelect() {
  if (!window.bpmnInstances) return;
  const formKeyExist =
    !!window.bpmnInstances.elementRegistry.find(
      (el) => el.type == "bpmn:StartEvent"
    )?.businessObject?.formKey ?? false;

  if (!formKeyExist) {
    ElMessageBox.alert("请为开始节点选择表单", "未选择表单", {
      confirmButtonText: "去选择",
      icon: "warning",
    })
      .then(() => {
        const startEle = window.bpmnInstances.elementRegistry.find(
          (el) => el.type == "bpmn:StartEvent"
        );
        const EventBus = window.bpmnInstances.eventBus;
        startEle && EventBus.fire("element.click", { element: startEle });
        nextTick(() => {
          bmpnProcessPenalRef.value.openFormColla();
        });
      })
      .catch(() => {});
  }
}
function onSaveProcess(saveData) {
  emit("save", saveData);
}
</script>

<style lang="scss">
body {
  overflow: hidden;
  margin: 0;
  box-sizing: border-box;
}

body,
body * {
  /* 滚动条 */
  &::-webkit-scrollbar-track-piece {
    background-color: #fff;
    /*滚动条的背景颜色*/
    -webkit-border-radius: 0;
    /*滚动条的圆角宽度*/
  }

  &::-webkit-scrollbar {
    width: 10px;
    /*滚动条的宽度*/
    height: 8px;
    /*滚动条的高度*/
  }

  &::-webkit-scrollbar-thumb:vertical {
    /*垂直滚动条的样式*/
    height: 50px;
    background-color: rgba(153, 153, 153, 0.5);
    -webkit-border-radius: 4px;
    outline: 2px solid #fff;
    outline-offset: -2px;
    border: 2px solid #fff;
  }

  &::-webkit-scrollbar-thumb {
    /*滚动条的hover样式*/
    background-color: rgba(159, 159, 159, 0.3);
    -webkit-border-radius: 4px;
  }

  &::-webkit-scrollbar-thumb:hover {
    /*滚动条的hover样式*/
    background-color: rgba(159, 159, 159, 0.5);
    -webkit-border-radius: 4px;
  }
}
</style>
