<template>
  <div class="order-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <!-- 订单号 -->
      <el-table-column prop="orderNum" label="订单号" min-width="180" fixed="left">
        <template #default="{ row }">
          <span class="order-num">{{ row.orderNum }}</span>
        </template>
      </el-table-column>

      <!-- 用户信息 -->
      <el-table-column label="用户信息" min-width="160">
        <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 label="盒子详情" min-width="200">
        <template #default="{ row }">
          <div class="goods-info">
            <el-image
              v-if="row.goodsImgUrl"
              :src="row.goodsImgUrl"
              :preview-src-list="[row.goodsImgUrl]"
              fit="cover"
              class="goods-img"
              lazy
            >
              <template #error>
                <div class="image-error">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
            <div class="goods-detail">
              <div class="goods-title">{{ row.goodsTitle || '-' }}</div>
              <div class="goods-meta">
                <span>类型: {{ getOrderTypeName(row.orderType) }}</span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- 金额详情 -->
      <el-table-column label="金额详情" min-width="180">
        <template #default="{ row }">
          <div class="money-info">
            <div>订单金额: <span class="money">¥{{ row.orderTotal?.toFixed(2) || '0.00' }}</span></div>
            <div v-if="row.discount > 0">折扣: <span class="discount">-¥{{ row.discount?.toFixed(2) }}</span></div>
            <div>实付金额: <span class="money">¥{{ row.discountTotal?.toFixed(2) || '0.00' }}</span></div>
          </div>
        </template>
      </el-table-column>

      <!-- 抽奖情况 -->
      <el-table-column label="抽奖情况" min-width="120">
        <template #default="{ row }">
          <div class="lottery-info">
            <div>抽奖次数: <span class="count">{{ row.num }}</span></div>
            <div>中奖数: <span class="prize-count">{{ row.prizeNum }}</span></div>
          </div>
        </template>
      </el-table-column>

      <!-- 支付信息 -->
      <el-table-column label="支付信息" min-width="160">
        <template #default="{ row }">
          <div class="pay-info">
            <div v-if="row.weChatPayment > 0">微信: ¥{{ row.weChatPayment?.toFixed(2) }}</div>
            <div v-if="row.balancePayment > 0">余额: ¥{{ row.balancePayment?.toFixed(2) }}</div>
            <div v-if="row.integralPayment > 0">积分: {{ row.integralPayment }}</div>
            <div v-if="row.scorePayment > 0">评分: {{ row.scorePayment }}</div>
            <div v-if="row.couponPayment && row.couponPayment > 0">优惠券: ¥{{ row.couponPayment?.toFixed(2) }}</div>
            <div class="pay-type">
              <el-tag size="small" :type="getPayTypeTagType(row.payType)">
                {{ row.payTypeName }}
              </el-tag>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- 订单状态 -->
      <el-table-column label="状态" width="100" align="center">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)" size="small">
            {{ row.statusName }}
          </el-tag>
        </template>
      </el-table-column>

      <!-- 时间 -->
      <el-table-column label="时间" min-width="160">
        <template #default="{ row }">
          <div class="time-info">
            <div>下单: {{ row.createdAt }}</div>
            <div v-if="row.payTime">支付: {{ row.payTime }}</div>
          </div>
        </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 { Picture } from '@element-plus/icons-vue'
import { OrderStatus, PayType, type OrderListResponse } from '@/api/business/order'

interface Props {
  data: OrderListResponse[]
  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: OrderListResponse): 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 getStatusType = (status: number): 'success' | 'warning' | 'info' | 'danger' => {
  switch (status) {
    case OrderStatus.Paid:
      return 'success'
    case OrderStatus.Unpaid:
      return 'warning'
    case OrderStatus.Cancelled:
      return 'info'
    default:
      return 'info'
  }
}

const getPayTypeTagType = (payType: number): 'success' | 'warning' | 'info' | 'primary' => {
  switch (payType) {
    case PayType.WeChat:
      return 'success'
    case PayType.Alipay:
      return 'primary'
    case PayType.Unpaid:
      return 'warning'
    default:
      return 'info'
  }
}

const getOrderTypeName = (orderType: number): string => {
  const typeMap: Record<number, string> = {
    1: '一番赏',
    2: '无限赏',
    3: '擂台赏',
    4: '抽卡机',
    5: '福袋',
    6: '盲盒',
    7: '连击赏',
    8: '打打乐',
    9: '其他'
  }
  return typeMap[orderType] || '未知'
}

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

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

const handleViewDetail = (row: OrderListResponse) => {
  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;
}

.goods-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.goods-img {
  width: 50px;
  height: 50px;
  border-radius: 4px;
  flex-shrink: 0;
}

.image-error {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  color: #909399;
}

.goods-detail {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.goods-title {
  font-weight: 500;
  line-height: 1.4;
}

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

.money-info {
  font-size: 13px;
  line-height: 1.6;
}

.money {
  color: #e6a23c;
  font-weight: 500;
}

.discount {
  color: #67c23a;
}

.lottery-info {
  font-size: 13px;
  line-height: 1.6;
}

.count {
  color: #409eff;
  font-weight: 500;
}

.prize-count {
  color: #67c23a;
  font-weight: 500;
}

.pay-info {
  font-size: 12px;
  line-height: 1.6;
  color: #606266;
}

.pay-type {
  margin-top: 4px;
}

.time-info {
  font-size: 12px;
  line-height: 1.6;
  color: #606266;
}

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