<template>
  <div class="condition-node" :class="[data.selected ? 'selected' : '', data.status ? `status-${data.status}` : '']">
    <Handle type="target" :position="Position.Left" id="input" />
    <div class="diamond-shape">
      <div class="diamond-content">
        <BranchesOutlined class="node-icon" />
        <span class="node-title">{{ data.label }}</span>
      </div>
    </div>
    <div v-if="data.config?.expression" class="condition-expr">
      {{ data.config.expression }}
    </div>
    <div v-if="data.status" class="status-indicator">
      <LoadingOutlined v-if="data.status === 'running'" class="status-icon running-icon" />
      <CheckCircleFilled v-else-if="data.status === 'completed'" class="status-icon completed-icon" />
      <CloseCircleFilled v-else-if="data.status === 'failed'" class="status-icon failed-icon" />
      <ClockCircleFilled v-else-if="data.status === 'waiting_approval'" class="status-icon waiting-icon" />
    </div>
    <Handle type="source" :position="Position.Right" id="true" class="handle-true" />
    <span class="label-true">是</span>
    <Handle type="source" :position="Position.Bottom" id="false" class="handle-false" />
    <span class="label-false">否</span>
  </div>
</template>

<script setup lang="ts">
import { Handle, Position } from '@vue-flow/core'
import { BranchesOutlined, LoadingOutlined, CheckCircleFilled, CloseCircleFilled, ClockCircleFilled } from '@ant-design/icons-vue'
import type { WorkflowNodeData } from '@/utils/workflowSerializer'

defineProps<{
  data: WorkflowNodeData
}>()
</script>

<style scoped>
.condition-node {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 140px;
}

.diamond-shape {
  background: var(--bg-primary);
  border: 2px solid var(--color-warning);
  border-radius: 8px;
  padding: 8px 12px;
  box-shadow: var(--shadow-sm);
  transform: none;
  transition: border-color 0.3s, box-shadow 0.3s;
}

.condition-node:hover .diamond-shape {
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.25);
}

.condition-node.selected .diamond-shape {
  border-color: var(--color-warning);
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.2);
}

/* Execution status styles */
.condition-node.status-running .diamond-shape {
  border-color: var(--color-info);
  box-shadow: 0 0 8px rgba(59, 130, 246, 0.5);
  animation: pulse 1.5s ease-in-out infinite;
}

.condition-node.status-completed .diamond-shape {
  border-color: var(--color-success);
}

.condition-node.status-failed .diamond-shape {
  border-color: var(--color-error);
}

.condition-node.status-waiting_approval .diamond-shape {
  border-color: var(--color-warning);
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 4px rgba(59, 130, 246, 0.3); }
  50% { box-shadow: 0 0 12px rgba(59, 130, 246, 0.7); }
}

/* Status indicator icon */
.status-indicator {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
  z-index: 1;
}

.status-icon {
  font-size: 14px;
}

.running-icon {
  color: var(--color-info);
  animation: spin 1s linear infinite;
}

.completed-icon {
  color: var(--color-success);
}

.failed-icon {
  color: var(--color-error);
}

.waiting-icon {
  color: var(--color-warning);
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.diamond-content {
  display: flex;
  align-items: center;
  gap: 6px;
}

.node-icon {
  color: var(--color-warning);
  font-size: 14px;
}

.node-title {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 13px;
}

.condition-expr {
  margin-top: 4px;
  padding: 2px 8px;
  background: var(--color-warning-light);
  border: 1px solid var(--color-warning-light);
  border-radius: 4px;
  font-size: 11px;
  color: var(--color-warning);
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.handle-true {
  background: var(--color-success);
}

.handle-false {
  background: var(--color-error);
}

.label-true {
  position: absolute;
  right: -22px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--color-success);
  font-size: 11px;
  font-weight: 600;
}

.label-false {
  position: absolute;
  bottom: -18px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--color-error);
  font-size: 11px;
  font-weight: 600;
}
</style>
