<template>
  <!-- 导入流程弹出框 -->
  <el-dialog v-model="state.showImportDialog" title="批量导入" width="70%" :show-close="false" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <template #header>
      <div class="dialog-header">
        <span class="dialog-title">批量导入</span>
        <el-icon class="close-icon" @click="close">
          <Close />
        </el-icon>
      </div>
    </template>
    <div class="steps-container">
      <div class="steps">
        <div v-for="(step, index) in importSteps" :key="index" class="step"
          :class="{ 'active-step': importCurrentStep > index, 'current-step': importCurrentStep === index + 1 }">
          <div class="step-circle">
            <span class="step-number">{{ index + 1 }}</span>
          </div>
          <div class="step-text">{{ step }}</div>
          <div v-if="index < importSteps.length - 1" class="step-line"></div>
        </div>
      </div>
    </div>
    <div v-if="importCurrentStep === 1" class="step-content">
      <!-- 下载模板部分 -->
      <div class="import-section">
        <div class="section-icon">
          <img src="@/assets/images/download-icon.png" alt="下载图标" width="48">
        </div>
        <div class="section-content">
          <div class="section-title">填写需要导入的数据</div>
          <div class="section-description">
            请按照数据模板的格式准备导入数据，模板中的表头名称不可更改，表头行不能删除，表格格式不能修改
          </div>
          <el-button type="primary" plain @click="emit('downloadTemplate')" class="section-button">下载模板</el-button>
        </div>
      </div>

      <!-- 上传文件部分 -->
      <div class="import-section">
        <div class="section-icon">
          <img src="@/assets/images/upload-icon.png" alt="上传图标" width="48">
        </div>
        <div class="section-content">
          <div class="section-title">上传填好的表格</div>
          <div class="section-description">
            文件后缀名必须为xlsx（即Excel格式），文件大小不得大于5MB，数据不超过2000条
          </div>
          <el-button type="primary" plain @click="triggerFileUpload" class="section-button">上传文件</el-button>
          <input ref="fileInput" type="file" accept=".xlsx" style="display: none"
            @change="(event)=>emit('handleFileUpload', event)" />
          <div v-if="uploadedFile" class="file-info">
            <span class="file-name">{{ uploadedFile.name }}</span>
            <el-icon class="delete-icon" @click.stop="emit('removeFile')">
              <Close />
            </el-icon>
          </div>
        </div>
      </div>

      <div class="import-actions">
        <el-button type="primary" :disabled="importFailedCount > 0" @click="emit('handleImportNextStep')">
          下一步
        </el-button>
      </div>
    </div>
    <div v-if="importCurrentStep === 2" class="step-content">
      <div v-loading="importLoading" class="import-analysis">
        <div class="analysis-card">
          <div class="analysis-icon">
            <img src="@/assets/images/success-icon.png" alt="成功图标" width="36">
          </div>
          <div class="analysis-content">
            <div class="analysis-title">本次可导入数据</div>
            <div class="analysis-count">{{ importSuccessCount }}条</div>
          </div>
        </div>

        <div class="analysis-card">
          <div class="analysis-icon">
            <img src="@/assets/images/error-icon.png" alt="错误图标" width="36">
          </div>
          <div class="analysis-content">
            <div class="analysis-title">本次不可导入数据</div>
            <div class="analysis-count">{{ importFailedCount }}条</div>
          </div>
        </div>
      </div>

      <div class="error-list">
        <!-- 表格 -->
        <el-table :data="importError" border style="width: 100%">
          <el-table-column prop="rowN" label="行数" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column prop="origin" label="原始数据" width="300" show-overflow-tooltip></el-table-column>
          <el-table-column prop="error" label="错误提示" show-overflow-tooltip></el-table-column>
        </el-table>
      </div>

      <div class="import-actions">
        <el-button plain @click="emit('handleImportLastStep')">返回重新上传</el-button>
        <el-button type="primary" :disabled="importSuccessCount == 0" @click="emit('handleImportNextStep')">
          下一步
        </el-button>
      </div>
    </div>
    <div v-if="importCurrentStep === 3" class="step-content">
      <div class="import-result">
        <p>导入完成！</p>
        <p>成功导入 {{ importSuccessCount }} 条数据</p>
        <p v-if="importFailedCount > 0">失败 {{ importFailedCount }} 条数据</p>
        <el-button v-if="importFailedCount > 0" type="text" @click="emit('downloadFailedData')">下载失败数据</el-button>
      </div>
      <div style="display: flex;justify-content: center;">
      <el-button type="primary" @click="close" class="import-actions">
        关闭
      </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { defineProps, ref, defineEmits,reactive ,defineExpose } from 'vue';

const importSteps = ['上传文件', '执行导入', '导入完成']
const fileInput = ref(null)
const props = defineProps({
  importCurrentStep: {
    type: Number,
    default: 1,
  },
  uploadedFile: {
    type: Object,
    default: null
  },
  importFailedCount: {
    type: Number,
    default: 0,
  },
  importLoading: {
    type: Boolean,
    default: false,
  },
  importSuccessCount: {
    type: Number,
    default: 0,
  },
  importError: {
    type: Array,
    default: () => [],
  }
})

const emit = defineEmits([ 'downloadTemplate', 'handleFileUpload', 'removeFile', 'downloadFailedData', 'handleImportNextStep', 'handleImportLastStep',"handleQuery"])

const state = reactive({
  showImportDialog:false
})


// 触发文件上传
const triggerFileUpload = () => {
  fileInput?.value?.click()
}
const show = () => {
  state.showImportDialog = true
}
const close = () => {
  state.showImportDialog = false
  emit('handleQuery')
}

defineExpose({
	show,
  close
});

</script>
<style scoped>
.settings-btn {
  margin-left: 16px;
  padding: 8px 16px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: #333;
  transition: color 0.3s;
}

.settings-btn:hover,
.settings-btn.active {
  color: #1890ff;
}

.project-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  background-color: white;
}

.container {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.filter-selector {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.year-selector {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 250px;
}

.subject-selector {
  flex: 1;
}

.year-dropdown {
  width: 120px;
}

.year-dropdown :deep(.el-input__inner) {
  border-radius: 4px;
  transition: border-color 0.3s;
}

.full-year-btn {
  width: 80px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  background-color: #FFF;
  color: #606266;
  transition: all 0.3s;
}

.btn-hover {
  border-color: #409EFF;
  color: #409EFF;
}

.btn-active {
  background-color: #409EFF;
  color: #FFF;
  border-color: #409EFF;
}

.month-selector {
  margin-bottom: 30px;
}

.month-scroll-container {
  display: flex;
  overflow-x: auto;
  gap: 15px;
  padding-bottom: 10px;
}

.month-item {
  position: relative;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
  color: #606266;
  transition: color 0.3s;
}

.month-item:hover {
  color: #409EFF;
}

.month-active {
  color: #409EFF;
  font-weight: bold;
}

.month-underline {
  position: absolute;
  bottom: -5px;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #409EFF;
}

.data-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(12%, 1fr));
  gap: 15px;
  margin-bottom: 20px;
  width: 100%;
}

.data-card {
  padding: 15px;
  border-radius: 8px;
  text-align: center;
  background-color: #fff;
}

.card-title {
  color: #606266;
  font-size: 14px;
  margin-bottom: 10px;
}

.card-value {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
}

.button-group2 {
  margin-bottom: 20px;
}

.device-table-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  position: relative;
}

.filter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  height: 100%;
}

.filter-icon {
  color: #c0c4cc;
  font-size: 14px;
  margin-left: 5px;
  transition: all 0.3s;
}

.filter-header:hover .filter-icon {
  color: #409eff;
}



.filter-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.allocation-link {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}

.allocation-link:hover {
  color: #66b1ff;
}

.batch-actions {
  margin-top: 20px;
  display: flex;
  gap: 10px;
}

.gray-btn {
  color: #606266;
  border-color: #dcdfe6;
}

.gray-btn:hover {
  color: #409eff;
  border-color: #409eff;
}

.red-btn {
  color: #fff;
  background-color: #f56c6c;
  border-color: #f56c6c;
}

.red-btn:hover {
  background-color: #f78989;
  border-color: #f78989;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.action-btn {
  padding: 5px 8px;
  margin: 0 2px;
}


.form-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.form-visible .form-overlay {
  opacity: 1;
}

.slide-form {
  position: absolute;
  top: 0;
  right: -30%;
  width: 30%;
  height: 100%;
  background-color: #fff;
  box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);
  transition: right 0.3s ease;
  display: flex;
  flex-direction: column;
}

.form-visible .slide-form {
  right: 0;
}

.form-header {
  padding: 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
}

.close-icon {
  cursor: pointer;
  font-size: 20px;
  margin-right: 10px;
  color: #909399;
}

.close-icon:hover {
  color: #409eff;
}

.form-title {
  font-size: 18px;
  font-weight: bold;
}

.divider {
  flex-grow: 1;
  height: 1px;
  background-color: #eee;
  margin-left: 10px;
}

.form-content {
  flex-grow: 1;
  padding: 20px;
  overflow-y: auto;
}

.form-footer {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
  border-top: 1px solid #eee;
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-right: 20px;
}

.dialog-title {
  font-size: 18px;
  font-weight: bold;
}

.steps-container {
  margin: 20px 0 30px;
}

.steps {
  display: flex;
  justify-content: space-between;
  position: relative;
}

.step {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  flex: 1;
}

.step-circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #f0f0f0;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 8px;
  z-index: 1;
}

.step-number {
  color: #999;
  font-weight: bold;
}

.step-text {
  color: #999;
  font-size: 14px;
}

.step-line {
  position: absolute;
  top: 15px;
  left: 50%;
  right: -50%;
  height: 2px;
  background-color: #f0f0f0;
  z-index: 0;
}

.step.active-step .step-circle,
.step.current-step .step-circle {
  background-color: #409eff;
}

.step.active-step .step-number,
.step.current-step .step-number {
  color: white;
}

.step.active-step .step-text,
.step.current-step .step-text {
  color: #333;
  font-weight: bold;
}

.step.active-step .step-line {
  background-color: #409eff;
}

.step-content {
  min-height: 200px;
  padding: 0 20px;
}

.reconciliation-table {
  margin-top: 20px;
  max-height: 400px;
  overflow-y: auto;
}

.reconciliation-progress {
  margin: 50px 0;
  text-align: center;
}

.reconciliation-result {
  text-align: center;
  margin: 40px 0;
}

.success-message {
  color: #67c23a;
  margin-top: 10px;
}

.error-message {
  color: #f56c6c;
  margin-top: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 20px;
}

.action-buttons {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.file-requirements {
  color: #999;
  font-size: 14px;
  margin-top: 20px;
}

.import-progress {
  margin: 50px 0;
  text-align: center;
}

.import-result {
  text-align: center;
  margin: 40px 0;
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .slide-form {
    width: 40%;
    right: -40%;
  }
}

@media (max-width: 992px) {
  .data-cards {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 768px) {
  .slide-form {
    width: 70%;
    right: -70%;
  }

  .data-cards {
    grid-template-columns: repeat(3, 1fr);
  }

  .filter-selector {
    flex-direction: column;
    gap: 10px;
  }

  .year-selector {
    width: 100%;
  }
}

@media (max-width: 576px) {
  .slide-form {
    width: 90%;
    right: -90%;
  }

  .data-cards {
    grid-template-columns: repeat(2, 1fr);
  }

  .container {
    padding: 10px;
  }

  .device-table-container {
    padding: 10px;
  }
}

/* 按钮组容器 */
.button-group2 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

/* 日期范围容器 */
.date-range-container {
  display: flex;
  align-items: center;
  margin-left: auto;
  /* 让这部分靠右 */
  gap: 10px;
}

/* 日期选择器样式 */
.date-range-picker {
  width: 350px;
}

/* 搜索和重置按钮样式 */
.search-action,
.reset-action {
  color: #409EFF;
  /* 蓝色文字 */
  padding: 8px 5px;
}

.search-action:hover,
.reset-action:hover {
  color: #66b1ff;
  /* 悬停时浅蓝色 */
}

.search-action .el-icon,
.reset-action .el-icon {
  margin-right: 5px;
  font-size: 14px;
}

/* 导入对话框样式 */
.import-section {
  display: flex;
  margin-bottom: 30px;
  padding: 20px;
  border-radius: 8px;
  background-color: #f8f9fa;
}

.section-icon {
  margin-right: 20px;
}

.section-content {
  flex: 1;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.section-description {
  font-size: 14px;
  color: #999;
  margin-bottom: 16px;
  line-height: 1.5;
}

.section-button {
  width: 120px;
}

.file-info {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background-color 0.3s;
}

.file-info:hover {
  background-color: #f0f0f0;
}

.file-name {
  font-size: 14px;
  color: #666;
}

.delete-icon {
  color: #999;
  cursor: pointer;
}

.delete-icon:hover {
  color: #ff4d4f;
}

/* 导入分析部分 */
.import-analysis {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.analysis-card {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 20px;
  border-radius: 8px;
  background-color: #f8f9fa;
}

.analysis-icon {
  margin-right: 16px;
}

.analysis-content {
  flex: 1;
}

.analysis-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 4px;
}

.analysis-count {
  font-size: 24px;
  font-weight: 600;
}

/* 错误列表 */
.error-list {
  margin-bottom: 20px;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
}

.error-list-header {
  display: flex;
  padding: 12px 16px;
  background-color: #f8f9fa;
  font-weight: 600;
  border-bottom: 1px solid #f0f0f0;
}

.error-list-header span {
  flex: 1;
}

.error-item {
  display: flex;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.error-row {
  width: 60px;
  color: #666;
}

.error-data {
  flex: 2;
  color: #666;
  line-height: 1.6;
}

.error-message {
  flex: 3;
  line-height: 1.6;
}

.error-text {
  color: #ff4d4f;
}

/* 导入操作按钮 */
.import-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}
</style>
