<template>
  <div class="node-palette">
    <el-input v-model="search" size="small" clearable placeholder="搜索控件" class="palette-search">
      <template #prefix><el-icon><Search /></el-icon></template>
    </el-input>

    <div v-for="group in grouped" :key="group.category" class="palette-group">
      <div class="palette-group-title">
        <span class="palette-group-dot" :style="{ background: group.color }" />
        {{ group.category }}
      </div>
      <div
        v-for="item in group.items"
        :key="item.type"
        class="palette-item"
        draggable="true"
        @dragstart="onDragStart($event, item.type)"
      >
        <span class="palette-dot" :style="{ background: item.color }" />
        <div class="palette-item-text">
          <div class="palette-item-label">{{ item.label }}</div>
          <div v-if="item.description" class="palette-item-desc">{{ item.description }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { CATEGORY_ORDER, NODE_CATALOG } from '@/workflow/nodeCatalog'

const search = ref('')

const CATEGORY_COLORS: Record<string, string> = {
  流程入口: '#67c23a',
  调度动作: '#e6a23c',
  流程逻辑: '#9b59b6',
  事件与信号: '#409eff',
  协议控制: '#13c2c2'
}

const grouped = computed(() => {
  const q = search.value.trim().toLowerCase()
  const filtered = NODE_CATALOG.filter((n) => {
    if (!q) return true
    return (
      n.label.toLowerCase().includes(q) ||
      n.type.toLowerCase().includes(q) ||
      n.category.toLowerCase().includes(q)
    )
  })

  const byCategory = new Map<string, typeof NODE_CATALOG>()
  for (const item of filtered) {
    const list = byCategory.get(item.category) ?? []
    list.push(item)
    byCategory.set(item.category, list)
  }

  return CATEGORY_ORDER.filter((cat) => byCategory.has(cat)).map((category) => ({
    category,
    color: CATEGORY_COLORS[category] ?? '#909399',
    items: byCategory.get(category) ?? []
  }))
})

function onDragStart(e: DragEvent, type: string) {
  if (!e.dataTransfer) return
  e.dataTransfer.setData('application/workflow-node', type)
  e.dataTransfer.effectAllowed = 'move'
}
</script>

<style scoped>
.node-palette {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  box-sizing: border-box;
}
.palette-search {
  margin-bottom: 10px;
}
.palette-group {
  margin-bottom: 14px;
}
.palette-group-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: #606266;
  margin-bottom: 8px;
}
.palette-group-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.palette-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  background: #fafafa;
  cursor: grab;
  transition: border-color 0.15s, background 0.15s;
}
.palette-item:hover {
  border-color: #c6e2ff;
  background: #ecf5ff;
}
.palette-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-top: 3px;
  flex-shrink: 0;
}
.palette-item-label {
  font-size: 13px;
  font-weight: 500;
  color: #303133;
}
.palette-item-desc {
  font-size: 11px;
  color: #909399;
  margin-top: 2px;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
