<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <div class="flex items-center justify-center">
            <TableAction
              :actions="[
                { label: '编辑', onClick: handleEdit.bind(null, record) },
                {
                  label: '删除',
                  popConfirm: {
                    title: '是否确认删除',
                    placement: 'topRight',
                    confirm: handleDelete.bind(null, record),
                  },
                },
              ]"
            />
          </div>
        </template>
      </template>
    </BasicTable>
    <IndustriesDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import dayjs from 'dayjs'
  import { BasicTable, useTable, TableAction } from '/@/components/Table'
  import { getShrimpIndustries, deleteShrimpIndustries } from '/@/api/sys/user'
  import { message } from 'ant-design-vue'
  import { useDrawer } from '/@/components/Drawer'
  import IndustriesDrawer from './IndustriesDrawer.vue'
  import { columns, searchFormSchema } from './shrimp-industries'
  const [registerDrawer, { openDrawer }] = useDrawer()
  const [registerTable, { reload }] = useTable({
    title: '稻虾产业 - 列表',
    api: async (e) => {
      if (e.year) e.year = dayjs(e.year).format('YYYY')
      const { data, meta } = await getShrimpIndustries({ ...e })
      return {
        items: data,
        total: meta?.total,
      }
    },
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: true,
  })

  const handleSuccess = () => {
    message.success('操作成功')
    reload()
  }
  const handleCreate = () => {
    openDrawer(true, {
      isUpdate: false,
    })
  }
  const handleEdit = (record: Recordable) => {
    openDrawer(true, {
      ...record,
      isUpdate: true,
    })
  }
  const handleDelete = async (record: Recordable) => {
    await deleteShrimpIndustries(record.id)
    message.success('删除成功')
    reload()
  }
</script>
<style lang="less" scoped>
  :deep(.ant-picker) {
    width: 100%;
  }
</style>
