<template>
  <transition name="fade">
    <div
      class="event-action-menu"
      :style="{ left: `${adjustedPosition.left}px`, top: `${adjustedPosition.top}px` }"
      @click.stop
      @contextmenu.prevent
    >
      <div class="menu-header">
        <span>事件操作</span>
        <i class="el-icon-close" @click.stop="$emit('close')"></i>
      </div>
      <ul class="menu-list">
        <li
          v-for="element in flatMenuItems"
          :key="element.key"
          :class="[
            element.type === 'divider' ? 'menu-divider' : 'menu-item',
            { disabled: element.disabled }
          ]"
          @click.stop="element.type === 'item' && handleSelect(element.item)"
        >
          <template v-if="element.type === 'item'">
            <i :class="element.item.icon" />
            <span>{{ element.item.label }}</span>
          </template>
        </li>
        <li v-if="!menuItems.length" class="menu-item disabled">
          暂无可用操作
        </li>
      </ul>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'EventActionMenu',
  props: {
    position: {
      type: Object,
      required: true
    },
    event: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      adjustedPosition: {
        left: 0,
        top: 0
      }
    }
  },
  computed: {
    menuItems() {
      const isImportant = this.event.importantFlag === 1
      return [
        { label: '查看事件详情', action: 'view', icon: 'el-icon-view' },
        { label: '定位到此事件', action: 'locate', icon: 'el-icon-location-outline' },
        { label: '创建调度任务', action: 'createTask', icon: 'el-icon-document-add' },
        {
          label: isImportant ? '取消重点' : '标记为重点',
          action: 'pin',
          icon: isImportant ? 'el-icon-star-on' : 'el-icon-star-off',
          divided: true
        },
        { label: '复制事件关键信息', action: 'copyInfo', icon: 'el-icon-document-copy' }
      ]
    },
    flatMenuItems() {
      const flat = []
      this.menuItems.forEach((item, index) => {
        if (item.divided && index > 0) {
          flat.push({ type: 'divider', key: `divider-${item.action}` })
        }
        flat.push({ type: 'item', key: item.action, item: item, disabled: item.disabled })
      })
      return flat
    }
  },
  watch: {
    position: {
      immediate: true,
      deep: true,
      handler() {
        this.$nextTick(() => {
          this.updatePosition()
        })
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.updatePosition()
    })
    window.addEventListener('resize', this.updatePosition, { passive: true })
    window.addEventListener('scroll', this.updatePosition, true)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.updatePosition)
    window.removeEventListener('scroll', this.updatePosition, true)
  },
  methods: {
    updatePosition() {
      const menuEl = this.$el?.querySelector('.event-action-menu')
      const menuWidth = menuEl?.offsetWidth || 200
      const menuHeight = menuEl?.offsetHeight || 240

      // 将菜单右下角对齐到点击位置
      // position 是按钮的右下角坐标
      let left = this.position.left - menuWidth
      let top = this.position.top - menuHeight

      const viewportWidth = window.innerWidth
      const viewportHeight = window.innerHeight

      // 如果左侧空间不够，则右对齐
      if (left < 8) {
        left = this.position.left
      }
      // 如果右侧超出，则调整到视口内
      if (left + menuWidth > viewportWidth) {
        left = viewportWidth - menuWidth - 8
      }

      // 如果上方空间不够，则下对齐
      if (top < 8) {
        top = this.position.top
      }
      // 如果下方超出，则调整到视口内
      if (top + menuHeight > viewportHeight) {
        top = viewportHeight - menuHeight - 8
      }

      this.adjustedPosition = { left, top }
    },
    handleSelect(item) {
      if (item.disabled) return
      this.$emit('action', item.action)
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.event-action-menu {
  position: fixed;
  min-width: 180px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  padding: 8px 0;
  z-index: 1000;
  border: 1px solid #ebeef5;
  pointer-events: auto;
}

.menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 12px 6px;
  font-size: 13px;
  color: #606266;
  border-bottom: 1px solid #f2f6fc;

  .el-icon-close {
    cursor: pointer;
    font-size: 14px;
    color: #909399;

    &:hover {
      color: #606266;
    }
  }
}

.menu-list {
  list-style: none;
  margin: 0;
  padding: 6px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  font-size: 13px;
  color: #303133;
  cursor: pointer;
  border-top: 1px solid transparent;

  i {
    font-size: 14px;
    color: #909399;
  }

  &:hover {
    background: #f5f7fa;
  }

  &.disabled {
    color: #c0c4cc;
    cursor: not-allowed;
    i {
      color: #c0c4cc;
    }
  }
}

.menu-divider {
  height: 1px;
  background: #f2f6fc;
  margin: 4px 0;
  list-style: none;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.15s ease;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>