<template>
  <transition name="fade">
    <div
      class="marker-context-menu"
      :style="{ left: `${position.left}px`, top: `${position.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="item in menuItems"
          :key="item.label"
          class="menu-item"
          @click.stop="handleSelect(item)"
        >
          <i :class="item.icon" />
          <span>{{ item.label }}</span>
        </li>
        <li v-if="!menuItems.length" class="menu-item disabled">
          暂无可用操作
        </li>
      </ul>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'MarkerContextMenu',
  props: {
    position: {
      type: Object,
      required: true
    },
    type: {
      type: String,
      default: null
    },
    payload: {
      type: Object,
      default: null
    }
  },
  computed: {
    menuItems() {
      const isImportant = this.payload?.data?.importantFlag === 1
      const menus = {
        events: [
          { label: '查看事件详情', action: 'event:view', icon: 'el-icon-view' },
          { label: '定位到此事件', action: 'event:locate', icon: 'el-icon-location-outline' },
          { label: '创建调度任务', action: 'event:createTask', icon: 'el-icon-document-add' },
          {
            label: isImportant ? '取消重点' : '标记为重点',
            action: 'event:pin',
            icon: isImportant ? 'el-icon-star-on' : 'el-icon-star-off'
          },
          { label: '复制事件关键信息', action: 'event:copyInfo', icon: 'el-icon-document-copy' }
        ],
        teams: [
          { label: '查看队伍详情', action: 'team:view', icon: 'el-icon-view' }
        ],
        warehouses: [
          { label: '查看仓库详情', action: 'warehouse:view', icon: 'el-icon-view' }
        ],
        experts: [
          { label: '查看专家详情', action: 'expert:view', icon: 'el-icon-view' }
        ],
        vehicles: [
          { label: '查看车辆详情', action: 'vehicle:view', icon: 'el-icon-view' }
        ],
        cameras: [
          { label: '预览摄像头', action: 'camera:preview', icon: 'el-icon-video-camera' },
          { label: '定位到此摄像头', action: 'camera:locate', icon: 'el-icon-location-outline' }
        ]
      }
      return menus[this.type] || []
    }
  },
  methods: {
    handleSelect(item) {
      this.$emit('action', {
        action: item.action,
        payload: this.payload
      })
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.marker-context-menu {
  position: absolute;
  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;

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

  &:hover {
    background: #f5f7fa;
  }

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

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