<template>
  <div class="achievement-container">
    <div class="achievement-content">
      <!-- 左侧导航卡片 -->
      <div class="left-sidebar">
        <div class="sidebar-card">
          <div class="sidebar-header">
            <span class="title">科研成果</span>
          </div>
          <div class="sidebar-decorator">
            <div
              class="decorator-segment"
              style="background-color: #4b9f37"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #c51a2d"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #e37221"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #0298db"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #f8c72e"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #d0103a"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #028542"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #bf8b2e"
            ></div>
            <div
              class="decorator-segment"
              style="background-color: #0b97d9"
            ></div>
          </div>
          <div class="sidebar-menu">
            <div
              class="menu-item"
              :class="{ active: currentMenu === 'overview' }"
              @click="handleMenuClick('overview')"
            >
              成果总体描述
            </div>
            <div
              class="menu-item"
              :class="{ active: currentMenu === 'sdgsat1' }"
              @click="handleMenuClick('sdgsat1')"
            >
              科学成果
            </div>
            <div
              class="menu-item"
              :class="{ active: currentMenu === 'sdgCases' }"
              @click="handleMenuClick('sdgCases')"
            >
              SDG应用案例
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧内容区域 -->
      <div class="right-content">
        <div class="content-card" v-loading="loading">
          <div class="content-header" v-if="currentMenu == 'overview'">
            <span class="title">{{ currentTitle }}</span>
          </div>
          <div class="rich-text-rendering">
            <!-- 成果总体描述内容 -->
            <div v-if="currentMenu === 'overview'" class="overview-content">
              <div
                class="rich-text"
                v-html="overviewData.content || '<p>暂无内容</p>'"
              ></div>
            </div>

            <!-- 科学成果内容 -->
            <div v-if="currentMenu === 'sdgsat1'" class="sdgsat1-content">
              <section class="sdgsat1-achievements-list">
                <div class="achievements-list">
                  <div
                    class="achievement-card"
                    v-for="(item, index) in sdgsat1AchievementList"
                    :key="index"
                  >
                    <div class="achievement-header">
                      <h3 class="achievement-title">
                        {{ item.achievementName }}
                      </h3>
                    </div>
                    <div class="achievement-body">
                      <div class="achievement-left">
                        <div
                          class="achievement-introduction"
                          v-if="item.introduction"
                        >
                          <div class="rich-text" v-html="item.introduction"></div>
                        </div>
                        <div class="achievement-actions">
                          <el-button
                            type="primary"
                            size="small"
                            @click="viewDetail(item.id)"
                          >
                            查看详情
                          </el-button>
                        </div>
                      </div>
                      <div class="achievement-right">
                        <div
                          class="achievement-thumbnail"
                          v-if="item.thumbnail"
                        >
                          <img
                            :src="item.thumbnail"
                            :alt="item.achievementName"
                          />
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

                <div class="pagination-wrapper">
                  <el-pagination
                    background
                    layout="prev, pager, next"
                    :total="sdgsat1Total"
                    :page-size="sdgsat1PageSize"
                    :current-page="sdgsat1PageNum"
                    @current-change="handleSdgsat1PageChange"
                  />
                </div>
              </section>
            </div>

            <!-- SDG应用案例内容 -->
            <div v-if="currentMenu === 'sdgCases'" class="cases-content">
              <div v-if="caseList.length === 0" class="empty-container">
                <i class="el-icon-folder-opened"></i>
                <p>暂无应用案例数据</p>
              </div>
              <div v-else class="cases-list">
                <div class="case-item" v-for="(item, index) in caseList" :key="item.id || index">
                  <div class="case-header">
                    <h3 class="case-title">{{ item.title }}</h3>
                    <div class="case-tag">
                      <el-tag size="small" :type="item.tagType">{{ item.tag }}</el-tag>
                    </div>
                  </div>
                  <div class="case-thumbnail" v-if="item.thumbnail">
                    <img :src="item.thumbnail" :alt="item.title" />
                  </div>
                  <div class="case-intro" v-html="item.desc"></div>
                  <div class="case-meta">
                    <span><i class="el-icon-location"></i> {{ item.location }}</span>
                    <span><i class="el-icon-date"></i> {{ item.date }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getSdgsat1AchievementList,
  getAchievementOverview,
} from "@/api/portal/achievement";
import { getApplicationList } from "@/api/portal/application";

export default {
  name: "Achievement",
  data() {
    return {
      loading: true,
      currentMenu: "overview",
      overviewData: {
        title: "",
        content: "",
      },
      selectedField: "",
      selectedType: "",
      sdgsat1AchievementList: [],
      sdgsat1Total: 0,
      sdgsat1PageSize: 10,
      sdgsat1PageNum: 1,
      // SDG应用案例数据
      caseList: [],
    };
  },
  computed: {
    currentTitle() {
      const titleMap = {
        overview: "成果总体描述",
        sdgsat1: "科学成果",
        sdgCases: "SDG应用案例",
      };
      return titleMap[this.currentMenu] || "";
    },
  },
  mounted() {
    this.fetchAchievementOverview();
  },
  methods: {
    async fetchAchievementOverview() {
      this.loading = true;
      try {
        const response = await getAchievementOverview();
        if (response.code === 200 && response.data) {
          this.overviewData = response.data;
        }
      } catch (error) {
        console.error("获取成果总体描述失败:", error);
      } finally {
        this.loading = false;
      }
    },
    async fetchSdgsat1Achievements() {
      this.loading = true;
      try {
        const response = await getSdgsat1AchievementList({
          pageNum: this.sdgsat1PageNum,
          pageSize: this.sdgsat1PageSize,
        });
        if (response.code === 200) {
          this.sdgsat1AchievementList = response.rows || [];
          this.sdgsat1Total = response.total || 0;
        }
      } catch (error) {
        console.error("获取SDGSAT-1成果列表失败:", error);
        this.$message.error("获取成果列表失败");
      } finally {
        this.loading = false;
      }
    },
    handleSdgsat1PageChange(page) {
      this.sdgsat1PageNum = page;
      this.fetchSdgsat1Achievements();
    },
    viewDetail(id) {
      this.$router.push({
        path: "/portal/achievement/detail",
        query: { id },
      });
    },
    handleMenuClick(menu) {
      this.currentMenu = menu;
      if (menu === "overview") {
        this.fetchAchievementOverview();
      } else if (menu === "sdgsat1") {
        this.fetchSdgsat1Achievements();
      } else if (menu === "sdgCases") {
        this.getCaseList();
      }
    },
    // 获取SDG应用案例列表
    getCaseList() {
      this.loading = true;
      const query = {
        pageNum: 1,
        pageSize: 100,
        status: "0", // 只查询正常状态的
      };
      getApplicationList(query)
        .then((response) => {
          if (response.code === 200 && response.rows) {
            // 转换后端数据为前端展示格式
            this.caseList = response.rows.map((item) => ({
              id: item.id,
              title: item.caseName,
              desc: item.introduction || item.remark || "暂无描述",
              location: item.applicationUnit || "未知",
              date: item.publishTime
                ? this.parseTime(item.publishTime, "{y}-{m}")
                : "",
              tag: item.applicationField || "应用案例",
              tagType: this.getTagType(item.applicationField),
              thumbnail: item.thumbnail,
            }));
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          // 如果接口失败，使用默认静态数据
          this.caseList = this.getDefaultCaseList();
        });
    },
    // 根据应用领域获取标签类型
    getTagType(field) {
      if (!field) return "info";
      const typeMap = {
        气候变化: "primary",
        生态环境: "success",
        农业: "warning",
        健康: "danger",
        城市: "info",
        水资源: "primary",
      };
      for (let key in typeMap) {
        if (field.includes(key)) {
          return typeMap[key];
        }
      }
      return "info";
    },
    // 默认案例数据
    getDefaultCaseList() {
      return [
        {
          title: "气候变化监测系统应用",
          desc: "在某省部署气候变化监测系统，实现气候变化指标的实时监测和预警，为应对气候变化提供科学依据。",
          location: "某省",
          date: "2023-06",
          tag: "气候变化",
          tagType: "primary",
        },
        {
          title: "生态环境评估平台应用",
          desc: "为某市提供生态环境评估服务，支持生态环境治理决策，有效改善区域生态环境质量。",
          location: "某市",
          date: "2023-08",
          tag: "生态环境",
          tagType: "success",
        },
        {
          title: "农业产量预测系统应用",
          desc: "在多个农业大县推广农业产量预测系统，提高农业生产效率，助力粮食安全保障。",
          location: "多县",
          date: "2023-09",
          tag: "农业发展",
          tagType: "warning",
        },
      ];
    },
  },
};
</script>

<style lang="scss" scoped>
.achievement-container {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 40px 0;
}

.achievement-content {
  max-width: 1390px;
  margin: 0 auto;
  padding: 0 30px;
  display: grid;
  grid-template-columns: 300px 1060px;
  gap: 30px;
  align-items: start;
}

/* 左侧导航卡片 */
.left-sidebar {
  .sidebar-card {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    overflow: hidden;
  }

  .sidebar-header {
    background: #1ea8c5;
    // padding: 30px 24px;
    height: 58px;
    display: flex;
    align-items: center;
    // justify-content: center;
    padding-left: 19px;

    .title {
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: bold;
      font-size: 18px;
      color: #ffffff;
      line-height: 18px;
      text-align: left;
      font-style: normal;
      text-transform: none;
    }
  }

  .sidebar-decorator {
    width: 100%;
    height: 6px;
    display: flex;
    overflow: hidden;

    .decorator-segment {
      width: 80px;
      height: 100%;

      &:first-child {
        margin-left: -40px;
      }

      &:last-child {
        margin-right: -40px;
      }
    }
  }

  .sidebar-menu {
    padding: 16px 0;

    .menu-item {
      padding: 16px 24px;
      cursor: pointer;
      transition: all 0.3s ease;
      font-size: 16px;
      color: #666666;
      border-left: 3px solid transparent;

      &:hover {
        background-color: #f5f7fa;
        color: #1e3c72;
      }

      &.active {
        background-color: #f0f2f5;
        color: #1e3c72;
        font-weight: 600;
        border-left-color: #1e3c72;
      }
    }
  }
}

/* 右侧内容区域 */
.right-content {
  .content-card {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    overflow: hidden;
    min-height: 600px;
  }
}

/* SDGSAT-1 科学成果样式 */
.sdgsat1-content {
  .sdgsat1-overview {
    margin-bottom: 40px;

    h3 {
      font-size: 20px;
      font-weight: 600;
      color: #1e3c72;
      margin: 0 0 16px 0;
    }

    .overview-desc {
      font-size: 15px;
      color: #666666;
      line-height: 1.8;
      margin: 0;
    }
  }
}

/* SDG应用案例样式 */
.cases-content {
  .cases-list {
    display: flex;
    flex-direction: column;
    gap: 20px;

    .case-item {
      background: #ffffff;
      border-radius: 8px;
      padding: 24px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      transition: all 0.3s ease;

      &:hover {
        transform: translateX(5px);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
      }

      .case-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 15px;
        padding-bottom: 15px;
        border-bottom: 1px solid #e8e8e8;

        .case-title {
          font-size: 18px;
          font-weight: 600;
          color: #1e3c72;
          margin: 0;
          flex: 1;
        }

        .case-tag {
          flex-shrink: 0;
          margin-left: 15px;
        }
      }

      .case-thumbnail {
        display: flex;
        justify-content: center;
        margin-bottom: 15px;

        img {
          max-width: 100%;
          max-height: 300px;
          width: auto;
          height: auto;
          border-radius: 8px;
          object-fit: contain;
        }
      }

      .case-intro {
        font-size: 15px;
        line-height: 1.8;
        color: #333333;
        margin-bottom: 15px;

        :deep(p) {
          margin: 0 0 12px 0;
          &:last-child {
            margin-bottom: 0;
          }
        }

        :deep(img) {
          max-width: 100%;
          height: auto;
          border-radius: 6px;
          margin: 10px 0;
        }

        :deep(h1), :deep(h2), :deep(h3), :deep(h4) {
          color: #1e3c72;
          margin: 16px 0 10px 0;
        }

        :deep(ul), :deep(ol) {
          margin: 10px 0;
          padding-left: 20px;
        }

        :deep(li) {
          margin-bottom: 6px;
        }

        :deep(a) {
          color: #1e3c72;
          text-decoration: none;
          &:hover {
            text-decoration: underline;
          }
        }
      }

      .case-meta {
        display: flex;
        gap: 20px;
        font-size: 13px;
        color: #999999;
        padding-top: 15px;
        border-top: 1px dashed #e8e8e8;

        span {
          display: flex;
          align-items: center;
          gap: 5px;

          i {
            font-size: 14px;
          }
        }
      }
    }
  }
}

.sdgsat1-achievements-list {
  .achievements-list {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 30px;
  }

  .achievement-card {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    overflow: hidden;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-3px);
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    }
  }

  .achievement-header {
    padding: 20px 24px;
    border-bottom: 1px solid #f0f0f0;

    .achievement-title {
      font-size: 18px;
      font-weight: 600;
      color: #1e3c72;
      margin: 0;
      line-height: 1.4;
    }
  }

  .achievement-body {
    display: flex;
    padding: 24px;
    gap: 24px;
  }

  .achievement-left {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .achievement-introduction {
    flex: 1;

    p {
      font-size: 14px;
      color: #666666;
      line-height: 1.8;
      margin: 0;
      text-align: justify;
    }
  }

  .achievement-actions {
    display: flex;
    gap: 12px;
  }

  .achievement-right {
    width: 260px;
    flex-shrink: 0;
  }

  .achievement-thumbnail {
    width: 100%;
    height: 180px;
    border-radius: 6px;
    overflow: hidden;
    background: #f5f7fa;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s ease;

      &:hover {
        transform: scale(1.05);
      }
    }
  }

  .pagination-wrapper {
    display: flex;
    justify-content: center;
    padding: 20px 0;
  }
}
</style>
