<template>
  <div class="x6-vue-shape">
    <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="nodeData?.id && currentNodeData?.nodeId && currentNodeData.nodeId == nodeData.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="nodeData" @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 { getFlowBaseIcon } from '@/utils/common/util';
import { Tooltip as ATooltip, Popover as APopover } from 'ant-design-vue';

export default defineComponent({
  name: 'X6DiamondShape',
  components: { X6AddNode, ATooltip, APopover },
  setup(props) {
    // store
    const workflowStore = useWorkflowStore();

    const { canAdd, addNodeItem } = X6ShapeUtil();

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

    // dom
    const x6VueShapeRef = ref(null);

    const nodeData = ref({});

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

    //
    const graph = ref(null);

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

    // 图片列表
    const iconList = ref(getFlowBaseIcon());

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

    onMounted(() => {
      graph.value = workflowStore.graph;

      // 节点
      const node = inject('getNode');
      nodeData.value = node();
      if (node()?.data) {
        let nodeData = JSON.parse(node().data);

        iconClass.value = nodeData.icon;
        nameVal.value = nodeData.data.nodeName;
      }

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

    // 添加节点
    const addNode = (type, nodeItem) => {
      if (canAdd(nodeItem)) {
        const position = nodeData.value.position();
        let x = position.x;
        let y = position.y;

        if (type == 'top') {
          if (nodeItem.type == 60) {
            x = x - 44;
          } else {
            x = x - 86;
          }
          y = y - 200;
        } else if (type == 'right') {
          if (nodeItem.type == 60) {
            y = y + 30;
          } else {
            y = y + 10;
          }

          x = x + 200;
        } else if (type == 'bottom') {
          if (nodeItem.type == 60) {
            x = x - 46;
          } else {
            x = x - 86;
          }
          y = y + 200;
        } else if (type == 'left') {
          if (nodeItem.type == 60) {
            y = y + 30;
            x = x - 200;
          } else {
            y = y + 10;
            x = x - 400;
          }
        }
        addNodeItem(nodeItem, x, y, type, currentNodeData.value, nodeData.value);
      }
    };

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

    return {
      nodeData,
      nameVal,
      addNode,
      graph,
      iconList,
      iconClass,
      activeIconSrc,
      x6VueShapeRef,
      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: 64px;
  }
}

.x6-shape-add {
  position: absolute;
  width: 100%;
  height: 100%;
  .shape-top,
  .shape-right,
  .shape-bottom,
  .shape-left {
    position: absolute;
    &::before {
      position: absolute;
      left: 19px;
      top: 31px;
      content: '';
      width: 1px;
      height: 45px;
      background: #ccc;
    }
  }
  .shape-top {
    left: 50%;
    margin-left: -20px;
    top: -78px;
  }
  .shape-right {
    right: -80px;
    top: 21px;
    &::before {
      width: 45px;
      height: 1px;
      top: 18px;
      left: -40px;
    }
  }
  .shape-bottom {
    left: 50%;
    bottom: -78px;
    margin-left: -19px;
    &::before {
      left: 19px;
      top: -40px;
    }
  }
  .shape-left {
    left: -80px;
    top: 21px;
    &::before {
      width: 45px;
      height: 1px;
      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>
