<template>
  <div class="sdgsat1-case-detail">
    <!-- 面包屑导航 -->
    <div class="breadcrumb-container">
      <div class="breadcrumb-wrapper">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item><a href="/portal">首页</a></el-breadcrumb-item>
          <el-breadcrumb-item
            ><a href="/portal/sdgsat1">SDGSAT-1专区</a></el-breadcrumb-item
          >
          <el-breadcrumb-item
            ><a href="/portal/sdgsat1?menu=cases"
              >数据开发与应用</a
            ></el-breadcrumb-item
          >
          <el-breadcrumb-item>案例详情</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button
          class="back-btn"
          icon="el-icon-arrow-left"
          size="small"
          @click="goBack"
          >返回</el-button
        >
      </div>
    </div>

    <div class="page-container">
      <div class="detail-card" v-loading="loading">
        <div class="detail-header">
          <h1 class="detail-title">{{ caseDetail.title }}</h1>
        </div>

        <div class="detail-body">
          <div class="detail-thumbnail" v-if="caseDetail.thumbnail">
            <img :src="caseDetail.thumbnail" :alt="caseDetail.title" />
          </div>

          <div class="detail-content">
            <div class="content-section" v-if="caseDetail.content">
              <div class="content-text" v-html="caseDetail.content"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCaseDetail } from "@/api/portal/sdgsat1";

export default {
  name: "SDGSAT1CaseDetail",
  data() {
    return {
      loading: true,
      debugInfo: "",
      caseDetail: {
        title: "",
        content: "",
      },
    };
  },
  created() {
    this.getDetail();
  },
  methods: {
    async getDetail() {
      this.loading = true;
      try {
        const { id } = this.$route.query;
        if (!id) {
          this.$message.error("缺少案例ID");
          this.debugInfo = "缺少案例ID: " + JSON.stringify(this.$route.query);
          this.loading = false;
          return;
        }

        this.debugInfo = "获取案例详情，ID: " + id;
        console.log("获取案例详情，ID:", id);

        const response = await getCaseDetail(id);
        this.debugInfo += "\nAPI响应: " + JSON.stringify(response);
        console.log("API响应:", response);

        if (response.code === 200 && response.data) {
          this.caseDetail = response.data;
          this.debugInfo +=
            "\n案例详情数据: " + JSON.stringify(this.caseDetail);
          console.log("案例详情数据:", this.caseDetail);
        } else {
          this.debugInfo += "\nAPI响应格式错误: " + JSON.stringify(response);
          console.error("API响应格式错误:", response);
          this.$message.error("获取案例详情失败：响应格式错误");
        }
      } catch (error) {
        this.debugInfo += "\n获取案例详情失败: " + error.message;
        console.error("获取案例详情失败:", error);
        this.$message.error("获取案例详情失败：网络错误");
      } finally {
        this.loading = false;
      }
    },
    goBack() {
      this.$router.back();
    },
  },
};
</script>

<style lang="scss" scoped>
.sdgsat1-case-detail {
  width: 100%;
  min-height: calc(100vh - 200px);
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

/* 面包屑导航 */
.breadcrumb-container {
  padding: 20px 0;
}

.breadcrumb-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .back-btn {
    margin-left: 20px;
  }
}

.page-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px;
}

.detail-card {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.detail-header {
  padding: 40px 40px 30px;
  border-bottom: 1px solid #f0f0f0;

  .detail-title {
    font-size: 32px;
    font-weight: 700;
    color: #1e3c72;
    margin: 0;
    line-height: 1.4;
  }
}

.detail-body {
  padding: 40px;
}

.detail-thumbnail {
  width: 100%;
  max-height: 500px;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 40px;
  background: #f5f7fa;

  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
}

.detail-content {
  .content-section {
    margin-bottom: 40px;

    &:last-child {
      margin-bottom: 0;
    }

    .content-text {
      font-size: 15px;
      color: #333333;
      line-height: 1.8;
      text-align: justify;

      ::v-deep {
        p {
          margin-bottom: 15px;

          &:last-child {
            margin-bottom: 0;
          }
        }

        img {
          max-width: 100%;
          height: auto;
          border-radius: 8px;
          margin: 20px 0;
        }

        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
          color: #1e3c72;
          margin-top: 30px;
          margin-bottom: 20px;
        }

        h1 {
          font-size: 24px;
          font-weight: 600;
        }

        h2 {
          font-size: 20px;
          font-weight: 600;
        }

        h3 {
          font-size: 18px;
          font-weight: 500;
        }

        ul,
        ol {
          margin-left: 24px;
          margin-bottom: 20px;
        }

        li {
          margin-bottom: 10px;
        }

        a {
          color: #1e3c72;
          text-decoration: none;

          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .detail-header {
    padding: 30px 20px;

    .detail-title {
      font-size: 24px;
    }
  }

  .detail-body {
    padding: 20px;
  }

  .detail-thumbnail {
    max-height: 300px;
  }
}
</style>