<template>
  <div class="flex-1 flex flex-col" v-loading="loading">
    <div class="flex-1 p-4 pt-1">
      <div class="relative">
        <el-tabs v-model="searchParams.type" @tab-change="toggleTab">
          <el-tab-pane
            v-for="tab in activityTypeListOptions"
            :key="tab.value"
            :label="tab.label"
            :name="tab.value"
          />
        </el-tabs>
        <div class="absolute right-0 top-2.5 z-1000">
          <el-icon
            size="15"
            class="cursor-pointer hover:rotate-180 transition-all duration-300"
            @click="refresh"
            ><IEpRefresh
          /></el-icon>
        </div>
      </div>
      <!-- 加一行提示 -->
      <div class="flex justify-end">
        <p class="text-gray-500 text-sm mb-1 flex items-center gap-1">
          <el-icon><IEpInfoFilled /></el-icon>
          页面仅展示竞拍成功的记录
        </p>
      </div>
      <div v-if="!list.length" class="flex flex-col items-center justify-center h-64">
        <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mb-4">
          <el-icon class="text-2xl text-gray-300"><IEpDocument /></el-icon>
        </div>
        <div class="text-gray-500 text-lg mb-2">暂无内容</div>
      </div>

      <div v-else class="space-y-4">
        <el-table height="500" :data="list" stripe border>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="startingPrice" label="起拍价" />
          <el-table-column prop="bidPrice" label="支出YA币" />
        </el-table>
      </div>
    </div>

    <div
      v-if="list.length"
      class="flex items-center justify-end px-6 py-4 border-t border-gray-200"
    >
      <div class="pagination-wrapper bg-white rounded-lg shadow-sm border border-gray-100 p-2">
        <el-pagination
          v-model:current-page="searchParams.current"
          v-model:page-size="searchParams.size"
          @size-change="changePageSize"
          @current-change="goToPage"
          :page-sizes="[10, 20, 30, 40]"
          layout="prev, pager, next, jumper, total"
          :total="total"
          class="custom-pagination"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getSelfAuctionRecord } from '@/api'
import { usePageSearch } from '@/hooks'
import { activityTypeListOptions } from '@/constants/options'
import { ActivityTypeEnum } from '@/constants/enums'
import type { TabPaneName } from 'element-plus'

const toggleTab = (key: TabPaneName) => {
  searchParams.value.type = key as ActivityTypeEnum
  refresh()
}

const { list, loading, searchParams, total, refresh, goToPage, changePageSize } = usePageSearch(
  getSelfAuctionRecord,
  {
    defaultParams: {
      type: activityTypeListOptions[0]!.value,
    },
    immediate: false,
  },
)

onActivated(() => {
  searchParams.value.type = activityTypeListOptions[0]!.value
  refresh()
})

defineExpose({
  refresh: () => {
    searchParams.value.type = activityTypeListOptions[0]!.value
    refresh()
  },
})
</script>
