<template>
  <div class="progress-steps">
    <div class="steps-container">
      <div
        v-for="(step, index) in steps"
        :key="step.msg_type"
        class="step-item"
        :class="getStepClass(step)"
      >
        <!-- 步骤圆圈 -->
        <div class="step-circle">
          <div v-if="step.status === '处理完成'" class="step-icon completed">
            <el-icon><Check /></el-icon>
          </div>
          <div v-else-if="step.status === '正在处理'" class="step-icon processing">
            {{ index + 1 }}
          </div>
          <div v-else-if="step.status === '处理失败'" class="step-icon failed">
            <el-icon><Close /></el-icon>
          </div>
          <div v-else class="step-icon waiting">
            {{ index + 1 }}
          </div>
        </div>

        <!-- 步骤内容 -->
        <div class="step-content">
          <div class="step-title">{{ step.name }}</div>
          <div class="step-status">
            {{ getStepTitle(step) }}
            <div v-if="step.status !== '等待处理'" class="step-details">
              第{{ step.current }}/{{ step.total }}款 {{ getCurrentGoodsNo(step) }}
            </div>
          </div>
<!--          <div v-if="step.status === '处理完成'" class="step-actions">
            <span class="view-results" @click="handleViewResults(step)">查看结果</span>
          </div>-->
        </div>

        <!-- 连接线 -->
        <div v-if="index < steps.length - 1" class="step-connector"></div>
      </div>
    </div>

    <!-- 完成按钮 -->
    <div v-if="allStepsCompleted" class="completion-section">
      <el-button
        type="primary"
        class="completion-button"
        @click="handleComplete"
      >
        全部处理完毕，点击打开最终图片目录
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { Check, Close } from '@element-plus/icons-vue'

interface StepData {
  msg_type: string
  goods_art_no: string
  name: string
  status: '等待处理' | '正在处理' | '处理完成' | '处理失败'
  current: number
  total: number
  error: number
}

interface Props {
  steps: StepData[]
  onComplete?: () => void
}

const props = withDefaults(defineProps<Props>(), {
  steps: () => [],
  onComplete: () => {}
})

const emit = defineEmits<{
  (e: 'complete'): void
}>()


// 获取步骤标题
const getStepTitle = (step: StepData) => {
  return  step.status
}

// 获取步骤样式类
const getStepClass = (step: StepData) => {
  return {
    'step-completed': step.status === '处理完成',
    'step-processing': step.status === '正在处理',
    'step-failed': step.status === '处理失败',
    'step-waiting': step.status === '等待处理'
  }
}

// 获取进度百分比
const getProgressPercentage = (step: StepData) => {
  if (step.total === 0) return 0
  return Math.round((step.current / step.total) * 100)
}

// 检查是否所有步骤都完成
const allStepsCompleted = computed(() => {
  return props.steps.every(step => step.status === '处理完成')
})

// 获取当前货号（这里需要根据实际数据结构调整）
const getCurrentGoodsNo = (step: StepData) => {
  // 这里需要根据实际的数据结构来获取当前货号
  console.log('step===================');
  console.log(step);
  // 暂时返回一个示例值
  return step.goods_art_no
}

// 处理查看结果
const handleViewResults = (step: StepData) => {
  console.log('查看结果:', step)
  // 这里可以添加查看结果的逻辑
}

// 处理完成按钮点击
const handleComplete = () => {
  emit('complete')
  props.onComplete()
}

// 监听步骤变化
watch(() => props.steps, (newSteps) => {
  console.log('步骤更新:', newSteps)
}, { deep: true })
</script>

<style lang="scss" scoped>
.progress-steps {
  width: 100%;
  padding: 20px 0;
  background: #fff;
}

.steps-container {
  display: flex;
  flex-direction: column;
  gap: 0;
  position: relative;
  padding: 0 20px;
}

.step-item {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  position: relative;
  padding: 20px 0;
  min-height: 60px;
}

.step-circle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  flex-shrink: 0;
  position: relative;
  z-index: 2;
}

.step-icon {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  color: white;
  font-size: 14px;
  background: #FFF;
  border:1px solid #E5E5E5;
}

.step-icon.completed {
  border-color: #2957FF;
  color: #2957FF;
}

.step-icon.processing {
  border-color: #2957FF;
  color: #2957FF;
}

.step-icon.failed {
  border-color: #F56C6C;
  color: #F56C6C;
}

.step-icon.waiting {
  border-color:#E5E5E5;
  color: #E5E5E5;
}

.step-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  min-height: 32px;
  line-height: 32px;
}

.step-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 4px;
  min-width: 120px;
  text-align: left;
}

.step-status {
  font-size: 14px;
  color: #606266;
  margin-bottom: 4px;
  flex: 1;
  text-align: left;
  margin-left: 20px;
  display: flex;
}

.step-details {
  margin-left: 10px;
}

.step-actions {
  margin-top: 8px;
}

.view-results {
  color: #2957FF;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;

  &:hover {
    text-decoration: underline;
  }
}

.step-connector {
  position: absolute;
  top: 52px;
  left: 15px;
  width: 2px;
  height: calc(100% - 32px);
  background: #E4E7ED;
  z-index: 1;
}

.step-item:last-child .step-connector {
  display: none;
}

.completion-section {
  margin-top: 30px;
  text-align: center;
  padding: 0 20px;
}

.completion-button {
  background: linear-gradient(135deg, #2FB0FF, #B863FB);
  border: none;
  padding: 25px 30px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(47, 176, 255, 0.3);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(47, 176, 255, 0.4);
  }
}

// 步骤状态样式
.step-completed {
  .step-title {
    color: rgba(0,0,0,1);
    font-size: 16px;
  }
  .step-status {
    color: rgba(0,0,0,0.85);
    font-size: 14px;
  }
}

.step-processing {
  .step-title {
    font-weight: bold;
  }
  .step-status {
    color: #000;
    font-size: 16px;
  }
}

.step-failed {
  .step-title {
    color: #F56C6C;
  }
}

.step-waiting {
  .step-title {
    color: rgba(0,0,0,0.45);
    font-size: 16px;
  }
  .step-status {
    color: rgba(0,0,0,0.45);
    font-size: 14px;
  }
}
</style>
