<template>
  <PageWrapper>
    <a-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <a-input v-model="model[field]" />
        </template>
      </BasicForm>
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.tableName"
        ref="tableRef"
        scroll-x="1200"
        :row-selection="{ onChange: onSelectionChange, selectedRowKeys: selectionData }"
        virtual-scroll
      >
        <template #tableTitle>
          <a-space>
            <a-button
              type="primary"
              @click="handleBatchGenerator"
              v-perm="['sys:generator:batchGenerator']"
              :disabled="!selectionData.length"
            >
              <template #icon>
                <PlusOutlined />
              </template>
              批量生成
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a-button
                type="primary"
                @click="handleGenerator(record.tableName)"
                v-perm="['sys:generator:generator']"
              >
                <template #icon><EditOutlined /></template>
                一键生成
              </a-button>
            </a-space>
          </template>
        </template>
      </BasicTable>
    </a-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, nextTick, reactive, ref, defineAsyncComponent } from 'vue';
  import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import { useForm } from '@/components/Form/index';
  import { getTableList, generator, batchGenerator } from '@/api/tool/generator';
  import { columns } from './columns';
  import { schemas } from './querySchemas';
  import { Modal, message } from 'ant-design-vue';

  /**
   * 定义参数变量
   */
  const tableRef = ref();
  const selectionData = ref([]);

  /**
   * 定义查询参数
   */
  const formParams = reactive({
    tableName: '',
    tableComment: '',
  });

  /**
   * 加载数据列表
   * @param res 参数
   */
  const loadDataTable = async (res) => {
    const result = await getTableList({ ...formParams, ...res });
    return result;
  };

  /**
   * 刷新数据列表
   * @param noRefresh 参数
   */
  function reloadTable(noRefresh = '') {
    selectionData.value = [];
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }

  /**
   * 执行批量生成
   */
  async function handleBatchGenerator() {
    Modal.confirm({
      title: '提示',
      content: '确定要批量一键生成吗？',
      onOk: async () => {
        await batchGenerator({ tableNames: selectionData.value });
        message.success('生成成功');
        selectionData.value = [];
        reloadTable();
      },
    });
  }

  /**
   * 执行单个生成
   */
  async function handleGenerator(name) {
    await generator({ tableNames: name });
    message('一键生成成功');
  }

  /**
   * 执行提交表单
   * @param values 参数
   */
  function handleSubmit(values) {
    for (const key in formParams) {
      formParams[key] = '';
    }
    for (const key in values) {
      formParams[key] = values[key];
    }
    reloadTable();
  }

  /**
   * 执行重置
   */
  function handleReset() {
    for (const key in formParams) {
      formParams[key] = '';
    }
    reloadTable();
  }

  /**
   * 选项发生变化
   * @param value 参数
   */
  function onSelectionChange(value) {
    selectionData.value = value;
  }

  /**
   * 注册
   */
  const [register, {}] = useForm({
    rowProps: { gutter: [16, 0] },
    colProps: {
      xs: 24,
      sm: 24,
      md: 12,
      lg: 8,
      xl: 6,
    },
    labelCol: { span: 6, offset: 0 },
    schemas,
  });
</script>
