<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>优惠券管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>添加优惠券
          </el-button>
        </div>
      </template>

      <!-- 搜索表单 -->
      <CouponSearchForm @search="handleSearch" @reset="handleReset" />

      <!-- 优惠券表格 -->
      <CouponTable
        :data="couponList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @delete="handleDelete"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <CouponFormDialog
      v-model="formDialogVisible"
      :coupon="currentCoupon"
      :is-edit="isEdit"
      @success="fetchData"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import CouponSearchForm from './components/CouponSearchForm.vue'
import CouponTable from './components/CouponTable.vue'
import CouponFormDialog from './components/CouponFormDialog.vue'
import {
  getCoupons,
  deleteCoupon,
  type CouponListRequest,
  type CouponResponse
} from '@/api/business/coupon'

// 列表数据
const loading = ref(false)
const couponList = ref<CouponResponse[]>([])
const total = ref(0)

// 查询参数
const queryParams = reactive<CouponListRequest>({
  page: 1,
  pageSize: 20
})

// 弹窗控制
const formDialogVisible = ref(false)
const isEdit = ref(false)
const currentCoupon = ref<CouponResponse | null>(null)

// 获取优惠券列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getCoupons(queryParams)
    couponList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = (params: Partial<CouponListRequest>) => {
  queryParams.keyword = params.keyword
  queryParams.type = params.type
  queryParams.page = 1
  fetchData()
}

// 重置
const handleReset = () => {
  queryParams.keyword = undefined
  queryParams.type = undefined
  queryParams.page = 1
  fetchData()
}

// 分页
const handlePageChange = (page: number) => {
  queryParams.page = page
  fetchData()
}

const handleSizeChange = (size: number) => {
  queryParams.pageSize = size
  queryParams.page = 1
  fetchData()
}

// 新增优惠券
const handleAdd = () => {
  isEdit.value = false
  currentCoupon.value = null
  formDialogVisible.value = true
}

// 编辑优惠券
const handleEdit = (row: CouponResponse) => {
  isEdit.value = true
  currentCoupon.value = { ...row }
  formDialogVisible.value = true
}

// 删除优惠券
const handleDelete = async (row: CouponResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除优惠券 "${row.title}" 吗？删除后不可恢复！`,
      '删除确认',
      { type: 'warning' }
    )
    await deleteCoupon(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch {
    // 取消操作
  }
}

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

<style scoped>
.page-container {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
