<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([
  { 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 },
  { id: 6, sellerName: '旺久多吉', licensePlate: '藏A·67890', contact: '134****0123', yakCount: 21 },
  { id: 7, sellerName: '卓玛拉姆', licensePlate: '藏B·78901', contact: '133****1234', yakCount: 35 },
  { id: 8, sellerName: '格桑花措', licensePlate: '藏A·89012', contact: '132****2345', yakCount: 16 },
  { id: 9, sellerName: '丹增诺布', licensePlate: '藏C·90123', contact: '131****3456', yakCount: 29 },
  { id: 10, sellerName: '白玛次仁', licensePlate: '藏A·01234', contact: '130****4567', yakCount: 22 },
  { id: 11, sellerName: '索朗达瓦', licensePlate: '藏B·12340', contact: '158****5678', yakCount: 19 },
  { id: 12, sellerName: '央金卓玛', licensePlate: '藏A·23451', contact: '157****6789', yakCount: 26 },
  { id: 13, sellerName: '阿旺罗布', licensePlate: '藏C·34562', contact: '156****7890', yakCount: 31 },
  { id: 14, sellerName: '拉巴次仁', licensePlate: '藏A·45673', contact: '155****8901', yakCount: 17 },
  { id: 15, sellerName: '曲珍措姆', licensePlate: '藏B·56784', contact: '154****9012', yakCount: 24 },
  { id: 16, sellerName: '边巴扎西', licensePlate: '藏A·67895', contact: '153****0123', yakCount: 33 },
  { id: 17, sellerName: '尼玛次仁', licensePlate: '藏C·78906', contact: '152****1234', yakCount: 20 },
  { id: 18, sellerName: '桑杰卓玛', licensePlate: '藏A·89017', contact: '151****2345', yakCount: 27 },
  { id: 19, sellerName: '强巴次仁', licensePlate: '藏B·90128', contact: '150****3456', yakCount: 15 },
  { id: 20, sellerName: '次央拉姆', licensePlate: '藏A·01239', contact: '149****4567', yakCount: 30 }
])

// 分页设置
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(() => {
  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> 