<template>
  <el-dialog
    v-model="visible"
    :show-close="!requesting"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="700px"
    custom-class="loading-dialog-EL"
    align-center
    append-to-body
  >
    <div class="loading-content">
      <ProgressSteps
        v-if="useNewProgress && progressSteps.length > 0"
        :steps="progressSteps"
        :disabled-button="disabledButton"
        :use-new-progress="useNewProgress"
        @complete="handleButtonClick"
        :on-open-folder="onOpenFolder"
        :message="message"
      />

      <div v-else class="progress-container">
        <div class="progress-bar">
          <div
            class="progress-inner"
            :style="{ width: `${progress}%` }"
          ></div>
        </div>
        <span class="progress-text">{{ progress.toFixed(2) }}%</span>
      </div>

      <div class="message" style="display: none">{{ message }}</div>
      <slot name="errList"></slot>
      <slot name="progressMessages"></slot>

      <el-button
          v-if="!disabledButton && !useNewProgress"
          :disabled="disabledButton"
          type="primary"
          class="action-button   button--primary1  mar-top-20"
          @click="handleButtonClick"
      >
        {{ buttonText }}
      </el-button>

      <el-button
          v-if="message  === allFailedText"
          type="primary"
          class="action-button   button--primary1  mar-top-20"
          @click="visible = false"
      >
        {{ message }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits , watch } from 'vue'
import ProgressSteps from './ProgressSteps.vue'
import i18n from '@/locales'

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

interface Props {
  modelValue: boolean
  progress?: number
  message?: string
  disabledButton?: boolean
  buttonText?: string
  requesting?: boolean
  useNewProgress?: boolean
  progressSteps?: StepData[]
  onOpenFolder?: (folder: string) => void
}

const props = withDefaults(defineProps<Props>(), {
  progress: 0,
  message: '',
  disabledButton: true,
  buttonText: '',
  requesting: false,
  useNewProgress: false,
  progressSteps: () => [],
  onOpenFolder: () => {}
})

// 使用 i18n 默认值
const defaultMessage = i18n.global.t('loadingDialog.processing')
const defaultButtonText = i18n.global.t('loadingDialog.processComplete')
const allFailedText = i18n.global.t('loadingDialog.allFailed')

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void
  (e: 'button-click'): void
}>()

const visible = ref(props.modelValue)

watch(() => visible.value, (newVal) => {
  emit('update:modelValue', newVal)
})

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

const handleButtonClick = () => {
  emit('button-click')
}
</script>
<style lang="scss">
.loading-dialog-EL{
  border-radius: 10px;
  .el-dialog__header {
    display: none;
    padding: 0;
  }
 .el-dialog__body {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    padding: 20px 20px;
  }
}
</style>

<style lang="scss" scoped>
.loading-dialog {
  :deep(.el-dialog__header) {
    display: none;
    padding: 0;
  }
  :deep(.el-dialog__body) {
    padding:0;
  }
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.progress-container {
  width: 100%;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;

  .progress-bar {
    flex: 1;
    height: 6px;
    background: #E4E7ED;
    border-radius: 3px;
    overflow: hidden;

    .progress-inner {
      height: 100%;
      background: linear-gradient(90deg, #2FB0FF,#B863FB);
      border-radius: 3px;
      transition: width 0.3s ease;
    }
  }

  .progress-text {
    min-width: 45px;
    color: #606266;
    font-size: 14px;
  }
}

.message {
  color: #606266;
  font-size: 14px;
  margin-bottom: 0px;
}

.action-button {
  padding: 10px  20px;
}
</style>
