<template>
  <transition name="fade">
    <div
      class="task-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: 'TaskActionMenu',
  props: {
    position: {
      type: Object,
      required: true
    },
    task: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      adjustedPosition: {
        left: 0,
        top: 0
      }
    }
  },
  computed: {
    menuItems() {
      const items = []
      const status = this.task.status

      if (status === 'draft') {
        items.push(
          { label: '发布', command: 'publish', icon: 'el-icon-upload2' },
          { label: '删除', command: 'delete', icon: 'el-icon-delete', divided: true }
        )
      }

      if (status === 'published' || status === 'in_progress') {
        items.push({ label: '取消任务', command: 'abort', icon: 'el-icon-remove-outline' })
      }

      if (status === 'in_progress') {
        items.push({ label: '关闭', command: 'close', icon: 'el-icon-circle-close' })
      }

      items.push({ label: '详情', command: 'detail', icon: 'el-icon-view', divided: true })

      return items
    },
    flatMenuItems() {
      const flat = []
      this.menuItems.forEach((item, index) => {
        if (item.divided && index > 0) {
          flat.push({ type: 'divider', key: `divider-${item.command}` })
        }
        flat.push({ type: 'item', item, key: item.command, 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('.task-action-menu')
      const menuWidth = menuEl?.offsetWidth || 200
      const menuHeight = menuEl?.offsetHeight || 240

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

      let left = this.position.left
      let top = this.position.top

      if (left + menuWidth > viewportWidth) {
        left = viewportWidth - menuWidth - 16
      }
      if (left < 8) {
        left = 8
      }

      if (top + menuHeight > viewportHeight) {
        top = viewportHeight - menuHeight - 16
      }
      if (top < 8) {
        top = 8
      }

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

<style lang="scss" scoped>
.task-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>