<template>
  <n-modal 
    v-model:show="show" 
    preset="card" 
    style="width: 600px"
    :mask-closable="status !== 'processing'"
    :close-on-esc="status !== 'processing'"
    @update:show="handleMaskClose"
  >
    <template #header>
      <div style="display: flex; align-items: center; gap: 8px">
        <n-icon v-if="status === 'processing'" :size="20">
          <Icon icon="tabler:hourglass" />
        </n-icon>
        <n-icon v-else-if="status === 'success'" :size="20" color="#10b981">
          <Icon icon="tabler:check-circle" />
        </n-icon>
        <n-icon v-else-if="status === 'error'" :size="20" color="#ef4444">
          <Icon icon="tabler:x-circle" />
        </n-icon>
        <h3 style="margin: 0">{{ title }}</h3>
      </div>
    </template>
    
    <div class="progress-content">
      <!-- 步骤列表 -->
      <div class="steps-container">
        <div 
          v-for="(step, index) in steps" 
          :key="index"
          class="step-item"
          :class="{
            'step-active': step.status === 'processing',
            'step-success': step.status === 'success',
            'step-error': step.status === 'error',
            'step-pending': step.status === 'pending'
          }"
        >
          <div class="step-icon">
            <n-icon v-if="step.status === 'processing'" :size="20">
              <Icon icon="tabler:loader-2" class="rotating" />
            </n-icon>
            <n-icon v-else-if="step.status === 'success'" :size="20" color="#10b981">
              <Icon icon="tabler:check" />
            </n-icon>
            <n-icon v-else-if="step.status === 'error'" :size="20" color="#ef4444">
              <Icon icon="tabler:x" />
            </n-icon>
            <n-icon v-else :size="20" color="#94a3b8">
              <Icon icon="tabler:circle" />
            </n-icon>
          </div>
          <div class="step-content">
            <div class="step-title">{{ step.title }}</div>
            <div v-if="step.message" class="step-message">{{ step.message }}</div>
            <div v-if="step.error" class="step-error-message">{{ step.error }}</div>
          </div>
        </div>
      </div>
    </div>
    
    <template #action>
      <div class="dialog-actions">
        <n-button 
          v-if="status === 'error' || status === 'success'" 
          type="default"
          size="medium"
          @click="handleClose"
          class="action-btn-close"
        >
          {{ t('Close') }}
        </n-button>
        <n-button 
          v-if="status === 'error'" 
          type="primary"
          size="medium"
          @click="handleRetry"
          class="action-btn-retry"
        >
          {{ t('Retry') }}
        </n-button>
      </div>
    </template>
  </n-modal>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { NModal, NIcon, NButton } from 'naive-ui'
import { Icon } from '@iconify/vue'
import { t } from '@/shared/i18n'

export interface Step {
  title: string
  status: 'pending' | 'processing' | 'success' | 'error'
  message?: string
  error?: string
}

interface Props {
  modelValue: boolean
  title: string
  steps: Step[]
  status: 'processing' | 'success' | 'error'
}

const props = defineProps<Props>()
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  'retry': []
}>()

const show = ref(props.modelValue)

watch(() => props.modelValue, (newVal) => {
  show.value = newVal
})

watch(show, (newVal) => {
  emit('update:modelValue', newVal)
})

function handleClose() {
  if (props.status !== 'processing') {
    show.value = false
  }
}

function handleMaskClose(value: boolean) {
  // 如果用户通过点击遮罩或按ESC关闭，且不在处理中，则允许关闭
  if (!value && props.status !== 'processing') {
    show.value = false
  } else if (!value && props.status === 'processing') {
    // 处理中时不允许关闭，恢复显示
    show.value = true
  } else {
    show.value = value
  }
}

function handleRetry() {
  emit('retry')
}
</script>

<style scoped>
.progress-content {
  padding: 20px 0;
}

.steps-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.step-item {
  display: flex;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  transition: all 0.2s;
}

.step-pending {
  opacity: 0.5;
}

.step-active {
  background: #eff6ff;
  border: 1px solid #3b82f6;
}

.step-success {
  background: #f0fdf4;
  border: 1px solid #10b981;
}

.step-error {
  background: #fef2f2;
  border: 1px solid #ef4444;
}

.step-icon {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.step-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.step-title {
  font-weight: 500;
  color: #1f2937;
}

.step-message {
  font-size: 13px;
  color: #6b7280;
}

.step-error-message {
  font-size: 13px;
  color: #ef4444;
  margin-top: 4px;
}

.rotating {
  animation: rotate 1s linear infinite;
}

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

/* 对话框操作按钮布局 - 右下角对齐 */
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  width: 100%;
}

/* 关闭按钮 - 与返回按钮样式一致 */
.action-btn-close {
  background: #f3f4f6 !important;
  color: #374151 !important;
  border: 1px solid rgba(0, 0, 0, 0.08) !important;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.action-btn-close :deep(.n-button__border),
.action-btn-close :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}

.action-btn-close:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.action-btn-close:hover:not(:disabled) :deep(.n-button__border),
.action-btn-close:hover:not(:disabled) :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}

/* 重试按钮 - 与保存按钮样式一致 */
.action-btn-retry {
  background: #e6f8f0 !important;
  border: 1px solid #1fc76f !important;
  color: #0d684b !important;
}

.action-btn-retry :deep(.n-button__border) {
  border: none !important;
  border-color: transparent !important;
}

.action-btn-retry :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}

.action-btn-retry:hover {
  background: #dcfce7 !important;
  border-color: #1fc76f !important;
  border: 1px solid #1fc76f !important;
  color: #166534 !important;
  box-shadow: 0 2px 8px rgba(31, 199, 111, 0.15) !important;
}

.action-btn-retry:hover :deep(.n-button__border),
.action-btn-retry:hover :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}

.action-btn-retry:focus {
  background: #dcfce7 !important;
  border-color: #1fc76f !important;
  border: 1px solid #1fc76f !important;
  color: #166534 !important;
  box-shadow: 0 0 0 2px rgba(31, 199, 111, 0.2) !important;
}

.action-btn-retry:focus :deep(.n-button__border),
.action-btn-retry:focus :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}

.action-btn-retry:active {
  background: #1fc76f !important;
  border-color: #1fc76f !important;
  border: 1px solid #1fc76f !important;
  color: white !important;
  box-shadow: 0 1px 4px rgba(31, 199, 111, 0.2) !important;
}

.action-btn-retry:active :deep(.n-button__border),
.action-btn-retry:active :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}

.action-btn-retry:disabled {
  background: #f1f5f9 !important;
  border: 1px solid #e2e8f0 !important;
  border-color: #e2e8f0 !important;
  color: #94a3b8 !important;
  opacity: 0.6 !important;
  cursor: not-allowed !important;
}

.action-btn-retry:disabled :deep(.n-button__border),
.action-btn-retry:disabled :deep(.n-button__state-border) {
  border: none !important;
  border-color: transparent !important;
}
</style>

