<template>
  <div class="progress-bar-container">
    <div v-if="showInfo" class="info-row">
      <span class="filename" :title="filename">{{ filename || '未命名文件' }}</span>
      <span class="file-size">{{ formattedSize }}</span>
    </div>

    <div class="progress-track-wrapper">
      <div class="progress-track" :class="`status-${status}`">
        <div
          class="progress-fill"
          :class="[`status-${status}`, { 'with-text': percent >= 10 }]"
          :style="{ width: `${percent}%` }"
        >
          <span v-if="percent >= 10" class="percent-text">{{ percent }}%</span>
        </div>

        <div v-if="status !== 'idle'" class="status-icon">
          <LoadingOutlined v-if="status === 'uploading'" class="icon-spin" />
          <CheckCircleOutlined v-else-if="status === 'success'" class="icon-success" />
          <CloseCircleOutlined v-else-if="status === 'error'" class="icon-error" />
        </div>
      </div>
    </div>

    <div v-if="statusText" class="status-message" :class="`text-${status}`">
      {{ statusText }}
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { LoadingOutlined, CheckCircleOutlined, CloseCircleOutlined } from '@ant-design/icons-vue'

const props = defineProps({
  percent: {
    type: Number,
    default: 0,
    validator: (val) => val >= 0 && val <= 100
  },
  status: {
    type: String,
    default: 'idle',
    validator: (val) => ['idle', 'uploading', 'success', 'error'].includes(val)
  },
  filename: {
    type: String,
    default: ''
  },
  loaded: {
    type: Number,
    default: 0
  },
  total: {
    type: Number,
    default: 0
  },
  showInfo: {
    type: Boolean,
    default: true
  },
  error: {
    type: String,
    default: ''
  }
})

const formattedSize = computed(() => {
  const formatBytes = (bytes) => {
    if (bytes === 0) return '0 B'
    const k = 1024
    const sizes = ['B', 'KB', 'MB', 'GB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
  }

  if (props.total > 0) {
    return `${formatBytes(props.loaded)} / ${formatBytes(props.total)}`
  }
  return ''
})

const statusText = computed(() => {
  switch (props.status) {
    case 'uploading':
      return `上传中 ${props.percent}%...`
    case 'success':
      return '✅ 上传成功'
    case 'error':
      return `❌ 上传失败: ${props.error || '未知错误'}`
    default:
      return ''
  }
})
</script>

<style scoped>
.progress-bar-container {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  width: 100%;
}

.info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  gap: 12px;
}

.filename {
  flex: 1;
  font-size: 14px;
  color: #374151;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.file-size {
  font-size: 13px;
  color: #6b7280;
  white-space: nowrap;
  flex-shrink: 0;
}

.progress-track-wrapper {
  position: relative;
  width: 100%;
}

.progress-track {
  position: relative;
  height: 8px;
  border-radius: 4px;
  overflow: visible;
  transition: background-color 0.3s ease;
}

.progress-track.status-idle {
  background-color: #e5e7eb;
}

.progress-track.status-uploading {
  background-color: #dbeafe;
}

.progress-track.status-success {
  background-color: #d1fae5;
}

.progress-track.status-error {
  background-color: #fee2e2;
}

.progress-fill {
  position: relative;
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  min-width: 0;
}

.progress-fill.status-uploading {
  background: linear-gradient(90deg, #3b82f6, #60a5fa, #3b82f6);
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite, pulse 1s ease-in-out infinite;
}

.progress-fill.status-success {
  background-color: #10b981;
}

.progress-fill.status-error {
  background-color: #ef4444;
}

.progress-fill.with-text .percent-text {
  font-size: 11px;
  font-weight: 600;
  color: white;
  white-space: nowrap;
  user-select: none;
}

.status-icon {
  position: absolute;
  right: -24px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.icon-spin {
  color: #3b82f6;
  animation: spin 1s linear infinite;
}

.icon-success {
  color: #10b981;
}

.icon-error {
  color: #ef4444;
}

.status-message {
  margin-top: 6px;
  font-size: 13px;
  line-height: 1.4;
}

.text-uploading {
  color: #3b82f6;
}

.text-success {
  color: #059669;
}

.text-error {
  color: #dc2626;
}

@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 640px) {
  .info-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .file-size {
    align-self: flex-start;
  }
}
</style>
