<template>
  <div class="settlement-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 + 1 }} 页 / 共 {{ totalPages }} 页</span>
        <span class="total-count">总计 {{ settlementData.length }} 条记录</span>
      </div>
    </div>
    
    <div class="table-container">
      <table class="settlement-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">
            <td>
              <div class="seller-name">{{ record.settler }}</div>
            </td>
            <td>
              <div class="license-plate">{{ record.licensePlate }}</div>
            </td>
            <td>
              <div class="yak-count">{{ record.yakCount }}头</div>
            </td>
            <td>
              <div class="settlement-time">{{ record.settlementTime }}</div>
            </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 currentPage = ref(0)
const pageSize = 8
const countdown = ref(10)
let timer = null
let countdownTimer = null

// 交易结算数据
const settlementData = ref([])

// 加载交易结算数据
const loadSettlementData = 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.交易结算数据)) {
      settlementData.value = data.交易结算数据
      console.log(`成功加载 ${settlementData.value.length} 条交易结算数据`)
    } else {
      throw new Error('交易结算数据格式不正确')
    }
  } catch (error) {
    console.error('加载交易结算数据失败，使用默认数据:', error)
    loadDefaultSettlementData()
  }
}

// 加载默认数据（作为备用方案）
const loadDefaultSettlementData = () => {
  settlementData.value = [
    { id: 1, settler: '扎西多吉', licensePlate: '藏A·12345', yakCount: 15, settlementTime: '09:30' },
    { id: 2, settler: '次仁央吉', licensePlate: '藏B·67890', yakCount: 8, settlementTime: '09:45' },
    { id: 3, settler: '洛桑旺堆', licensePlate: '藏C·11111', yakCount: 22, settlementTime: '10:15' },
    { id: 4, settler: '普布扎西', licensePlate: '藏A·22222', yakCount: 12, settlementTime: '10:30' },
    { id: 5, settler: '德吉梅朵', licensePlate: '藏B·33333', yakCount: 18, settlementTime: '11:00' }
  ]
}

// 计算总页数
const totalPages = computed(() => {
  return Math.ceil(settlementData.value.length / pageSize)
})

// 当前页数据
const currentPageData = computed(() => {
  const start = currentPage.value * pageSize
  const end = start + pageSize
  return settlementData.value.slice(start, end)
})

// 当前页合计
const currentPageTotal = computed(() => {
  return currentPageData.value.reduce((sum, record) => sum + record.yakCount, 0)
})

// 翻页函数
const nextPage = () => {
  currentPage.value = (currentPage.value + 1) % totalPages.value
  countdown.value = 10
}

// 启动定时器
const startTimer = () => {
  // 翻页定时器
  timer = setInterval(() => {
    nextPage()
  }, 10000)
  
  // 倒计时定时器
  countdownTimer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      countdown.value = 10
    }
  }, 1000)
}

// 停止定时器
const stopTimer = () => {
  if (timer) {
    clearInterval(timer)
    timer = null
  }
  if (countdownTimer) {
    clearInterval(countdownTimer)
    countdownTimer = null
  }
}

onMounted(async () => {
  await loadSettlementData()
  startTimer()
})

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

<style scoped>
.settlement-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;
  display: flex;
  flex-direction: column;
}

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

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

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

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

.settlement-table tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.03);
}

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

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

.settlement-time {
  font-size: 20px;
  color: #E6A23C !important;
}

.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);
  flex-shrink: 0;
}

.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> 