<script setup lang="tsx">
import { reactive,  unref } from 'vue'
import { getCouponList } from '@/api/marketing'
import { useTable } from '@/hooks/web/useTable'
import { Table, TableColumn } from '@/components/Table'
import { BaseButton } from '@/components/Button'

const { tableRegister, tableState } = useTable({
  fetchDataApi: async () => {
    const res = await getCouponList({
      page: unref(currentPage) || 1,
      limit: unref(pageSize) || 10,
    })
    return {
      list: res.data.list,
      total: res.data.count || 0
    }
  }
})
const emits = defineEmits(['giveCoupon'])
const { dataList, loading, total, currentPage, pageSize } = tableState

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'id',
    label: 'ID',
    align: 'center',
    width: 70
  },
  {
    field: 'title',
    label: '名称',
    minWidth: 150
  },
  
  {
    field: 'coupon_price',
    label: '面值',
    minWidth: 100
  },
  {
    field: 'use_min_price',
    label: '最少消费',
    minWidth: 100
  },
 
  {
    field: 'action',
    label: "操作",
    width: 200,
    fixed: 'right',
    align: 'center',
    headerAlign: 'center',
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            <BaseButton link size="small" type="primary" onClick={() => action(row)}>
              发放
            </BaseButton>
          </>
        )
      }
    }
  }
])
const action = (data) => {
  emits('giveCoupon', data)
}

</script>

<template>
    <Table
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :columns="tableColumns"
      default-expand-all
      node-key="id"
      stripe
      :data="dataList"
      :loading="loading"
      @register="tableRegister"
      :pagination="{
        total
      }"
    />
</template>
