<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <PermissionBtn @btn-event="onBtnClicked" />
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                // icon: 'ant-design:ellipsis-outlined',
                label: '立即办理',
                onClick: transaction.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <CreateModal @register="registerModal" @success="handleSuccess" />
    <AuditModel @register="registerAuditModel" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { queryMyDraft } from '@/api/demo/handlingmanagement';

  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import { CreateModal, AuditModel } from './page';
  import { PermissionBtn } from '@/components/PermissionBtn/index';

  import { columns, searchFormSchema } from './data';

  defineOptions({ name: 'RoleManagement' });

  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal: openCreateModal }] = useModal();
  const [registerAuditModel, { openModal: openAuditModel }] = useModal();

  const searchInfo = reactive<Recordable>({});

  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    // 表格名称
    title: '拟办列表',
    // 获取数据的接口
    api: queryMyDraft,
    // 表单列信息 BasicColumn[]
    columns,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    // 使用搜索表单
    useSearchForm: true,
    // 显示表格设置工具
    showTableSetting: true,
    // 是否显示表格边框
    bordered: true,
    // 序号列
    showIndexColumn: true,
    // 搜索
    handleSearchInfoFn(info) {
      return info;
    },
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openCreateModal(true, {
      isUpdate: false,
    });
  }

  async function transaction(record) {
    console.log('transaction', record);
    // openAuditModel(true, {
    //   record,
    // });
    openCreateModal(true, {
      isUpdate: true,
      record,
    });
  }

  function handleSuccess() {
    clearSelectedRowKeys();
    reload();
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        handleCreate();
        break;
      default:
        break;
    }
  }
</script>
