<template>
  <div class="order-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <!-- 用户信息 -->
      <el-table-column label="用户信息" min-width="160" fixed="left">
        <template #default="{ row }">
          <div class="user-info">
            <div class="nickname">{{ row.userNickname || '-' }}</div>
            <div class="uid">UID: {{ row.userId }}</div>
            <div class="mobile" v-if="row.userMobile">{{ row.userMobile }}</div>
          </div>
        </template>
      </el-table-column>

      <!-- 回收单号 -->
      <el-table-column prop="recoveryNum" label="回收单号" min-width="180">
        <template #default="{ row }">
          <span class="order-num">{{ row.recoveryNum }}</span>
        </template>
      </el-table-column>

      <!-- 兑换金额 -->
      <el-table-column label="兑换金额" min-width="120" align="right">
        <template #default="{ row }">
          <span class="money">¥{{ row.money?.toFixed(2) || '0.00' }}</span>
        </template>
      </el-table-column>

      <!-- 兑换数量 -->
      <el-table-column prop="count" label="兑换数量" min-width="100" align="center">
        <template #default="{ row }">
          <el-tag type="info" size="small">{{ row.count }} 件</el-tag>
        </template>
      </el-table-column>

      <!-- 兑换时间 -->
      <el-table-column prop="createdAt" label="兑换时间" min-width="160">
        <template #default="{ row }">
          <span class="time-text">{{ row.createdAt }}</span>
        </template>
      </el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作" width="120" fixed="right" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleViewDetail(row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="currentPageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
      class="pagination"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import type { RecoveryOrderResponse } from '@/api/business/order'

interface Props {
  data: RecoveryOrderResponse[]
  loading: boolean
  total: number
  page: number
  pageSize: number
}

const props = defineProps<Props>()

const emit = defineEmits<{
  (e: 'page-change', page: number): void
  (e: 'size-change', size: number): void
  (e: 'view-detail', row: RecoveryOrderResponse): void
}>()

const currentPage = ref(props.page)
const currentPageSize = ref(props.pageSize)

watch(() => props.page, (val) => {
  currentPage.value = val
})

watch(() => props.pageSize, (val) => {
  currentPageSize.value = val
})

const handlePageChange = (page: number) => {
  emit('page-change', page)
}

const handleSizeChange = (size: number) => {
  emit('size-change', size)
}

const handleViewDetail = (row: RecoveryOrderResponse) => {
  emit('view-detail', row)
}
</script>

<style scoped>
.order-table {
  width: 100%;
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nickname {
  font-weight: 500;
}

.uid {
  font-size: 12px;
  color: #909399;
}

.mobile {
  font-size: 12px;
  color: #606266;
}

.order-num {
  font-family: monospace;
  font-size: 13px;
}

.money {
  color: #67c23a;
  font-weight: 600;
  font-size: 14px;
}

.time-text {
  font-size: 13px;
  color: #606266;
}

.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
