<template>
  <div class="sjsc-page">
    <div>
      <!-- 面包屑导航 -->
      <!-- <BreadcrumbNav currentPage="碳数据市场" /> -->
  
      <!-- 二级菜单 -->
      <div class="secondary-nav">
        <div class="secondary-nav-content">
          <div class="nav-tabs">
            <button v-for="tab in navTabs" :key="tab.path" :class="['nav-tab', { active: isActiveTab(tab.path) }]"
              @click="goToTab(tab.path)">
              {{ tab.label }}
            </button>
          </div>
          <!-- <button class="publish-btn" @click="handlePublish">免费发布数据</button> -->
        </div>
      </div>
  
      <!-- 页面主体 -->
      <div class="page-body">
        <!-- 左侧筛选栏 -->
        <div class="sidebar">
          <div class="filter-toggle" @click="filterCollapsed = !filterCollapsed">
            <span class="toggle-text">筛选</span>
            <span class="toggle-icon">{{ filterCollapsed ? '▼' : '▲' }}</span>
          </div>
          <div :class="['sidebar-content', { collapsed: filterCollapsed }]">
            <div class="sidebar-title">数据类型</div>
            <div class="filter-list">
              <div v-for="item in dataTypeList" :key="item.value" class="filter-item"
                :class="{ active: selectedType === item.value }" @click="handleTypeChange(item.value)">
                <span class="check-icon">{{ selectedType === item.value ? '✓' : '' }}</span>
                {{ item.label }}
              </div>
            </div>
          </div>
        </div>
  
        <!-- 右侧数据库卡片列表 -->
        <div class="card-list">
          <div class="service-grid">
            <div
              v-for="card in filteredCards"
              :key="card.id"
              class="service-card"
            >
              <div class="card-header">
                <div class="card-title-box">
                  <div class="card-title-text">
                    <div class="card-title-row">
                      <div class="card-title-main">{{ card.name }}</div>
                    </div>
                    <div class="card-title-sub">
                      <div class="card-tags">
                        <span
                          v-for="tag in card.tags"
                          :key="tag"
                          class="tag"
                          :class="getTagClass(tag)"
                        >
                          {{ tag }}
                        </span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="card-content">
                <p class="card-desc">{{ card.description }}</p>
              </div>
              <div class="card-footer">
                <div class="card-price-info">
                  <span class="price-value" :class="card.price === '免费' ? 'free' : 'paid'">
                    {{ card.price }}
                  </span>
                </div>
                <div class="card-actions">
                  <span @click="goToDataList(card.id)">查看数据库</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <Footer />
  </div>
</template>

<script>
import NewNav from '@/pages/index/components/new-nav/index.vue';
import Footer from '@/pages/index/components/footer/index.vue';
import BreadcrumbNav from '@/pages/index/components/breadcrumb/index.vue';
import gxzxApi from '@/pages/index/api/gxzx/index.js';

export default {
  name: 'SjscPage',
  components: {
    NewNav,
    Footer,
    BreadcrumbNav,
  },
  data() {
    return {
      navTabs: [
        { label: '碳服务市场', path: '/tfwsc' },
        { label: '碳需求市场', path: '/txqsc' },
        { label: '碳金融市场', path: '/tjrsc' },
        { label: '碳数据市场', path: '/tsjsc' },
      ],
      selectedType: 'all',
      dataTypeList: [
        { label: '全部', value: 'all' },
        { label: '公共数据', value: 'public' },
        { label: '因子库', value: 'factor' },
        { label: '社会性数据', value: 'social' }
      ],
      cardList: [],
      pagination: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      },
      // 移动端筛选折叠状态
      filterCollapsed: true,
    };
  },
  computed: {
    filteredCards() {
      if (this.selectedType === 'all') {
        return this.cardList;
      }
      return this.cardList.filter(card => card.sjlxDm === this.selectedType);
    }
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      const params = {
        pageNo: this.pagination.pageNo,
        pageSize: this.pagination.pageSize,
        sjlxDm: this.selectedType === 'all' ? '' : this.selectedType,
        nr: ''
      };
      gxzxApi.sjscList(params).then(res => {
        if (res && res.data && res.data.records) {
          this.cardList = res.data.records.map(item => ({
            id: item.uuid,
            name: item.sjmc,
            description: item.sjms,
            tags: item.sjlxMc ? [item.sjlxMc] : [],
            price: '免费',
            llcs: item.llcs,
            xzcs: item.xzcs
          }));
          this.pagination.total = res.data.total;
        }
      });
    },
    handleTypeChange(value) {
      this.selectedType = value;
      this.pagination.pageNo = 1;
      this.loadData();
    },
    isActiveTab(path) {
      return this.$route.path === path;
    },
    goToTab(path) {
      this.$router.push(path);
    },
    handlePublish() {
      this.$router.push({ path: '/sjscfb' });
    },
    goToDataList(id) {
      this.$router.push({ path: '/tsjlbc', query: { id } });
    },
    getTagClass(tag) {
      const classMap = {
        '公共数据': 'tag-green',
        '社会性数据': 'tag-blue',
        '商业数据': 'tag-gray',
        '公益数据': 'tag-green',
        '因子库': 'tag-orange'
      };
      return classMap[tag] || 'tag-gray';
    }
  }
};
</script>

<style lang="less" scoped>
.sjsc-page {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 64px);
  background: #f5f5f5;
  justify-content: space-between;
}

// 页面主体
.page-body {
  display: flex;
  gap: 20px;
  align-items: stretch;
  max-width: 1400px;
  padding: 20px;
  margin: 0 auto;
}

// 二级菜单
.secondary-nav {
  border-bottom: none;
}

.secondary-nav-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1400px;
  padding: 20px 20px 0;
  margin: 0 auto;
}

.nav-tabs {
  display: flex;
  gap: 40px;
  width: 596px;
  height: 42px;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-tab {
  position: relative;
  min-width: max-content;
  height: 42px;
  padding: 8px 16px;
  font-size: 18px;
  font-weight: 500;
  color: #003B1A;
  cursor: pointer;
  background: transparent;
  border-radius: 32px;
  transition: all 0.3s;

  &:hover {
    color: #009a29;
  }

  &.active {
    background: #8CFFCE;
    box-shadow: inset 0 0 0 1px #00B96B;
  }
}

.publish-btn {
  width: 220px;
  height: 42px;
  line-height: 26px;
  padding: 8px 16px;
  font-size: 18px;
  font-weight: 400;
  color: #fff;
  cursor: pointer;
  background: linear-gradient(135deg, #009a29 0%, #48C666 100%);
  border: none;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 154, 41, 0.25);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 154, 41, 0.35);
  }

  &:active {
    transform: translateY(0);
  }
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.list-count {
  font-size: 14px;
  font-weight: 400;
  color: #6B8575;
}

// 左侧筛选栏
.sidebar {
  position: sticky;
  // top: 104px;
  width: 220px;
  min-width: 220px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    min-width: auto;
  }
}

.sidebar-content {
  padding: 20px;
}

// 移动端筛选栏折叠
.filter-toggle {
  display: none;
}

@media (max-width: 768px) {
  .filter-toggle {
    display: flex;
    padding: 12px;
    margin-bottom: 8px;
    cursor: pointer;
    background: #fff;
    border-radius: 8px;
    justify-content: space-between;
    align-items: center;

    .toggle-text {
      font-size: 14px;
      font-weight: 600;
      color: #333;
    }

    .toggle-icon {
      font-size: 18px;
      color: #666;
      transition: transform 0.3s;
    }

    &.collapsed .toggle-icon {
      transform: rotate(-90deg);
    }
  }

  .sidebar-content {
    max-height: 1000px;
    padding: 20px;
    overflow: hidden;
    transition: max-height 0.3s ease, padding 0.3s ease;
  }

  .sidebar-content.collapsed {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
  }
}

.sidebar-title {
  display: flex;
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  align-items: center;
  gap: 6px;

  &::before {
    width: 4px;
    height: 14px;
    background: linear-gradient(180deg, #009a29, #48C666);
    border-radius: 2px;
    content: '';
  }
}

.filter-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.filter-item {
  display: flex;
  padding: 10px 12px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  background: #f5f5f5;
  border-radius: 6px;
  transition: all 0.3s;
  align-items: center;
  gap: 8px;

  .check-icon {
    display: flex;
    width: 18px;
    height: 18px;
    font-size: 12px;
    color: #fff;
    background: #ccc;
    border-radius: 4px;
    transition: all 0.3s;
    align-items: center;
    justify-content: center;
  }

  &:hover {
    color: #009a29;
    background: #e8f5e9;
  }

  &.active {
    color: #009a29;
    background: #e8f5e9;

    .check-icon {
      background: #009a29;
    }
  }
}

// 右侧卡片列表
.card-list {
  flex: 1;
  min-width: 0;
}

// 服务卡片网格
.service-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
}

.service-card {
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;

  &.highlight-card {
    animation: highlight-pulse 3s ease-out;
  }
}

.card-header {
  position: relative;
  padding: 16px;
  height: 70px;

  &::after {
    position: absolute;
    right: 16px;
    bottom: -6px;
    left: 16px;
    height: 1px;
    background: #E8F0EC;
    content: '';
  }
}

.card-title-box {
  display: flex;
  justify-content: space-between;
}

.card-title-text {
  flex: 1;
  min-width: 0;
}

.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.card-title-main {
  flex: 1;
  overflow: hidden;
  font-size: 16px;
  font-weight: 500;
  line-height: 24px;
  color: #003B1A;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-title-sub {
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  padding: 2px 8px;
  font-size: 13px;
  font-weight: 400;
  color: #00B96B;
  background: #EEFAE2;
  border-radius: 4px;
}

.tag-green {
  color: #2e7d32;
  background: #e8f5e9;
}

.tag-blue {
  color: #1565c0;
  background: #e3f2fd;
}

.tag-gray {
  color: #666;
  background: #f5f5f5;
}

.tag-orange {
  color: #e65100;
  background: #fff3e0;
}

.card-content {
  padding: 16px;
}

.card-desc {
  display: block;
  height: 70px;
  overflow: hidden;
  font-size: 14px;
  line-height: 1.6;
  color: #666;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
}

.card-footer {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  height: 64px;

  &::before {
    position: absolute;
    top: 0;
    right: 16px;
    left: 16px;
    height: 1px;
    background: #E8F0EC;
    content: '';
  }
}

.card-price-info {
  display: flex;
  align-items: baseline;
}

.card-actions {
  display: flex;
  height: 32px;
  padding: 6px 12px;
  border-radius: 4px;
  border: 1px solid #00b96b;
  color: #00b96b;
  font-size: 14px;
  font-weight: 400;

  &:hover {
    background: rgba(0, 154, 41, 0.1);
    cursor: pointer;
  }
}

.price-value {
  font-size: 20px;
  font-weight: 600;
  color: #FF4D4F;

  &.free {
    color: #2e7d32;
  }

  &.paid {
    color: #e65100;
  }
}

@media (max-width: 768px) {
  .page-body {
    flex-direction: column;
    padding: 12px;
    gap: 12px;
  }

  .secondary-nav-content {
    flex-direction: column;
    padding: 12px 16px;
    gap: 12px;
  }

  .nav-tabs {
    width: 100%;
    padding-bottom: 4px;
    overflow-x: auto;
    gap: 4px;
  }

  .nav-tab {
    padding: 10px 12px;
    font-size: 13px;
    white-space: nowrap;
  }

  .publish-btn {
    width: 100%;
    padding: 10px;
  }

  .sidebar {
    position: relative;
    top: 0;
    width: 100%;
    min-width: auto;
  }

  .content-area {
    width: 100%;
  }

  .card-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .card-footer {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .card-footer .t-button {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .page-body {
    padding: 8px;
  }

  .secondary-nav-content {
    padding: 8px 12px;
  }

  .nav-tab {
    padding: 8px 10px;
    font-size: 12px;
  }

  .publish-btn {
    padding: 8px;
    font-size: 13px;
  }

  .sidebar {
    padding: 12px;
  }

  .sidebar-title {
    font-size: 13px;
  }

  .database-card {
    padding: 16px;
  }

  .card-title {
    font-size: 16px;
  }

  .card-desc {
    margin-bottom: 12px;
    font-size: 13px;
  }
}
</style>
