<template>
  <div class="history-page">
    <el-card class="page-card">
      <template #header>
        <span class="page-title">{{ t('往期历史') }}</span>
      </template>

      <el-form :model="queryForm" inline>
        <el-form-item :label="t('期号')">
          <el-input v-model="queryForm.issue_no" :placeholder="t('请输入期号')" clearable />
        </el-form-item>

        <el-form-item :label="t('日期区间')">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">{{ t('查询') }}</el-button>
          <el-button @click="handleReset">{{ t('重置') }}</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="historyList" v-loading="loading" stripe>
        <el-table-column prop="issue_no" :label="t('期号')" width="120" />
        <el-table-column prop="openCode1" :label="t('第一位')" width="80" align="center" />
        <el-table-column prop="openCode2" :label="t('第二位')" width="80" align="center" />
        <el-table-column prop="openCode3" :label="t('第三位')" width="80" align="center" />
        <el-table-column prop="sum" :label="t('总和')" width="80" align="center" />
        <el-table-column prop="keno" :label="t('开奖号码')" width="200">
          <template #default="{ row }">
            <div class="keno-display">
              <span v-for="(num, index) in row.keno" :key="index" :class="getKenoCellClass(num)">
                {{ num }}
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" :label="t('开奖时间')" width="180" />
      </el-table>

      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        layout="total, prev, pager, next"
        @current-change="handlePageChange"
        style="margin-top: 20px; justify-content: center;"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { apiUrl } from "@/request/index";

const { t } = useI18n()
const loading = ref(false)
const historyList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dateRange = ref([])

const queryForm = reactive({
  issue_no: ''
})

const getKenoCellClass = (num) => {
  if (!num) return ''
  const number = Number(num)
  return number % 2 === 0 ? 'bg-red' : 'bg-blue'
}

const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      page: currentPage.value,
      limit: pageSize.value,
      issue_no: queryForm.issue_no || undefined
    }

    if (dateRange.value && dateRange.value.length === 2) {
      params.start_time = dateRange.value[0]
      params.end_time = dateRange.value[1]
    }

    const res = await new Promise((resolve) => {
      fetch(`${apiUrl}newPc/list?page=${params.page}&limit=${params.limit}${params.issue_no ? '&issue_no=' + params.issue_no : ''}${params.start_time ? '&start_time=' + params.start_time : ''}${params.end_time ? '&end_time=' + params.end_time : ''}`)
        .then(res => res.json())
        .then(data => resolve(data))
        .catch(() => resolve(null))
    })

    if (res?.code === 0) {
      historyList.value = res.data.list || []
      total.value = res.data.count || 0
    }
  } catch (error) {
    console.error('获取往期历史失败', error)
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  currentPage.value = 1
  fetchData()
}

const handleReset = () => {
  queryForm.issue_no = ''
  dateRange.value = []
  currentPage.value = 1
  fetchData()
}

const handlePageChange = (page) => {
  currentPage.value = page
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.history-page {
  max-width: 1400px;
  margin: 0 auto;
}

.page-card {
  border-radius: 12px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
}

.keno-display {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.keno-display span {
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  border-radius: 2px;
  font-size: 12px;
  font-weight: bold;
}

.keno-display .bg-red {
  background: #d32f2f;
  color: #fff;
}

.keno-display .bg-blue {
  background: #1e88e5;
  color: #fff;
}
</style>
