<template>
  <div class="x6-vue-shape" ref="x6VueShapeRef">
    <div class="node-name" ref="nodeNameRef">
      <img :src="activeIconSrc" alt="" class="svg-img" />
      <span class="node-name-title">
        <a-tooltip placement="topLeft" :title="nameVal">
          <span class="x6-vue-shape-name-class">{{ nameVal }}</span>
        </a-tooltip>
      </span>
    </div>
    <!-- 加号 -->
    <div
      class="x6-shape-add"
      v-if="false && isShowAdd && nodeData2?.id && currentNodeData?.nodeId && currentNodeData.nodeId == nodeData2.id"
    >
      <template v-for="item in addButtonList" :key="item.type">
        <div :class="item.class" v-if="currentNodeData.groupList.find(gItem => gItem.name == item.type)">
          <a-popover placement="right" style="border-radius: 5px">
            <template #content>
              <X6AddNode :type="item.type" :node="nodeData2" @addNode="addNode" />
            </template>
            <img src="@/assets/images/add.svg" alt="" class="shape-img" />
          </a-popover>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted, inject, watch, computed } from 'vue';
import { useWorkflowStore } from '@/store/modules/workflow';
import X6ShapeUtil from '../utils/x6-shape-util';
import X6AddNode from './components/x6-add-node.vue';
import { Tooltip as ATooltip } from 'ant-design-vue';
import { getBpmnFlowIcon } from '@/utils/common/util';

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

    const { canAdd, addNodeItem } = X6ShapeUtil();

    // 名称
    const nameVal = ref('');
    // 流程图标class
    const iconClass = ref('');

    const iconList = ref(getBpmnFlowIcon());

    // dom
    const nodeNameRef = ref(null);

    const nodeData2 = ref({});

    // 当前node数据
    const currentNodeData = ref({
      nodeId: '',
      groupList: []
    });

    // 边框颜色
    const borderColor = ref('');

    //
    const graph = ref(null);

    // 当前节点类型
    const currentType = ref();
    // 添加按钮列表
    const addButtonList = ref([
      {
        type: 'top',
        class: 'shape-top'
      },
      {
        type: 'right',
        class: 'shape-right'
      },
      {
        type: 'bottom',
        class: 'shape-bottom'
      },
      {
        type: 'left',
        class: 'shape-left'
      }
    ]);

    /**
     * 图标
     */
    const activeIconSrc = computed(() => {
      const icon = iconList.value.find(t => t.name == iconClass.value);
      return icon ? icon.url : '';
    });

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

      iconClass.value = data?.icon;
      nameVal.value = data?.name;
      graph.value = workflowStore.graph;
      if (!data) {
        // 节点
        const node = inject('getNode');
        nodeData2.value = node();
        if (node()?.data) {
          let nodeData = JSON.parse(node().data);
          iconClass.value = nodeData.icon;
          currentType.value = nodeData.type;
          nameVal.value = nodeData.data.nodeName;

          // 修改节点边框颜色
          if (nodeData.data?.borderColor) {
            borderColor.value = nodeData.data.borderColor;
            nodeNameRef.value.style.backgroundColor = nodeData.data.borderColor;
            // 如果节点状态是 进行中 高亮显示节点
            if (nodeData.data.nodeStatus === 20) {
              nodeNameRef.value.style.animation = 'twinkle 1s infinite alternate';
            }
          }
        }

        if (node()) {
          node().on('change:data', ({ current }) => {
            let nodeData = JSON.parse(current);
            if (nodeData.icon) {
              iconClass.value = nodeData.icon;
            }
            nameVal.value = nodeData.data.nodeName;
            if (nodeData.data?.borderColor) {
              borderColor.value = nodeData.data.borderColor;
              nodeNameRef.value.style.backgroundColor = nodeData.data.borderColor;

              // 如果节点状态是 进行中 高亮显示节点
              if (nodeData.data.nodeStatus === 20) {
                nodeNameRef.value.style.animation = 'twinkle 1s infinite alternate';
              }
            }
          });
        }
      }
    });

    // 添加节点
    const addNode = (type, nodeItem) => {
      if (canAdd(nodeItem)) {
        const position = nodeData2.value.position();
        let x = position.x;
        let y = position.y;
        if (type == 'top') {
          y = y - 150;
        } else if (type == 'right') {
          x = x + 350;
        } else if (type == 'bottom') {
          y = y + 150;
        } else if (type == 'left') {
          x = x - 350;
        }
        addNodeItem(nodeItem, x, y, type, currentNodeData.value, nodeData2.value);
      }
    };

    watch(
      () => workflowStore.nodeData,
      val => {
        currentNodeData.value = val;
      },
      { deep: true, immediate: true }
    );

    return {
      nodeData2,
      nameVal,
      addNode,
      graph,
      iconList,
      iconClass,
      currentType,
      borderColor,
      nodeNameRef,
      activeIconSrc,
      currentNodeData,
      addButtonList
    };
  }
});
</script>

<style scoped lang="less">
.x6-vue-shape {
  cursor: move;
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-radius: 4px;

  .node-name {
    height: 100%;
    width: 100%;
    font-size: 15px;
    color: #43505e;
    display: flex;
    align-items: center;
    flex-direction: column;

    .node-name-title {
      margin: auto;
      text-align: center;
      width: 100%;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      overflow: hidden;
      white-space: normal;
      text-overflow: ellipsis;
      -o-text-overflow: ellipsis;
    }
  }

  .svg-img {
    width: 40px;
  }
}
.x6-shape-add {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  .shape-top,
  .shape-right,
  .shape-bottom,
  .shape-left {
    position: absolute;
    &::before {
      position: absolute;
      left: 19px;
      top: 33px;
      content: '';
      width: 1px;
      height: 45px;
      background: #ccc;
    }
  }
  .shape-top {
    left: 40%;
    margin-left: -24px;
    top: -78px;
  }
  .shape-right {
    right: -55px;
    top: 0px;
    &::before {
      width: 45px;
      height: 1.5px;
      top: 18px;
      left: -40px;
    }
  }
  .shape-bottom {
    left: 40%;
    top: 80px;
    margin-left: -24px;
    &::before {
      left: 18px;
      top: -40px;
      width: 1.5px;
    }
  }
  .shape-left {
    left: -105px;
    top: 0px;
    &::before {
      width: 45px;
      height: 1.5px;
      top: 18px;
      left: 33px;
    }
  }
  .shape-img {
    cursor: pointer;
    width: 38px;
  }
}

.ant-menu-vertical {
  border: 0px !important;
}
:deep(.ant-menu-item) {
  width: 240px !important;
  height: 40px;
  line-height: 40px;
  color: #000;
  font-size: 14px;
  margin-left: -16px !important;
  margin-right: -16px !important;
  margin-bottom: 0px !important;
  &:hover {
    background: #f5f5f5;
    color: #000;
  }
}
:deep(.ant-menu-title-content) {
  display: flex;
  align-items: center;
}
</style>
