<template>
  <a-modal
    v-if="visible"
    :visible="visible"
    class="echart-menu-modal"
    :footer="null"
    :mask="false"
    :mask-closable="true"
    :closable="false"
    :dialog-style="position"
    :get-container="container"
  >
    <a-menu @click="handleMenu">
      <a-menu-item v-for="item in MENUS" :key="item.key">{{
        item.text
      }}</a-menu-item>
    </a-menu>
  </a-modal>
</template>
<script>
import { defineComponent, computed } from "@vue/composition-api";
import { Modal, Menu } from "ant-design-vue";
import { isEmpty, throttle } from "lodash-es";

const MENUS = [
  { key: "none", text: "不排序" },
  { key: "ascending", text: "升序", icon: "GroupAsc" },
  { key: "descending", text: "降序", icon: "GroupDesc" },
  // { key: SortMethodType.custom, text: '自定义排序', icon: 'Trend' },
];

const ModalNode = defineComponent({
  components: {
    [Modal.name]: Modal,
    [Menu.name]: Menu,
    [Menu.Item.name]: Menu.Item,
  },
  props: ["optData"],
  setup(props, vm) {
    const pageRoot = document.querySelector("#page-scene-root");
    const container = () => pageRoot;

    const visible = computed(() => {
      const newVisible = !isEmpty(props.optData);
      if (newVisible) {
        document.addEventListener("mousemove", mouseFunc, false);
      } else {
        document.removeEventListener("mousemove", mouseFunc, false);
      }

      return newVisible;
    });

    const mouseFunc = throttle((e) => {
      // 150x120弹窗, 20px偏差值
      const { clientX, clientY } = e;
      const { x, y } = props.optData;
      // 内容+偏差值
      if (
        clientX < x - 20 ||
        clientX > x + 170 ||
        clientY < y - 20 ||
        clientY > y + 140
      ) {
        vm.emit("menuclick", "close");
      }
    }, 500);

    const position = computed(() => {
      return {
        top: `${props.optData.y || 0}px`,
        left: `${props.optData.x || 0}px`,
      };
    });

    const handleMenu = ({ key }) => {
      vm.emit("menuclick", props.optData?.meta?.key, key, props.optData?.meta);
    };

    return {
      visible,
      position,
      handleMenu,
      MENUS,
      container,
    };
  },
});

export default ModalNode;
</script>
<style lang="less">
.echart-menu-modal {
  @keyframes identifier {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  animation: none !important;
  & * {
    animation: 0.1s identifier !important;
  }

  & .biAnt-modal {
    margin: 0;
  }
  & .biAnt-modal-content {
    width: 150px;
  }
  & .biAnt-modal-body {
    padding: 0 !important;
  }
  & .biAnt-modal-confirm-content {
    margin: 0;
  }

  & .biAnticon-question-circle,
  & .biAnt-modal-confirm-btns {
    display: none;
  }
  .biAnt-menu {
    background: #f8f9fa;
    color: #202020;
    border-right: none;
    height: calc(100% - 44px);
    overflow-y: auto;
    overflow-x: hidden;

    & * {
      border: none;
    }

    .biAnt-menu-item {
      margin: 0;
      font-size: 12px;
      position: relative;
      padding-left: 39px !important;
      .board-submenu-title-wrapper {
        .board-dele-icon {
          position: absolute;
          right: 0;
          top: 14px;
          display: none;
        }
      }
      .biAnticon {
        font-size: 12px !important;
      }
    }
    .biAnt-menu-item:hover {
      .board-submenu-title-wrapper .board-dele-icon {
        display: block;
        color: #202020;
      }
    }
  }
}
</style>
