<!-- =============================================================================
// 企微IT智能服务台 — 决策树递归节点组件
// =============================================================================
// 说明：递归渲染决策树节点
// 节点类型：
//   - step: 圆形编号节点 + 连接线 + 标签
//   - decision: 黄底方块 "❓ 判断" + 判断文字 + 是/否分支
// ============================================================================= -->

<template>
  <div class="flowchart-node" :class="[`flowchart-node--${node.type}`, `flowchart-node--${node.status || 'pending'}`]">
    <!-- 步骤节点 -->
    <div v-if="node.type === 'step'" class="flowchart-step">
      <div class="flowchart-step__indicator" :class="statusClass">
        <span class="flowchart-step__number">{{ stepIndex }}</span>
      </div>
      <div class="flowchart-step__connector" :class="statusClass"></div>
      <div class="flowchart-step__label" :class="statusClass">{{ node.label }}</div>
    </div>

    <!-- 判断节点 -->
    <div v-if="node.type === 'decision'" class="flowchart-decision">
      <div class="flowchart-decision__box">
        <span class="flowchart-decision__icon">❓</span>
        <span class="flowchart-decision__text">{{ node.label }}</span>
      </div>

      <!-- 分支 -->
      <div class="flowchart-decision__branches">
        <!-- "是" 分支 -->
        <div v-if="node.yes_branch" class="flowchart-branch flowchart-branch--yes">
          <div class="flowchart-branch__label">是</div>
          <div class="flowchart-branch__connector"></div>
          <div class="flowchart-branch__content">
            <FlowchartNode
              :node="node.yes_branch"
              :base-index="stepIndex"
            />
          </div>
        </div>

        <!-- "否" 分支 -->
        <div v-if="node.no_branch" class="flowchart-branch flowchart-branch--no">
          <div class="flowchart-branch__label">否</div>
          <div class="flowchart-branch__connector"></div>
          <div class="flowchart-branch__content">
            <FlowchartNode
              :node="node.no_branch"
              :base-index="stepIndex"
            />
          </div>
        </div>
      </div>
    </div>

    <!-- 子节点列表（step 类型的 children） -->
    <div v-if="node.type === 'step' && node.children && node.children.length > 0" class="flowchart-node__children">
      <FlowchartNode
        v-for="(child, idx) in node.children"
        :key="child.id"
        :node="child"
        :base-index="stepIndex + idx + 1"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
// ============================================================================
// 导入
// ============================================================================
import { computed } from 'vue'
import type { FlowchartNode as FlowchartNodeType } from '@/api/troubleshooting'

// ============================================================================
// Props
// ============================================================================

interface Props {
  /** 节点数据 */
  node: FlowchartNodeType
  /** 基础序号（用于步骤编号） */
  baseIndex?: number
}

const props = withDefaults(defineProps<Props>(), {
  baseIndex: 1,
})

// ============================================================================
// 计算属性
// ============================================================================

/** 步骤序号（1-based） */
const stepIndex = computed(() => {
  return props.baseIndex
})

/** 状态样式类 */
const statusClass = computed(() => {
  return `flowchart-status--${props.node.status || 'pending'}`
})
</script>

<style scoped>
/* 节点容器 */
.flowchart-node {
  margin-bottom: 4px;
}

/* ===== 步骤节点 ===== */
.flowchart-step {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 步骤指示器（圆形编号） */
.flowchart-step__indicator {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 700;
  color: var(--bg-secondary);
}

.flowchart-step__indicator.flowchart-status--done {
  background: var(--color-success);
}

.flowchart-step__indicator.flowchart-status--current {
  background: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.flowchart-step__indicator.flowchart-status--pending {
  background: var(--bg-active);
  color: var(--text-tertiary);
}

/* 连接线 */
.flowchart-step__connector {
  width: 2px;
  height: 16px;
  margin-left: 10px;
}

.flowchart-step__connector.flowchart-status--done {
  background: var(--color-success);
}

.flowchart-step__connector.flowchart-status--current {
  background: var(--accent);
}

.flowchart-step__connector.flowchart-status--pending {
  background: var(--border-color);
}

/* 步骤标签 */
.flowchart-step__label {
  font-size: 13px;
  line-height: 1.5;
}

.flowchart-step__label.flowchart-status--done {
  color: var(--color-success);
}

.flowchart-step__label.flowchart-status--current {
  color: var(--accent);
  font-weight: 600;
}

.flowchart-step__label.flowchart-status--pending {
  color: var(--text-tertiary);
}

/* 子节点容器 */
.flowchart-node__children {
  margin-left: 11px;
  padding-left: 12px;
  border-left: 2px solid var(--border-color);
}

/* ===== 判断节点 ===== */
.flowchart-decision {
  margin-bottom: 4px;
}

.flowchart-decision__box {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: var(--warning-soft);
  border: 1px solid var(--warning-soft);
  border-radius: var(--radius-md);
  font-size: 13px;
  color: var(--color-warning);
  font-weight: 500;
  margin-bottom: 8px;
}

.flowchart-decision__icon {
  font-size: 14px;
}

.flowchart-decision__text {
  font-size: 13px;
}

/* 分支容器 */
.flowchart-decision__branches {
  display: flex;
  gap: 16px;
  margin-top: 4px;
}

/* 分支 */
.flowchart-branch {
  flex: 1;
  min-width: 0;
}

.flowchart-branch__label {
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 4px;
  padding: 1px 8px;
  border-radius: 8px;
  display: inline-block;
}

.flowchart-branch--yes .flowchart-branch__label {
  background: var(--success-soft);
  color: var(--color-success);
}

.flowchart-branch--no .flowchart-branch__label {
  background: var(--danger-soft);
  color: var(--color-danger);
}

.flowchart-branch__connector {
  width: 1px;
  height: 8px;
  margin-left: 16px;
  border-left: 2px dashed var(--border-color);
}

.flowchart-branch__content {
  padding-left: 12px;
  border-left: 2px dashed var(--border-color);
}
</style>
