<template>
  <div class="x6-vue-shape" ref="x6VueShapeRef">
    <div class="node-name" ref="nodeNameRef">
      <span class="node-name-title">
        <a-tooltip placement="topLeft" :title="nameVal">
          <div v-html="nameVal"></div>
        </a-tooltip>
      </span>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted, inject } from 'vue';
import { useWorkflowStore } from '@/store/modules/workflow';
import { Tooltip as ATooltip } from 'ant-design-vue';

export default defineComponent({
  name: 'BpmnNotesShape',
  props: {
    // 节点信息
    nodeData: {
      type: Object
    },
    isShowAdd: {
      type: Boolean,
      default: true
    }
  },
  components: { ATooltip },
  setup(props) {
    // store
    const workflowStore = useWorkflowStore();

    // 名称
    const nameVal = ref('');

    // dom
    const nodeNameRef = ref(null);

    //
    const graph = ref(null);

    onMounted(() => {
      let data = props.nodeData;

      nameVal.value = data?.name;
      graph.value = workflowStore.graph;
      if (!data) {
        // 节点
        const node = inject('getNode');
        if (node()?.data) {
          let nodeData = JSON.parse(node().data);
          if (nodeData.data.nodeData?.bpmnConfig?.remark) {
            nameVal.value = nodeData.data.nodeData?.bpmnConfig?.remark;
          } else {
            nameVal.value = nodeData.data.nodeName;
          }
          nameVal.value = nameVal.value.replace(/\n/g, "</br>")
        }

        if (node()) {
          node().on('change:data', ({ current }) => {
            let nodeData = JSON.parse(current);
            if (nodeData.data.nodeData?.bpmnConfig?.remark) {
              nameVal.value = nodeData.data.nodeData?.bpmnConfig?.remark;
            } else {
              nameVal.value = nodeData.data.nodeName;
            }
            nameVal.value = nameVal.value.replace(/\n/g, "</br>")
          });
        }
      }
    });

    return {
      nameVal,
      graph,
      nodeNameRef
    };
  }
});
</script>

<style scoped lang="less">
.x6-vue-shape {
  cursor: move;
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  padding: 10px;
  box-sizing: border-box;
  align-items: center;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-left: 2px solid #43505e;
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 10%;
    height: 2px;
    background-color: #43505e;
  }
  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 10%;
    height: 2px;
    background-color: #43505e;
  }

  .node-name {
    height: 100%;
    width: 100%;
    font-size: 14px;
    margin-top: -3px;
    color: #43505e;

    .node-name-title {
      width: 100%;
    }
  }
}
</style>
