<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-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 { listTeam, teamDel } from '@/apis/team/jiangli'
import type { TeamListResp } from '@/apis/team/type'

defineOptions({ name: 'Jiangli' })

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((page) => listTeam({ ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '邀请个人奖励', dataIndex: 'inviteOneReward', slotName: 'inviteOneReward', align: 'center', width: 180 },
  { title: '团队A个人奖励', dataIndex: 'teamAScale', slotName: 'teamAScale', align: 'center', width: 180 },
  { title: '团队B个人奖励', dataIndex: 'teamBScale', slotName: 'teamBScale', align: 'center', width: 180 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 350,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: true,
  },
]

// 重置
const reset = () => {
  search()
}

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

const onDelete = (record: TeamListResp) => {
  return handleDelete(() => teamDel(record.id), {
    content: `是否确定删除该团队奖励`,
    showModal: true,
  })
}
</script>

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