<template>
  <div
    class="flow-stepper"
    :class="{ clickable }"
    @click="onClick"
  >
    <template v-for="(step, idx) in steps" :key="step.key">
      <div
        class="flow-step"
        :class="step.state"
        :title="step.label"
      >
        <span class="flow-dot">
          <n-icon v-if="step.state === 'done'" size="12"><CheckmarkOutline /></n-icon>
          <span v-else class="flow-num">{{ idx + 1 }}</span>
        </span>
        <span class="flow-label">{{ step.shortLabel }}</span>
      </div>
      <div v-if="idx < steps.length - 1" class="flow-line" :class="step.state" />
    </template>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { NIcon } from 'naive-ui'
import { CheckmarkOutline } from '@vicons/ionicons5'
import type { ProcessPlanItemVO } from '@/api/orderProcessPlan'

const props = withDefaults(defineProps<{
  list?: ProcessPlanItemVO[]
  clickable?: boolean
}>(), {
  clickable: false,
})

const emit = defineEmits<{
  click: []
}>()

const steps = computed(() => {
  const list = [...(props.list ?? [])].sort((a, b) => (a.operNumber ?? 0) - (b.operNumber ?? 0))
  return list.map((item, idx) => {
    const label = item.processName || item.processCode || `工序${idx + 1}`
    const state = item.processStatus === 4 ? 'done' : item.processStatus != null && item.processStatus >= 2 ? 'active' : 'pending'
    return {
      key: item.planId ?? `${item.processCode}-${idx}`,
      label,
      shortLabel: label.length > 5 ? `${label.slice(0, 5)}…` : label,
      state,
    }
  })
})

function onClick() {
  if (props.clickable) emit('click')
}
</script>

<style scoped>
.flow-stepper {
  display: flex;
  align-items: center;
  gap: 0;
  min-width: 0;
  overflow: hidden;
}

.flow-stepper.clickable {
  cursor: pointer;
  border-radius: 6px;
  padding: 4px 6px;
  margin: -4px -6px;
  transition: background 0.15s;
}

.flow-stepper.clickable:hover {
  background: rgba(32, 128, 240, 0.08);
}

.flow-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  max-width: 56px;
}

.flow-dot {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  border: 2px solid #d9d9d9;
  background: #fff;
  color: #8c8c8c;
  transition: all 0.2s;
}

.flow-step.done .flow-dot {
  background: #18a058;
  border-color: #18a058;
  color: #fff;
}

.flow-step.active .flow-dot {
  background: #2080f0;
  border-color: #2080f0;
  color: #fff;
}

.flow-label {
  font-size: 11px;
  color: #8c8c8c;
  text-align: center;
  line-height: 1.2;
  white-space: nowrap;
}

.flow-step.done .flow-label,
.flow-step.active .flow-label {
  color: #333;
}

.flow-line {
  flex: 1;
  min-width: 12px;
  max-width: 28px;
  height: 2px;
  background: #e8e8e8;
  margin: 0 2px;
  margin-bottom: 16px;
}

.flow-line.done,
.flow-line.active {
  background: #18a058;
}

.flow-num {
  line-height: 1;
}
</style>
