<template>
  <div class="pending-yak-card">
    <div class="card-header">
      <div class="title-container">
        <h2 class="card-title">待交易牦牛数据</h2>
        <div class="title-tibetan">ཚོང་རྒྱུ་བསྡད་པའི་གཡག་གི་གྲངས་ཐོ།</div>
      </div>
      <div class="pagination-info">
        <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
        <span class="total-count">总计 {{ totalRecords }} 条记录</span>
      </div>
    </div>
    
    <div class="table-container">
      <table class="data-table">
        <thead>
          <tr>
            <th>
              <div class="header-text">卖家姓名</div>
              <div class="header-tibetan">ཚོང་པའི་མིང་།</div>
            </th>
            <th>
              <div class="header-text">车牌号</div>
              <div class="header-tibetan">མོ་ཊའི་ཨང་གྲངས།</div>
            </th>
            <th>
              <div class="header-text">联系方式</div>
              <div class="header-tibetan">འབྲེལ་བ་བྱེད་ཐབས།</div>
            </th>
            <th>
              <div class="header-text">待交易牦牛数</div>
              <div class="header-tibetan">ཚོང་རྒྱུ་བསྡད་པའི་གཡག་གྲངས།</div>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr 
            v-for="(record, index) in currentPageData" 
            :key="record.id"
            :class="{ 'highlight': index % 2 === 0 }"
          >
            <td class="seller-name">{{ record.sellerName }}</td>
            <td class="license-plate">{{ record.licensePlate }}</td>
            <td class="contact">{{ record.contact }}</td>
            <td class="yak-count">
              <span class="count-number">{{ record.yakCount }}</span>
              <span class="unit">头</span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div class="table-footer">
      <div class="auto-flip-indicator">
        <div class="flip-icon">⏰</div>
        <span>{{ countdown }}秒后自动翻页</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

// 待交易牦牛卖家信息
const allRecords = ref([])

// 加载待交易牦牛数据
const loadYakData = async () => {
  try {
    console.log('正在从JSON文件加载待交易牦牛数据...')
    const response = await fetch('/待交易牦牛数据.json')
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    const data = await response.json()
    
    if (data.待交易牦牛数据 && Array.isArray(data.待交易牦牛数据)) {
      allRecords.value = data.待交易牦牛数据
      console.log(`成功加载 ${allRecords.value.length} 条待交易牦牛数据`)
    } else {
      throw new Error('待交易牦牛数据格式不正确')
    }
  } catch (error) {
    console.error('加载待交易牦牛数据失败，使用默认数据:', error)
    loadDefaultYakData()
  }
}

// 加载默认数据（作为备用方案）
const loadDefaultYakData = () => {
  allRecords.value = [
    { id: 1, sellerName: '扎西多吉', licensePlate: '藏A·12345', contact: '138****5678', yakCount: 25 },
    { id: 2, sellerName: '次仁央吉', licensePlate: '藏A·23456', contact: '139****6789', yakCount: 18 },
    { id: 3, sellerName: '洛桑旺堆', licensePlate: '藏B·34567', contact: '137****7890', yakCount: 32 },
    { id: 4, sellerName: '普布扎西', licensePlate: '藏A·45678', contact: '136****8901', yakCount: 14 },
    { id: 5, sellerName: '德吉措姆', licensePlate: '藏C·56789', contact: '135****9012', yakCount: 28 }
  ]
}

// 分页设置
const pageSize = 8 // 每页显示8条记录
const currentPage = ref(1)
const countdown = ref(10) // 倒计时

// 计算属性
const totalRecords = computed(() => allRecords.value.length)
const totalPages = computed(() => Math.ceil(totalRecords.value / pageSize))

const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  const end = start + pageSize
  return allRecords.value.slice(start, end)
})

const currentPageTotal = computed(() => {
  return currentPageData.value.reduce((sum, record) => sum + record.yakCount, 0)
})

// 自动翻页逻辑
let flipTimer = null
let countdownTimer = null

const resetCountdown = () => {
  countdown.value = 10
}

const startCountdown = () => {
  countdownTimer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      flipPage()
      resetCountdown()
    }
  }, 1000)
}

const flipPage = () => {
  if (currentPage.value >= totalPages.value) {
    currentPage.value = 1
  } else {
    currentPage.value++
  }
}

const startAutoFlip = () => {
  resetCountdown()
  startCountdown()
}

const stopAutoFlip = () => {
  if (flipTimer) {
    clearInterval(flipTimer)
    flipTimer = null
  }
  if (countdownTimer) {
    clearInterval(countdownTimer)
    countdownTimer = null
  }
}

// 模拟数据更新
const updateData = () => {
  // 随机更新一些记录的牦牛数量
  allRecords.value.forEach(record => {
    if (Math.random() > 0.8) { // 20%的概率更新
      const change = Math.floor(Math.random() * 5) - 2 // -2到2的变化
      record.yakCount = Math.max(1, record.yakCount + change)
    }
  })
}

onMounted(async () => {
  await loadYakData()
  startAutoFlip()
  
  // 每30秒更新一次数据
  setInterval(updateData, 30000)
})

onUnmounted(() => {
  stopAutoFlip()
})
</script>

<style scoped>
.pending-yak-card {
  height: 100%;
  background: rgba(15, 25, 45, 0.4);
  backdrop-filter: blur(20px) saturate(180%);
  border-radius: 32px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  box-shadow: 
    0 16px 64px rgba(0, 0, 0, 0.3),
    0 8px 32px rgba(64, 158, 255, 0.1),
    inset 0 2px 0 rgba(255, 255, 255, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.title-container {
  display: flex;
  align-items: center;
  gap: 16px;
}

.card-title {
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, #409EFF 0%, #67C23A 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
}

.title-tibetan {
  font-size: 28px;
  color: #67C23A;
  font-weight: 600;
  opacity: 0.8;
}

.pagination-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  font-size: 14px;
  color: #ffffff;
  opacity: 0.8;
}

.total-count {
  color: #409EFF;
  font-weight: 500;
}

.table-container {
  flex: 1;
  overflow: hidden;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 20px;
}

.data-table thead th {
  background: rgba(64, 158, 255, 0.2);
  color: #409EFF;
  font-weight: 600;
  padding: 16px 12px;
  text-align: center;
  border-bottom: 2px solid rgba(64, 158, 255, 0.3);
  vertical-align: middle;
}

.header-text {
  font-size: 22px;
  line-height: 1.2;
  margin-bottom: 4px;
}

.header-tibetan {
  font-size: 18px;
  opacity: 0.8;
  line-height: 1.2;
}

.data-table tbody tr {
  transition: background-color 0.3s ease;
}

.data-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.05);
}

.data-table tbody tr.highlight {
  background: rgba(255, 255, 255, 0.03);
}

.data-table tbody td {
  padding: 14px 12px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  color: #ffffff;
  font-size: 20px;
  vertical-align: middle;
}

.seller-name {
  font-weight: 600;
  font-size: 22px;
  color: #67C23A !important;
}

.license-plate {
  font-family: 'Courier New', monospace;
  font-weight: 500;
  font-size: 20px;
  color: #409EFF !important;
}

.contact {
  font-family: 'Courier New', monospace;
  font-size: 20px;
  color: #E6A23C !important;
}

.yak-count {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.count-number {
  font-size: 26px;
  font-weight: 700;
  color: #F56C6C !important;
}

.unit {
  font-size: 18px;
  color: #ffffff;
  opacity: 0.7;
}

.table-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.auto-flip-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  color: #ffffff;
  opacity: 0.8;
}

.flip-icon {
  font-size: 16px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.summary-stats {
  font-size: 18px;
  font-weight: 600;
  color: #67C23A;
}
</style> 