<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="奖励列表"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['title']"
      @refresh="search"
    >
      <template #toolbar-left>
        <a-input-search v-model="queryForm.rewardsName" placeholder="名称" allow-clear @search="search" />
        <a-input-search v-model="queryForm.rewardsDescribe" placeholder="奖励说明" allow-clear @search="search" />
        <a-input-search v-model="queryForm.inviteNum" placeholder="邀请好友数量" allow-clear @search="search" />
        <a-input-search v-model="queryForm.goldCoinNum" placeholder="金币数量" allow-clear @search="search" />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link status="danger" title="删除" @click="onDelete(record)"> 删除 </a-link>
        </a-space>
      </template>
    </GiTable>
    <JiangliAddModal ref="JiangliAddModalRef" @save-success="search"></JiangliAddModal>
  </div>
</template>

<script setup lang="ts">
import JiangliAddModal from './jiangliAddModal.vue'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import type { JiangliListResp, JiangliQuery } from '@/apis/user/type'
import { jiangliDel, listJiangli } from '@/apis/user/userlist'

defineOptions({ name: 'Jliangli' })

const queryForm = reactive<JiangliQuery>({} as JiangliQuery)

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((page) => listJiangli({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '名称', dataIndex: 'rewardsName', slotName: 'rewardsName', align: 'center', width: 180 },
  { title: '奖励说明', dataIndex: 'rewardsDescribe', slotName: 'rewardsDescribe', align: 'center', width: 180 },
  { title: '邀请好友数量', dataIndex: 'inviteNum', slotName: 'inviteNum', align: 'center', width: 180 },
  { title: '金币数量', dataIndex: 'goldCoinNum', slotName: 'goldCoinNum', align: 'center', width: 180 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 350,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: true,
  },
]

// 重置
const reset = () => {
  queryForm.rewardsName = undefined
  queryForm.rewardsDescribe = undefined
  queryForm.inviteNum = undefined
  queryForm.goldCoinNum = undefined
  search()
}

// 新增
const JiangliAddModalRef = ref<InstanceType<typeof JiangliAddModal>>()
const onAdd = () => {
  JiangliAddModalRef.value?.onAdd()
}
const onUpdate = (record: JiangliListResp) => {
  JiangliAddModalRef.value?.onUpdate(record.id)
}

const onDelete = (record: JiangliListResp) => {
  return handleDelete(() => jiangliDel(record.id), {
    content: `是否确定删除奖励「${record.rewardsName}」？`,
    showModal: true,
  })
}
</script>

<style scoped lang="scss"></style>
