<template>
  <div class="drawer-content-wrapper" :class="{ 'no-pointer-events': showUploadAnimation }">
    <!-- 文件上传动画（详情页仅加载数据，无需保存动画） -->
    <div v-if="showUploadAnimation" class="global-upload-animation">
      <div class="animation-mask"></div>
      <div class="animation-content">
        <div class="spinner"></div>
        <div class="animation-text">{{ animationText }}</div>
        <div class="animation-subtext">正在加载详情数据，请稍候...</div>
      </div>
    </div>

    <div class="drawer-scrollable-content">
    <div class="content-body">
      <QualificationFormDetail
        ref="qualificationFormDetail"
        :is-detail="true"
      />
    </div>
    </div>

    <!-- 操作栏：仅保留“取消”按钮（隐藏保存按钮） -->
    <div class="action-footer">
      <el-button class="reset-btn" @click="$emit('cancel')">关闭</el-button>
    </div>
  </div>
</template>

<script>
import QualificationFormDetail from './child/QualificationFormDetail.vue'
import { getDetailDataAPI } from "@/api/enterpriseLibrary/qualification/qualification";

export default {
  name: 'QualificationDetail',
  components: { QualificationFormDetail },
  props: {
    qualificationId: {  // 接收要查看详情的资质ID
      type: Number,
      required: true  // 详情页必须传递资质ID
    },
    enterpriseId: {  // 接收企业ID
      type: String,
      required: true
    },
    drawerVisible: {  // 接收抽屉显示状态（用于触发数据加载）
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showUploadAnimation: false,  // 仅用于加载数据的动画
      animationText: '加载中'
    }
  },
  methods: {
    // 加载详情数据（复用编辑页的加载逻辑，仅无保存操作）
    async loadQualificationDetail() {
      this.showUploadAnimation = true;
      try {
        const validParams = {
          qualificationId: this.qualificationId,
          enterpriseId: this.enterpriseId
        };
        const response = await getDetailDataAPI(validParams);
        const qualificationData = response.data || {};

        if (Array.isArray(qualificationData.files)) {
          qualificationData.files = qualificationData.files.map(file => ({
            ...file,
            lsFilePath: file.lsFilePath || file.fileUrl || file.response?.fileRes?.url || '',
            businessType: file.businessType || 'qualification_certificate',
            fileName: file.fileName || file.name
          }));
        }

        // 调用子表单回显数据
        this.$refs.qualificationFormDetail.setFormData(qualificationData);
      } catch (error) {
        this.$message.error('加载资质详情失败：' + error.message);
      } finally {
        this.showUploadAnimation = false;
      }
    },

    // 重置表单（供父组件关闭抽屉时调用，避免数据残留）
    resetForm() {
      this.$refs.qualificationFormDetail?.resetForm();
    }
  },
  watch: {
    // 监听抽屉显示状态：打开时加载详情数据
    drawerVisible: {
      handler(newVal) {
        if (newVal && this.qualificationId && this.enterpriseId) {
          this.loadQualificationDetail();  // 打开抽屉时加载详情
        }
      },
      immediate: true  // 初始化时立即检查（覆盖首次打开场景）
    }
  },
  beforeDestroy() {
    // 移除可能的事件监听（若子表单有全局事件，此处可补充）
    this.resetForm();
  }
}
</script>

<style scoped lang="scss">
// 抽屉内容总容器（flex布局）
.drawer-content-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 24px;
  position: relative; // 用于定位动画元素
}

// 可滚动内容区域（占满剩余空间）
.drawer-scrollable-content {
  flex: 1;
  overflow-y: auto;
  padding-right: 12px; // 预留滚动条空间，避免内容被遮挡

  // 滚动条样式优化
  &::-webkit-scrollbar {
    width: 6px;
  }
  &::-webkit-scrollbar-track {
    background: #f5f5f5;
    border-radius: 3px;
  }
  &::-webkit-scrollbar-thumb {
    background: #c0c4cc;
    border-radius: 3px;
    &:hover {
      background: #909399;
    }
  }
}

// 操作栏（固定在底部）
.action-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 16px;
  margin-top: 16px;
  border-top: 1px solid #f5f5f5; // 添加分割线，区分内容和操作栏
  flex-shrink: 0; // 防止被压缩
}

.global-upload-animation {
  position: absolute; // 相对于drawer-content-wrapper定位
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;

  .animation-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.6);
    backdrop-filter: blur(4px);
    transition: opacity 0.3s ease;
  }

  .animation-content {
    position: relative;
    z-index: 10000;
    text-align: center;
    background: linear-gradient(135deg, #ffffff 0%, #f8faff 100%);
    padding: 40px 50px;
    border-radius: 20px;
    box-shadow:
      0 10px 40px rgba(31, 114, 234, 0.15),
      0 0 0 1px rgba(31, 114, 234, 0.1);
    min-width: 280px;
    animation: slideInUp 0.3s ease-out;

    .spinner {
      width: 60px;
      height: 60px;
      border: 4px solid #f3f7ff;
      border-top: 4px solid #409EFF;
      border-radius: 50%;
      animation: spin 1.2s linear infinite;
      margin: 0 auto 20px;
      position: relative;

      &::after {
        content: '';
        position: absolute;
        top: -4px;
        left: -4px;
        right: -4px;
        bottom: -4px;
        border: 4px solid transparent;
        border-top: 4px solid #66b1ff;
        border-radius: 50%;
        animation: spin 0.8s linear infinite reverse;
      }
    }

    .animation-text {
      font-size: 20px;
      font-weight: 600;
      color: #409EFF;
      margin-bottom: 8px;
      background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
    }

    .animation-subtext {
      font-size: 14px;
      color: #666;
      opacity: 0.8;
    }
  }
}

.content-body {
  padding-bottom: 20px; // 与底部操作栏保持间距
}

.content-row {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}

.form-pane {
  background: #fff;
  border-radius: 16px;
  min-height: 500px;
  padding: 20px;
  margin-bottom: 20px;
  flex: 1;
  min-width: 300px;
}

.search-btn {
  background: #409EFF;
  border-color: #409EFF;
  color: #fff;
  font-weight: 600;
  padding: 12px 24px;
  border-radius: 6px;
  box-shadow: 0px 4px 12px 0px rgba(64, 158, 255, 0.4);
  letter-spacing: 0.5px;
  font-size: 14px;
  transition: all 0.3s ease;

  &:hover {
    background: #66b1ff;
    border-color: #66b1ff;
    box-shadow: 0px 6px 16px 0px rgba(64, 158, 255, 0.5);
    transform: translateY(-1px);
  }

  &.is-loading {
    opacity: 0.8;
    pointer-events: none;
  }
}

.reset-btn {
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
  font-weight: 600;
  padding: 12px 24px;
  border-radius: 6px;
  box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.1);
  letter-spacing: 0.5px;
  font-size: 14px;
  transition: all 0.3s ease;

  &:hover {
    background: #f5f7fa;
    border-color: #c0c4cc;
    color: #409EFF;
    box-shadow: 0px 6px 16px 0px rgba(0, 0, 0, 0.15);
    transform: translateY(-1px);
  }
}

// 禁止点击样式
.no-pointer-events {
  pointer-events: none;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
</style>
