<template>
  <div class="coupon-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <el-table-column prop="id" label="ID" width="80" align="center" />
      
      <el-table-column prop="title" label="优惠券名称" min-width="150" />
      
      <el-table-column label="满减门槛" width="100" align="right">
        <template #default="{ row }">
          <span class="money">¥{{ row.minPrice?.toFixed(2) || '0.00' }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="优惠金额" width="100" align="right">
        <template #default="{ row }">
          <span class="discount">¥{{ row.discountPrice?.toFixed(2) || '0.00' }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="有效期" width="100" align="center">
        <template #default="{ row }">
          {{ row.validDays }}天
        </template>
      </el-table-column>
      
      <el-table-column label="类型" width="120" align="center">
        <template #default="{ row }">
          <el-tag :type="getCouponTypeTagType(row.type)" size="small">
            {{ row.typeName || getCouponTypeName(row.type) }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="使用限制" width="100" align="center">
        <template #default="{ row }">
          <span>{{ row.useLimitName || getUseLimitName(row.useLimit) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="100" align="center">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="禁用"
            inline-prompt
            @change="(val: number) => handleStatusChange(row, val)"
          />
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" width="160" align="center">
        <template #default="{ row }">
          {{ row.createdAt || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="150" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button type="danger" link size="small" @click="handleDelete(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 { ElMessage } from 'element-plus'
import {
  CouponType,
  CouponTypeLabels,
  CouponUseLimitLabels,
  updateCouponStatus,
  type CouponResponse
} from '@/api/business/coupon'

interface Props {
  data: CouponResponse[]
  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: 'edit', row: CouponResponse): void
  (e: 'delete', row: CouponResponse): 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 getCouponTypeName = (type: number): string => {
  return CouponTypeLabels[type] || '未知类型'
}

// 获取优惠券类型标签样式
const getCouponTypeTagType = (type: number): '' | 'success' | 'warning' | 'info' | 'danger' => {
  switch (type) {
    case CouponType.NewUser:
      return 'success'
    case CouponType.Equity:
      return 'warning'
    case CouponType.FullReduction:
      return ''
    default:
      return 'info'
  }
}

// 获取使用限制名称
const getUseLimitName = (useLimit: number): string => {
  return CouponUseLimitLabels[useLimit] || '不限制'
}

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

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

const handleEdit = (row: CouponResponse) => {
  emit('edit', row)
}

const handleDelete = (row: CouponResponse) => {
  emit('delete', row)
}

// 处理状态切换
const handleStatusChange = async (row: CouponResponse, status: number) => {
  try {
    await updateCouponStatus(row.id, status)
    ElMessage.success(status === 0 ? '已启用' : '已禁用')
  } catch (error: any) {
    // 恢复原状态
    row.status = status === 0 ? 1 : 0
    ElMessage.error(error.message || '状态更新失败')
  }
}
</script>

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

.money {
  color: #909399;
}

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

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