<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 === tCompleted.value" class="step-icon completed">
            <el-icon><Check /></el-icon>
          </div>
          <div v-else-if="step.status === tProcessing.value" class="step-icon processing">
            {{ index + 1 }}
          </div>
          <div v-else-if="step.status === tFailed.value" 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 !== tWaiting.value" class="step-details">
              {{ tCurrentProgress.value.replace('{current}', step.current).replace('{total}', step.total) }}
              {{ getCurrentGoodsNo(step) }}
              <span v-if="step.error" style="color: red; margin-left:5px;">{{ tFailedCount.value.replace('{count}', step.error) }}</span>
            </div>
          </div>
          <div v-if="step.folder" class="step-actions">
            <span class="view-results" @click="handleViewResults(step)">{{ tViewResults }}</span>
          </div>
        </div>

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

    <!-- 完成按钮 -->
    <div
        v-if="!disabledButton"
         class="completion-section">
      <el-button
        type="primary"
        class="completion-button"
        @click="handleComplete"
      >
        {{ tProcessComplete }}
      </el-button>
    </div>
  </div>
</template>

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

interface StepData {
  msg_type: string
  goods_art_no: string
  name: string
  status: string
  current: number
  total: number
  error: number
  folder?: string
}

interface Props {
  steps: StepData[]
  disabledButton?: boolean
  useNewProgress?: boolean
  onComplete?: () => void
  onOpenFolder?: (folder: string) => void // 添加打开目录的回调函数
}

const props = withDefaults(defineProps<Props>(), {
  steps: () => [],
  useNewProgress: false,
  disabledButton: true,
  onComplete: () => {},
  onOpenFolder: () => {} // 默认空函数
})

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

// i18n 翻译
const tWaiting = computed(() => i18n.global.t('loadingDialog.waiting'))
const tProcessing = computed(() => i18n.global.t('loadingDialog.processing'))
const tCompleted = computed(() => i18n.global.t('loadingDialog.completed'))
const tFailed = computed(() => i18n.global.t('loadingDialog.failed'))
const tViewResults = computed(() => i18n.global.t('loadingDialog.viewResults'))
const tProcessComplete = computed(() => i18n.global.t('loadingDialog.processComplete'))
const tCurrentProgress = computed(() => i18n.global.t('loadingDialog.currentProgress'))
const tFailedCount = computed(() => i18n.global.t('loadingDialog.failedCount'))

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

// 获取步骤样式类
const getStepClass = (step: StepData) => {
  return {
    'step-completed': step.status === tCompleted.value,
    'step-processing': step.status === tProcessing.value,
    'step-failed': step.status === tFailed.value,
    'step-waiting': step.status === tWaiting.value
  }
}

// 获取进度百分比
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)
  // 如果有folder路径，则调用外部传入的打开目录方法
  if (step.folder && props.onOpenFolder) {
    props.onOpenFolder(step.folder)
  }
}

// 处理完成按钮点击
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>
