<script setup lang="ts">
import { Handle, Position } from '@vue-flow/core'
import { ref } from 'vue'

interface InputOutput {
  id: string
  name: string
  type: string
  value?: any
}

interface Props {
  data: {
    label: string
    description?: string
    inputs?: InputOutput[]
    outputs?: InputOutput[]
    condition?: string
  }
}

const props = withDefaults(defineProps<Props>(), {
  data: () => ({ 
    label: '决策',
    condition: 'value > 0',
    inputs: [
      { id: 'decision_input', name: '判断条件', type: 'any', value: null }
    ],
    outputs: [
      { id: 'decision_true', name: '是 (True)', type: 'boolean', value: true },
      { id: 'decision_false', name: '否 (False)', type: 'boolean', value: false }
    ]
  })
})
</script>

<template>
  <div class="workflow-node decision-node">
    <!-- 节点头部 -->
    <div class="node-header">
      <div class="node-icon">
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none">
          <path d="M12 2l9.5 10L12 22 2.5 12z" stroke="currentColor" stroke-width="2" fill="currentColor"/>
          <path d="M8 12l2 2 4-4" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      </div>
      <span class="node-title">{{ data.label }}</span>
    </div>
    
    <!-- 节点描述 -->
    <div v-if="data.description" class="node-description">
      {{ data.description }}
    </div>
    
    <!-- 条件表达式 -->
    <div v-if="data.condition" class="condition-section">
      <div class="params-title">条件</div>
      <div class="condition-expression">{{ data.condition }}</div>
    </div>
    
    <!-- 输入参数区域 -->
    <div v-if="data.inputs && data.inputs.length" class="node-params">
      <div class="params-title">输入</div>
      <div 
        v-for="(input, index) in data.inputs" 
        :key="input.id"
        class="param-item input-param"
      >
        <Handle 
          :id="input.id"
          :position="Position.Left" 
          type="target" 
          class="param-handle param-handle-input"
          :style="{ top: `${80 + index * 28}px` }"
        />
        <div class="param-info">
          <div class="param-name">{{ input.name }}</div>
          <div class="param-type">{{ input.type }}</div>
        </div>
      </div>
    </div>
    
    <!-- 输出参数区域 -->
    <div v-if="data.outputs && data.outputs.length" class="node-params">
      <div class="params-title">输出</div>
      <div 
        v-for="(output, index) in data.outputs" 
        :key="output.id"
        class="param-item output-param"
        :class="{ 'true-output': output.value === true, 'false-output': output.value === false }"
      >
        <div class="param-info">
          <div class="param-name">{{ output.name }}</div>
          <div class="param-type">{{ output.type }}</div>
        </div>
        <Handle 
          :id="output.id"
          :position="Position.Right" 
          type="source" 
          class="param-handle param-handle-output"
          :class="{ 'handle-true': output.value === true, 'handle-false': output.value === false }"
          :style="{ top: `${80 + index * 28}px` }"
        />
      </div>
    </div>
  </div>
</template>

<style scoped>
.workflow-node {
  background: white;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  min-width: 200px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  font-size: 12px;
}

.decision-node {
  border-color: #faad14;
}

.decision-node .node-header {
  background: linear-gradient(135deg, #faad14, #d48806);
  color: white;
}

.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px 6px 0 0;
  background: #f5f5f5;
}

.node-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.2);
}

.node-title {
  font-weight: 600;
  font-size: 13px;
}

.node-description {
  padding: 8px 12px;
  font-size: 11px;
  color: #666;
  border-bottom: 1px solid #f0f0f0;
}

.condition-section {
  padding: 8px 12px;
  border-bottom: 1px solid #f0f0f0;
}

.condition-expression {
  font-family: 'Courier New', monospace;
  font-size: 11px;
  background: #f8f9fa;
  padding: 4px 8px;
  border-radius: 4px;
  color: #d63384;
  border: 1px solid #e9ecef;
}

.node-params {
  padding: 8px 12px;
}

.params-title {
  font-size: 10px;
  color: #999;
  font-weight: 600;
  margin-bottom: 6px;
  text-transform: uppercase;
}

.param-item {
  display: flex;
  align-items: center;
  padding: 4px 0;
  min-height: 24px;
  position: relative;
}

.input-param {
  padding-left: 12px;
}

.output-param {
  padding-right: 12px;
  justify-content: flex-end;
}

.true-output {
  color: #52c41a;
}

.false-output {
  color: #ff4d4f;
}

.param-info {
  flex: 1;
}

.param-name {
  font-size: 11px;
  font-weight: 500;
  color: #333;
  line-height: 1.2;
}

.param-type {
  font-size: 10px;
  color: #999;
  line-height: 1.2;
}

.param-handle {
  width: 8px;
  height: 8px;
  border: 2px solid #fff;
  border-radius: 50%;
  position: absolute !important;
}

.param-handle-input {
  background: #1890ff;
  left: -6px;
}

.param-handle-output {
  background: #52c41a;
  right: -6px;
}

.handle-true {
  background: #52c41a !important;
}

.handle-false {
  background: #ff4d4f !important;
}

.param-handle:hover {
  transform: scale(1.2);
  transition: transform 0.2s ease;
}

/* 连接状态 */
.param-handle.connected {
  background: #722ed1;
  box-shadow: 0 0 0 2px rgba(114, 46, 209, 0.2);
}
</style>