<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <c-button @click="openImportTableModal(true)" type="primary" preIcon="ant-design:import-outlined"> 导入 </c-button>
      </template>
      <Dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <Menu>
            <MenuItem key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </MenuItem>
          </Menu>
        </template>
        <c-button>批量操作<Icon icon="mdi:chevron-down" /></c-button>
      </Dropdown>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <CgformDrawer @register="registerDrawer" @success="reload()" />
    <PreviewDrawer @register="registerPreviewDrawer" />
    <ImportTableModal @register="registerImportTableModal" @success="reload()" />
  </div>
</template>
<script lang="ts" setup>
  import PreviewDrawer from './components/PreviewDrawer.vue';
  import ImportTableModal from './components/ImportTableModal.vue';
  import CgformDrawer from './components/CgformDrawer.vue';
  import { columns, searchFormSchema } from './codegen.data';
  import { useMessage } from '@jdlinker/func';
  import { useI18n } from 'vue-i18n';
  import { ActionItem, BasicTable, TableAction, useListPage, useDrawer, Button as CButton, useModal } from '@jdlinker/ui';
  import { deleteCodegenTable, downloadCodegen, getCodegenTablePage, syncCodegenFromDB } from './service';
  import { Dropdown, Menu, MenuItem } from 'ant-design-vue';

  defineOptions({ name: 'InfraCodegen' });
  const [registerDrawer, { openDrawer }] = useDrawer();
  const { t } = useI18n();
  const { createMessage } = useMessage();
  const [registerPreviewDrawer, { openDrawer: openPreviewDrawer }] = useDrawer();
  const [registerImportTableModal, { openModal: openImportTableModal }] = useModal();

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: '在线表单',
    tableProps: {
      api: getCodegenTablePage,
      columns,
      size: 'small',
      formConfig: {
        schemas: searchFormSchema
      },
      beforeFetch: (params) => {
        return Object.assign({ column: 'createTime', order: 'desc' }, params);
      }
    }
  });

  //注册table数据
  const [registerTable, { reload }, { selectedRowKeys }] = tableContext;

  function handlePreview(record: any) {
    openPreviewDrawer(true, {
      record
    });
  }

  function handleEditTable(record: any) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true
    });
  }

  async function handleGenTable(record: any) {
    await downloadCodegen(record);
    createMessage.success('成功');
  }

  async function handleSynchDb(record: any) {
    await syncCodegenFromDB(record.id);
    createMessage.success('同步成功');
    await reload();
  }

  async function handleDelete(record: any) {
    await deleteCodegenTable(record.id);
    createMessage.success('删除成功');
    await reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record): ActionItem[] {
    return [
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEditTable.bind(null, record)
      },
      {
        icon: 'ant-design:delete-outlined',
        tooltip: '删除',
        color: 'error',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record): ActionItem[] {
    return [
      {
        label: '预览',
        onClick: handlePreview.bind(null, record)
      },
      {
        label: '生成',
        onClick: handleGenTable.bind(null, record)
      },
      {
        label: '同步',
        popConfirm: {
          title: `确认要强制同步${record.tableName}表结构吗？`,
          placement: 'left',
          confirm: handleSynchDb.bind(null, record)
        }
      }
    ];
  }
</script>
