<template>
  <div class="approval-node" :class="[data.selected ? 'selected' : '', data.status ? `status-${data.status}` : '', isPaused ? 'paused' : '']">
    <Handle type="target" :position="Position.Left" id="input" />
    <div class="node-header">
      <UserOutlined class="node-icon" />
      <span class="node-title">{{ data.label }}</span>
      <a-tag v-if="isPaused" color="orange" class="pause-tag">等待审批</a-tag>
    </div>
    <div class="node-body">
      <div v-if="data.config?.approver" class="node-detail">
        <span class="detail-label">审批人:</span>
        <span class="detail-value">{{ data.config.approver }}</span>
      </div>
      <div v-if="data.config?.timeout" class="node-detail">
        <span class="detail-label">超时:</span>
        <span class="detail-value">{{ data.config.timeout }}s</span>
      </div>
    </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="output" />
  </div>
</template>

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

const props = defineProps<{
  data: WorkflowNodeData
}>()

const isPaused = computed(() => {
  return props.data.config?.status === 'paused'
})
</script>

<style scoped>
.approval-node {
  background: var(--bg-primary);
  border: 2px solid var(--color-primary);
  border-radius: 8px;
  padding: 8px 12px;
  min-width: 160px;
  font-size: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: border-color 0.3s, box-shadow 0.3s;
  position: relative;
}

.approval-node:hover {
  box-shadow: 0 4px 12px rgba(114, 46, 209, 0.25);
}

.approval-node.selected {
  border-color: var(--color-primary-hover);
  box-shadow: 0 0 0 3px rgba(114, 46, 209, 0.2);
}

.approval-node.paused {
  border-color: var(--color-warning);
  animation: pulse-border 2s ease-in-out infinite;
}

/* Execution status styles */
.approval-node.status-running {
  border-color: var(--color-info);
  box-shadow: 0 0 8px rgba(24, 144, 255, 0.5);
  animation: pulse 1.5s ease-in-out infinite;
}

.approval-node.status-completed {
  border-color: var(--color-success);
}

.approval-node.status-failed {
  border-color: var(--color-error);
}

.approval-node.status-waiting_approval {
  border-color: var(--color-warning);
  box-shadow: 0 0 8px rgba(250, 173, 20, 0.5);
  animation: pulse-waiting 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 4px rgba(24, 144, 255, 0.3); }
  50% { box-shadow: 0 0 12px rgba(24, 144, 255, 0.7); }
}

@keyframes pulse-waiting {
  0%, 100% { box-shadow: 0 0 4px rgba(250, 173, 20, 0.3); }
  50% { box-shadow: 0 0 12px rgba(250, 173, 20, 0.7); }
}

@keyframes pulse-border {
  0%, 100% { box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08); }
  50% { box-shadow: 0 0 0 4px rgba(250, 140, 22, 0.3); }
}

/* 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);
}

.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); }
}

.node-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

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

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

.pause-tag {
  font-size: 10px;
  line-height: 16px;
  padding: 0 4px;
  margin-left: auto;
}

.node-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.node-detail {
  display: flex;
  gap: 4px;
  color: var(--text-secondary);
  font-size: 11px;
}

.detail-label {
  color: var(--text-placeholder);
}

.detail-value {
  color: var(--text-secondary);
}
</style>
