<template>
  <div class="application-container">
    <div class="application-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 === 'sdgCases' }"
              @click="handleMenuClick('sdgCases')"
            >
              SDG应用案例
            </div>
            <div 
              class="menu-item"
              :class="{ active: currentMenu === 'regionDemos' }"
              @click="handleMenuClick('regionDemos')"
            >
              区域示范
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧内容区域 -->
      <div class="right-content">
        <div class="content-card" v-loading="loading">
          <div class="content-header">
            <span class="title">{{ currentTitle }}</span>
          </div>
          <div class="content-body">
            <!-- 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-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 v-if="currentMenu === 'regionDemos'" class="demos-content">
              <div v-if="demoList.length === 0" class="empty-container">
                <i class="el-icon-s-flag"></i>
                <p>暂无区域示范数据</p>
              </div>
              <div v-else class="demo-list">
                <div class="demo-item" v-for="(item, index) in demoList" :key="item.id || index">
                  <div class="demo-status">
                    <el-tag :type="item.statusType">{{ item.status }}</el-tag>
                  </div>
                  <div class="demo-content">
                    <h3>{{ item.title }}</h3>
                    <p>{{ item.desc }}</p>
                    <div class="demo-meta">
                      <span><i class="el-icon-office-building"></i> {{ item.unit }}</span>
                      <span><i class="el-icon-time"></i> {{ item.period }}</span>
                      <span><i class="el-icon-map-location"></i> {{ item.scope }}</span>
                    </div>
                  </div>
                  <div class="demo-progress">
                    <div class="progress-label">完成进度</div>
                    <el-progress :percentage="item.progress" :color="progressColor"></el-progress>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getApplicationList, getDemoList } from '@/api/portal/application'

export default {
  name: 'Application',
  data() {
    return {
      loading: true,
      currentMenu: 'sdgCases',
      // 应用案例数据（来自管理后台）
      caseList: [],
      // 区域示范数据
      demoList: [],
      // 进度条颜色
      progressColor: [
        { color: '#f56c6c', percentage: 20 },
        { color: '#e6a23c', percentage: 40 },
        { color: '#5cb87a', percentage: 60 },
        { color: '#1989fa', percentage: 80 },
        { color: '#6f7ad3', percentage: 100 }
      ]
    }
  },
  computed: {
    currentTitle() {
      return this.currentMenu === 'sdgCases' ? 'SDG应用案例' : '区域示范'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleMenuClick(menu) {
      this.currentMenu = menu
    },
    // 获取数据
    fetchData() {
      this.getCaseList()
      this.getDemoList()
    },
    // 获取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()
      })
    },
    // 获取区域示范列表
    getDemoList() {
      this.loading = true
      const query = {
        pageNum: 1,
        pageSize: 100,
        status: '0'
      }
      getDemoList(query).then(response => {
        if (response.code === 200 && response.rows) {
          // 转换后端数据为前端展示格式
          this.demoList = response.rows.map((item, index) => ({
            id: item.id,
            title: item.caseName,
            desc: item.remark || '暂无描述',
            unit: item.applicationUnit || '未知单位',
            period: item.publishTime ? this.parseTime(item.publishTime, '{y}-{m}') : '2024-2025',
            scope: item.applicationField || '区域',
            status: '进行中',
            statusType: 'warning',
            progress: 30 + (index * 15) % 70 // 模拟进度
          }))
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
        // 如果接口失败，使用默认静态数据
        this.demoList = this.getDefaultDemoList()
      })
    },
    // 根据应用领域获取标签类型
    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' 
        }
      ]
    },
    // 默认示范数据
    getDefaultDemoList() {
      return [
        { 
          title: '气候变化监测示范项目', 
          desc: '在某省建设气候变化监测示范项目，形成可复制推广的经验，为其他地区提供参考。', 
          unit: '某省生态环境厅', 
          period: '2023-2025', 
          scope: '全省', 
          status: '进行中', 
          statusType: 'warning', 
          progress: 65 
        },
        { 
          title: '生态环境治理示范项目', 
          desc: '在某市开展生态环境治理示范项目，探索生态治理新模式，取得显著成效。', 
          unit: '某市生态环境局', 
          period: '2023-2024', 
          scope: '全市', 
          status: '即将完成', 
          statusType: 'success', 
          progress: 90 
        }
      ]
    }
  }
}
</script>

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

.application-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;
  }

  .content-header {
    padding: 30px 30px 0;

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

  .content-body {
    padding: 30px;
    line-height: 1.8;
    color: #333333;
    overflow: hidden;
    word-wrap: break-word;
    overflow-wrap: break-word;

    :deep(h1),
    :deep(h2),
    :deep(h3),
    :deep(h4),
    :deep(h5),
    :deep(h6) {
      color: #1e3c72;
      margin-top: 24px;
      margin-bottom: 16px;
    }

    :deep(p) {
      margin-bottom: 16px;
      max-width: 100%;
      overflow: hidden;
    }

    :deep(img) {
      max-width: 100% !important;
      width: 100% !important;
      height: auto !important;
      border-radius: 4px;
      margin: 16px 0;
      display: block;
      box-sizing: border-box;
    }

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

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

    :deep(ul),
    :deep(ol) {
      margin-left: 24px;
      margin-bottom: 16px;
    }

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

/* 应用案例样式 */
.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-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;
          }
        }
      }
    }
  }
}

/* 示范项目样式 */
.demos-content {
  .demo-list {
    display: flex;
    flex-direction: column;
    gap: 20px;

    .demo-item {
      display: flex;
      align-items: flex-start;
      gap: 20px;
      background: #ffffff;
      padding: 24px;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      transition: all 0.3s ease;

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

      .demo-status {
        flex-shrink: 0;
      }

      .demo-content {
        flex: 1;

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

        p {
          font-size: 14px;
          color: #666666;
          line-height: 1.6;
          margin: 0 0 15px 0;
        }

        .demo-meta {
          display: flex;
          gap: 25px;
          font-size: 13px;
          color: #999999;

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

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

      .demo-progress {
        width: 180px;
        flex-shrink: 0;

        .progress-label {
          font-size: 13px;
          color: #666666;
          margin-bottom: 10px;
        }
      }
    }
  }
}

/* 加载和空状态样式 */
.loading-container,
.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  color: #999999;

  i {
    font-size: 48px;
    margin-bottom: 16px;
  }

  p {
    font-size: 14px;
  }
}

.loading-container {
  i {
    color: #2a5298;
  }
}

/* 响应式设计 */
@media (max-width: 992px) {
  .application-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .left-sidebar {
    order: 2;

    .sidebar-menu {
      display: flex;
      overflow-x: auto;
      white-space: nowrap;

      .menu-item {
        border-left: none;
        border-bottom: 3px solid transparent;
        display: inline-block;
        margin-right: 8px;
        border-radius: 4px 4px 0 0;

        &.active {
          border-left: none;
          border-bottom-color: #1e3c72;
        }
      }
    }
  }

  .right-content {
    order: 1;
  }

  .demo-item {
    flex-direction: column;

    .demo-progress {
      width: 100%;
    }
  }
}

@media (max-width: 768px) {
  .application-content {
    padding: 0 20px;
  }

  .right-content {
    .content-card {
      min-height: 500px;
    }

    .content-header {
      padding: 20px 20px 0;
    }

    .content-body {
      padding: 20px;
    }
  }

  .left-sidebar {
    .sidebar-header {
      padding: 20px 20px;

      h2 {
        font-size: 20px;
      }
    }

    .sidebar-menu {
      .menu-item {
        padding: 12px 20px;
        font-size: 14px;
      }
    }
  }

  .case-item {
    padding: 20px;
  }

  .demo-item {
    padding: 20px;
  }
}
</style>

<style scoped>
/* 确保图片宽度自适应 */
.content-body >>> img {
  max-width: 100% !important;
  width: 100% !important;
  height: auto !important;
  border-radius: 4px;
  margin: 16px 0;
  display: block;
  box-sizing: border-box;
}

/* 确保富文本编辑器生成的其他元素也不会溢出 */
.content-body >>> .ql-video {
  max-width: 100% !important;
  width: 100% !important;
  height: auto !important;
}
</style>
