<template>
  <div class="school-detail-content">
    <!-- 左侧 - 最新动态 -->
    <div class="left-section">
      <AnalysisBox title="最新动态" width="100%" height="100%" :show-all-btn="false" class="dynamics-box">
        <div class="dynamics-list">
          <div v-for="(item, index) in latestDynamics" :key="index" class="dynamic-item">
            <!-- 左侧：日期 -->
            <div class="time-col">
              <div class="year">{{ item.year }}</div>
              <div class="date">{{ item.date }}</div>
            </div>

            <!-- 中间：轴线三段式穿圆心 -->
            <div class="axis-col">
              <!-- 上段轴线，第一个节点不显示 -->
              <div class="axis-line axis-top" :class="{ invisible: index === 0 }"></div>
              <!-- 圆形节点 -->
              <div :class="['timeline-node', item.isHighlight ? 'highlight' : 'normal']">
                <img v-if="item.icon" :src="item.icon" alt="" class="node-img" />
              </div>
              <!-- 下段轴线，最后一个节点不显示 -->
              <div class="axis-line axis-bottom" :class="{ invisible: index === latestDynamics.length - 1 }"></div>
            </div>

            <!-- 右侧：内容 -->
            <div class="content-col">
              <h3 class="title">{{ item.title }}</h3>
              <p class="desc">{{ item.content }}</p>
              <div class="tag-row">
                <div class="tag-list">
                  <AreaTag v-for="(tag, tIndex) in item.tags" :key="tIndex" :tag-name="tag.name" :type="tag.type" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页 -->
        <div class="pagination-area">
          <div class="total">{{ `共${totalDynamics}项动态` }}</div>
          <div class="pagination">
            <button class="page-btn" :disabled="currentPage === 1" @click="handlePageChange(currentPage - 1)">&lt;</button>
            <button 
              v-for="page in displayedPages" 
              :key="page" 
              :class="['page-num', { active: page === currentPage }]"
              @click="typeof page === 'number' && handlePageChange(page)"
            >
              {{ page }}
            </button>
            <button class="page-btn" :disabled="currentPage === totalPages" @click="handlePageChange(currentPage + 1)">&gt;</button>
          </div>
        </div>
      </AnalysisBox>
    </div>

    <!-- 右侧 -->
    <div class="right-section">
      <!-- 基本信息 -->
      <AnalysisBox title="基本信息" width="100%" height="auto" :show-all-btn="false" class="basic-info-box">
        <div class="basic-info">
          <div class="university-image">
            <img :src="basicInfo.image" alt="University" />
          </div>
          <div class="info-list">
            <div class="info-item" v-for="(item, index) in infoList" :key="index">
              <span class="label">{{ item.label }}</span>
              <span class="value">{{ item.value }}</span>
            </div>
            <div class="info-item">
              <span class="label">重点方向：</span>
              <div class="focus-tags">
                <AreaTag v-for="(tag, index) in basicInfo.focusTags" :key="index" :tag-name="tag.name" :type="tag.type" />
              </div>
            </div>
          </div>
          <!-- 重点人物 -->
          <div class="key-people" v-if="keyPeople.length > 0">
            <div class="section-label">重点人物：</div>
            <!-- 最多展示2*2，超出可滚动 -->
            <div class="people-grid" :class="{ 'single-row': keyPeople.length <= 2 }">
              <div v-for="(person, index) in keyPeople" :key="index" class="person-card">
                <img :src="person.avatar" :alt="person.name" class="avatar" />
                <div class="person-info">
                  <div class="person-name">{{ person.name }}</div>
                  <div class="person-title">{{ person.title }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </AnalysisBox>

      <!-- 统计卡片 -->
      <div class="stats-cards">
        <div v-for="(stat, index) in statistics" :key="index" class="stat-card">
          <div class="stat-left">
            <span class="stat-label">{{ stat.label }}</span>
          </div>
          <div class="stat-right">
            <span class="stat-value" :style="{ color: stat.color }">{{ stat.value }}</span>
          </div>
          <div class="stat-indicator" :style="{ background: stat.color }"></div>
        </div>
      </div>

      <!-- 历史时间轴 -->
      <AnalysisBox title="历史时间轴" width="100%" height="auto" :show-all-btn="false" class="history-box">
        <HistoryTimeline :events="historyEvents" />
      </AnalysisBox>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import AreaTag from '@/components/base/AreaTag/index.vue'
import AnalysisBox from '@/components/base/boxBackground/analysisBox.vue'
import HistoryTimeline from '../components/HistoryTimeline.vue'

// Props
const props = defineProps({
  basicInfo: {
    type: Object,
    default: () => ({
      image: '',
      establishedTime: '',
      location: '',
      nature: '',
      studentCount: '',
      staffCount: '',
      qsRanking: '',
      focusTags: []
    })
  },
  keyPeople: {
    type: Array,
    default: () => []
  },
  statistics: {
    type: Array,
    default: () => []
  },
  historyEvents: {
    type: Array,
    default: () => []
  },
  latestDynamics: {
    type: Array,
    default: () => []
  },
  totalDynamics: {
    type: Number,
    default: 0
  }
})

// Emits
const emit = defineEmits(['page-change'])

// 分页
const currentPage = ref(5)
const totalPages = computed(() => Math.ceil(props.totalDynamics / 7))

const displayedPages = computed(() => {
  const pages = []
  pages.push(1)
  if (currentPage.value > 3) {
    pages.push('...')
  }
  for (let i = Math.max(2, currentPage.value - 1); i <= Math.min(totalPages.value - 1, currentPage.value + 1); i++) {
    if (!pages.includes(i)) {
      pages.push(i)
    }
  }
  if (currentPage.value < totalPages.value - 2) {
    pages.push('...')
  }
  if (totalPages.value > 1) {
    pages.push(totalPages.value)
  }
  return pages
})

const handlePageChange = (page) => {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page
    emit('page-change', page)
  }
}

// 基本信息列表
const infoList = computed(() => [
  { label: '成立时间：', value: props.basicInfo.establishedTime },
  { label: '总部地点：', value: props.basicInfo.location },
  { label: '组织性质：', value: props.basicInfo.nature },
  { label: '在校人数：', value: props.basicInfo.studentCount },
  { label: '教职工数：', value: props.basicInfo.staffCount },
  { label: 'QS排名：', value: props.basicInfo.qsRanking }
])
</script>

<style lang="scss" scoped>
.school-detail-content {
  display: flex;
  gap: 16px; 
  align-items: stretch;
  
  .left-section {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
     
    :deep(.dynamics-box),
    :deep(.analysis-box-wrapper) {
      flex: 1;
      display: flex;
      flex-direction: column;
      
      .wrapper-main {
        flex: 1;
        display: flex;
        flex-direction: column;
      }
    }
     
    :deep(.dynamics-box) {
      height: 100%;
    }
  }
  
  .right-section {
    width: 520px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }
} 
$node-size: 24px;
$axis-width: 2px;

.dynamics-list {
  padding: 8px 24px 16px;
  flex: 1;
  overflow-y: auto;

  .dynamic-item {
    display: flex;
    align-items: stretch;
    gap: 0;
 
    .time-col {
      width: 70px;
      flex-shrink: 0;
      text-align: right;
      padding-right: 16px; 
      padding-top: calc(#{$node-size} / 2 - 12px);

      .year, .date {
        font-size: 16px;
        font-weight: 700;
        font-family: 'Microsoft YaHei', sans-serif;
        color: rgb(5, 95, 194);
        line-height: 24px;
      }
    }
 
    .axis-col {
      width: $node-size;
      flex-shrink: 0;
      position: relative;
      // 修复：轴线不穿刺圆点 - 改为 flex-start，圆点用负margin浮在边界上
      display: flex;
      flex-direction: column;
      align-items: center;

      .axis-line {
        width: $axis-width;
        background-color: #D8D8D8;
        flex: 1;
        min-height: 12px;

        &.invisible {
          // 第一个节点上方无线：隐藏并占位，使圆点紧贴timeline起点
          height: 0;
          min-height: 0;
          overflow: hidden;
        }
      }

      .timeline-node {
        width: $node-size;
        height: $node-size;
        border-radius: 50%;
        flex-shrink: 0;
        overflow: visible; // 改为 visible，使负 margin 不被裁剪
        position: relative;
        z-index: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        // 第一个节点特殊处理：居顶，上方无线
        &:first-child {
          margin-top: 0;
        }
        // 后续节点使用负 margin 浮在轴线上
        &:not(:first-child) {
          margin-top: -$node-size / 2;
        }
        // 最后一个节点特殊处理：下方无线
        &:last-child {
          margin-bottom: 0;
        }
        // 非首尾节点使用负 margin
        &:not(:first-child):not(:last-child) {
          margin-bottom: -$node-size / 2;
        }

        &.highlight {
          background-color: rgba(245, 34, 45, 1);
        }

        &.normal {
          background-color: rgba(22, 119, 255, 1);
        }

        .node-img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          display: block;
        }
      }
    }
 
    .content-col {
      flex: 1;
      min-width: 0;
      padding-left: 16px;
      padding-bottom: 24px;
      padding-top: 4px;

      .title {
        font-size: 20px;
        font-weight: 700;
        font-family: 'Microsoft YaHei', sans-serif;
        color: rgb(59, 65, 75);
        line-height: 26px;
        margin: 0 0 8px 0;
        cursor: pointer;

        &:hover {
          color: rgb(5, 95, 194);
        }
      }

      .desc {
        font-size: 16px;
        font-weight: 400;
        font-family: 'Microsoft YaHei', sans-serif;
        color: rgb(95, 101, 108);
        line-height: 24px;
        margin: 0 0 8px 0;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        overflow: hidden;
      }

      .tag-row {
        .tag-list {
          display: flex;
          gap: 8px;
          flex-wrap: wrap;
        }
      }
    }
  }
}

.pagination-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 30px;
  border-top: 1px solid rgb(234, 236, 238);
  
  .total {
    font-size: 16px;
    font-weight: 400;
    font-family: 'Microsoft YaHei', sans-serif;
    color: rgb(59, 65, 75);
  }
  
  .pagination {
    display: flex;
    gap: 6px;
    
    .page-btn,
    .page-num {
      min-width: 32px;
      height: 32px;
      border-radius: 6px;
      border: 1px solid rgba(0, 0, 0, 0.15);
      background: #fff;
      font-size: 14px;
      font-family: 'Microsoft YaHei', sans-serif;
      color: rgb(95, 101, 108);
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      
      &:disabled {
        color: rgba(95, 101, 108, 0.45);
        cursor: not-allowed;
      }
      
      &.active {
        color: rgba(22, 119, 255, 1);
        border-color: rgba(22, 119, 255, 1);
      }
    }
  }
}

.basic-info-box {
  :deep(.wrapper-main) {
    overflow: visible;
  }
}

.basic-info {
  padding: 16px 24px;
  
  .university-image {
    width: 100%;
    height: 200px;
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 16px;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  
  .info-list {
    .info-item {
      display: flex;
      margin-bottom: 12px;
      
      .label {
        width: 88px;
        flex-shrink: 0;
        font-size: 16px;
        font-weight: 700;
        font-family: 'Microsoft YaHei', sans-serif;
        color: rgb(59, 65, 75);
        line-height: 24px;
      }
      
      .value {
        font-size: 16px;
        font-weight: 400;
        font-family: 'Microsoft YaHei', sans-serif;
        color: rgb(59, 65, 75);
        line-height: 24px;
      }
      
      .focus-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
      }
    }
  }
  
  .key-people {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid rgb(234, 236, 238);
    
    .section-label {
      font-size: 16px;
      font-weight: 700;
      font-family: 'Microsoft YaHei', sans-serif;
      color: rgb(59, 65, 75);
      margin-bottom: 12px;
    }
     
    .people-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 16px; 
      max-height: 144px;
      overflow-y: auto;
       
      &.single-row {
        max-height: 64px;
      }
       
      &::-webkit-scrollbar {
        width: 4px;
      }
      &::-webkit-scrollbar-track {
        background: transparent;
      }
      &::-webkit-scrollbar-thumb {
        background: rgb(185, 220, 255);
        border-radius: 2px;
      }
      
      .person-card {
        display: flex;
        align-items: center;
        gap: 8px;
        cursor: pointer;
        height: 64px;
        
        .avatar {
          width: 48px;
          height: 48px;
          border-radius: 50%;
          object-fit: cover;
          flex-shrink: 0;
        }
        
        .person-info {
          flex: 1;
          min-width: 0;
          
          .person-name {
            font-size: 16px;
            font-weight: 700;
            font-family: 'Microsoft YaHei', sans-serif;
            color: rgb(59, 65, 75);
            line-height: 24px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          
          .person-title {
            font-size: 14px;
            font-weight: 400;
            font-family: 'Microsoft YaHei', sans-serif;
            color: rgb(95, 101, 108);
            line-height: 22px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
      }
    }
  }
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  
  .stat-card {
    position: relative;
    background: white;
    border-radius: 10px;
    border: 1px solid rgba(234, 236, 238, 1);
    box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
    padding: 16px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    overflow: hidden;
    
    .stat-indicator {
      position: absolute;
      left: 0;
      top: 15px;
      bottom: 15px;
      width: 4px;
      border-radius: 0 2px 2px 0;
    }
    
    .stat-label {
      font-size: 16px;
      font-weight: 400;
      font-family: 'Microsoft YaHei', sans-serif;
      color: rgb(59, 65, 75);
    }
    
    .stat-value {
      font-size: 30px;
      font-weight: 700;
      font-family: 'Microsoft YaHei', sans-serif;
    }
  }
}


</style>
