<template>
  <div class="wf-list-panel">
    <div class="wf-list-head">
      <span class="wf-list-title">流程库</span>
      <div class="wf-list-actions">
        <el-button size="small" type="primary" link @click="emit('create')">+ 新建</el-button>
        <el-button size="small" type="success" link @click="emit('save')">保存</el-button>
      </div>
    </div>

    <ul class="wf-list">
      <li
        v-for="item in workflows"
        :key="item.id"
        class="wf-list-item"
        :class="{ active: item.id === activeId }"
        @click="emit('select', item.id)"
      >
        <div class="wf-list-item-main">
          <span class="wf-list-item-name">{{ item.name || '未命名流程' }}</span>
          <span class="wf-list-item-meta">{{ item.nodes.length }} 节点</span>
        </div>
        <el-dropdown
          trigger="click"
          @click.stop
          @command="(cmd) => onCommand(cmd, item.id)"
        >
          <el-button size="small" text class="wf-list-more" @click.stop>⋯</el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="duplicate">复制</el-dropdown-item>
              <el-dropdown-item command="delete" :disabled="workflows.length <= 1">删除</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </li>
    </ul>

    <p class="wf-list-hint">共 {{ workflows.length }} 个流程（仅当前会话内存）；持久化请点顶栏「导出」下载 JSON。</p>
  </div>
</template>

<script setup lang="ts">
import type { WorkflowDefinition } from '@/types/workflow'

defineProps<{
  workflows: WorkflowDefinition[]
  activeId: string
}>()

const emit = defineEmits<{
  select: [id: string]
  create: []
  save: []
  duplicate: [id: string]
  delete: [id: string]
}>()

function onCommand(cmd: string | number | object, id: string) {
  if (cmd === 'duplicate') emit('duplicate', id)
  if (cmd === 'delete') emit('delete', id)
}
</script>

<style scoped>
.wf-list-panel {
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid #ebeef5;
  flex-shrink: 0;
  max-height: 220px;
}
.wf-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 4px;
}
.wf-list-title {
  font-size: 12px;
  font-weight: 600;
  color: #606266;
}
.wf-list-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.wf-list {
  list-style: none;
  margin: 0;
  padding: 4px 8px 8px;
  overflow-y: auto;
}
.wf-list-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}
.wf-list-item:hover {
  background: #f5f7fa;
}
.wf-list-item.active {
  background: #ecf5ff;
  border: 1px solid #b3d8ff;
}
.wf-list-item-main {
  flex: 1;
  min-width: 0;
}
.wf-list-item-name {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: #303133;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.wf-list-item-meta {
  font-size: 11px;
  color: #909399;
}
.wf-list-more {
  padding: 0 4px;
  min-height: auto;
}
.wf-list-hint {
  margin: 0;
  padding: 6px 12px 10px;
  font-size: 11px;
  color: #909399;
  line-height: 1.4;
  border-top: 1px solid #f0f2f5;
}
</style>
