<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <c-button @click="handleCreate" type="primary" preIcon="ant-design:plus-outlined"> 新增</c-button>
      <c-button @click="doCgformButton" type="primary" preIcon="ant-design:highlight-outlined">自定义按钮</c-button>
      <c-button @click="doEnhanceJs" type="primary" preIcon="ant-design:strikethrough-outlined">JS增强</c-button>
      <c-button @click="doEnhanceSql" type="primary" preIcon="ant-design:filter-outlined">SQL增强</c-button>
      <c-button @click="doEnhanceJava" type="primary" preIcon="ant-design:tool-outlined">JAVA增强</c-button>
      <c-button @click="importOnlineForm" type="primary" preIcon="ant-design:import-outlined">导入数据表</c-button>
      <c-button @click="goGenerateCode" type="primary" preIcon="ant-design:database-outlined">代码生成</c-button>
      <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>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>
  </BasicTable>
  <!--表单抽屉-->
  <CgformDrawer @register="registerDrawer" @success="handleSuccess" />
  <code-generator ref="codeGenRef" />
  <!--导入数据库-->
  <TransDb2Online ref="transd2o" @success="reload" />
  <PreviewModal @register="registerPreviewModal" />
</template>

<script lang="ts" name="cgform" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction, ActionItem, useDrawer, useModal, useListPage } from '@jdlinker/ui';
  import { onlyMessage } from '@jdlinker/utils';
  import { Menu, MenuItem, Dropdown } from 'ant-design-vue';
  import { columns, searchFormSchema } from './cgform.data';
  import CgformDrawer from './components/CgformDrawer.vue';
  import TransDb2Online from './components/TransDb2Online.vue';
  import CodeGenerator from './components/CodeGenerator.vue';
  import PreviewModal from './components/PreviewModal.vue';

  import { list, removeRecord, deleteRecord, codeGenerate, doDbSynch, batchDelete } from './cgform.api';
  //注册drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerPreviewModal, { openModal: openPreviewModal }] = useModal();
  const codeGenRef = ref();
  const transd2o = ref();
  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: '在线表单',
    tableProps: {
      api: list,
      columns: columns,
      size: 'small',
      formConfig: {
        schemas: searchFormSchema
      },
      beforeFetch: (params) => {
        return Object.assign({ column: 'createTime', order: 'desc' }, params);
      }
    }
  });

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

  /**
   * 新增事件
   */
  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
      showFooter: true
    });
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: any) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true
    });
  }
  /**
   * 移除事件
   */
  async function handleRemove(record) {
    await removeRecord({ id: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, reload);
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteRecord({ id: record.id }, reload);
  }
  /**
   * 生成代码
   */
  async function handleGenTable(record) {
    await codeGenerate({ code: record.id, jformType: record.relationType }, reload);
  }
  /**
   * 同步数据库
   */
  async function handleDbSynch(record) {
    await doDbSynch({ code: record.id, synMethod: 'normal' }, reload);
  }

  const doCgformButton = () => {};
  const doEnhanceJs = () => {};
  const doEnhanceSql = () => {};
  const doEnhanceJava = () => {};
  const goGenerateCode = () => {
    if (!selectedRowKeys.value || selectedRowKeys.value.length != 1) {
      onlyMessage('请先选中一条记录', 'warning');
      return;
    }
    const row: any = selectedRows.value[0];
    if (!row.isDbSynch || row.isDbSynch == 'N') {
      onlyMessage('请先同步数据库!', 'warning');
      return;
    }
    if (row.tableType == 3) {
      onlyMessage('请选中该表对应的主表生成代码', 'warning');
      return;
    }
    codeGenRef.value.show(selectedRowKeys.value[0]);
  };
  const importOnlineForm = () => {
    transd2o.value.show();
  };
  const showMyCopyInfo = () => {};
  const copyConfig = () => {};

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }
  function handlePreview(record: any) {
    openPreviewModal(true, {
      record
    });
  }
  /**
   * 操作栏
   */
  function getTableAction(record): ActionItem[] {
    return [
      // {
      //   icon: 'ant-design:eye-outlined',
      //   tooltip: '预览',
      //   onClick: handlePreview.bind(null, record)
      // },
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record)
      }
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record): ActionItem[] {
    return [
      {
        label: '同步数据库',
        onClick: handleDbSynch.bind(null, record),
        ifShow: record.isDbSynch == 'N'
      },
      {
        label: '视图管理',
        onClick: showMyCopyInfo.bind(null, record)
      },
      {
        label: '生成视图',
        onClick: copyConfig.bind(null, record)
      },
      {
        label: '移除',
        popConfirm: {
          title: '是否确认移除',
          confirm: handleRemove.bind(null, record)
        }
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }
</script>

<style scoped></style>
