<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate" v-auth="'print:template:add'"> 新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete" v-auth="'print:template:delete'">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>
            批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <PrintTemplateModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="PrintTemplateList" setup>
  import { Icon } from '/@/components/Icon';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useRouter } from 'vue-router';
  import { columns, searchFormSchema } from './printTemplate.data';
  import { list, deleteOne, batchDelete } from './printTemplate.api';
  import PrintTemplateModal from './components/PrintTemplateModal.vue';

  defineOptions({ name: 'PrintTemplateList' });

  const router = useRouter();
  const [registerModal, { openModal }] = useModal();

  const { tableContext } = useListPage({
    tableProps: {
      title: '打印模板',
      api: list,
      columns,
      rowKey: 'id',
      formConfig: { schemas: searchFormSchema },
      actionColumn: { width: 220 },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

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

  function handleEdit(record: Recordable) {
    openModal(true, { isUpdate: true, record });
  }

  function handleDesign(record: Recordable) {
    router.push({ path: '/print/designer', query: { id: record.id } });
  }

  async function handleDelete(record: Recordable) {
    await deleteOne({ id: record.id }, reload);
  }

  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value.join(',') }, reload);
  }

  function handleSuccess() {
    reload();
  }

  function getTableAction(record: Recordable) {
    return [
      { label: '设计', onClick: handleDesign.bind(null, record), auth: 'print:template:edit' },
      { label: '编辑', onClick: handleEdit.bind(null, record), auth: 'print:template:edit' },
      {
        label: '删除',
        color: 'error',
        popConfirm: {
          title: '确认删除？',
          confirm: handleDelete.bind(null, record),
        },
        auth: 'print:template:delete',
      },
    ];
  }
</script>
