<template>
  <PageWrapper>
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset"></BasicForm>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :actionColumn="actionColumn"
         @selection-change="onSelectionChange"
      >
        <template #tableTitle>
          <el-button type="primary" @click="handleBatchGenerator()" v-perm="['sys:generator:batchGenerator']">
            <template #icon>
              <el-icon class="el-input__icon">
                <PlusOutlined />
              </el-icon>
            </template>
            批量生成
          </el-button>
        </template>
      </BasicTable>
    </el-card>

    <editDialog
        v-if="editVisible"
        :dataSourceId="dataSourceId"
        v-model:visible="editVisible"
        @success="reloadTable('noRefresh')"
    >
    </editDialog>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h,nextTick,defineAsyncComponent } from 'vue';
  import { ColProps } from 'element-plus';
  import { schemas } from './querySchemas';
  import { useForm } from '@/components/Form/index';
  import { TableAction } from '@/components/Table';
  import { getTableList,generator,batchGenerator } from '@/api/tool/generator';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import {message,confirm} from "@/utils/auth";
  const editDialog = defineAsyncComponent(() =>
    import('./edit.vue')
)
const dataSourceId =ref(0)
const editVisible=ref(false)
const selectionData = ref([])
  const tableRef = ref();
  const formParams = reactive({
    tableName:'',
    tableComment:''
  });
  const actionColumn = reactive({
    width: 150,
    label: '操作',
    prop: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction, {
        style: 'button',
        actions: [
          {
            label: '一键生成',
            icon:'SwitchFilled',
            type: 'primary',
            onClick: handleGenerator.bind(null, record),
            auth: ['sys:generator:generator'],
          },
          
        ],
      });
    },
  });

  const loadDataTable = async (res: any) => {
    const result = await getTableList({ ...formParams, ...res });
    return result;
  };

  function reloadTable(noRefresh='') {
    tableRef.value.reload(noRefresh?{}:{pageNo:1});
  }
  const [register, {}] = useForm({
    labelWidth: 100,
    layout: 'horizontal',
    colProps: { span: 6 } as ColProps,
    submitOnReset:true,
    schemas
  });
  function handleSubmit(values: Recordable) {
    handleReset()
    for (const key in values) {
      formParams[key] = values[key]
    }
    reloadTable();
  }

  function handleReset() {
    for (const key in formParams) {
        formParams[key] ='';
    }
  }
  
  /**
   * 批量生成
   * @param record 数据记录
   */
  async function handleBatchGenerator(record: Recordable) {
    let names = []
    if(!record){
      names = selectionData.value.map(({tableName}) => tableName);
    }
    await confirm('确定要批量一键生成吗？');
    record? await batchGenerator({tableNames:record.row.tableName}):await batchGenerator({tableNames:names});
    message("批量生成成功");
    reloadTable()
  }

  /**
   * 一键生成
   * @param recored 数据记录
   */
  const handleGenerator=async (record: Recordable)=>{
      await generator({tableNames:record.row.tableName})
      message("一键生成成功");
  }

  function onSelectionChange(value){
    selectionData.value = value
  }
</script>

<style lang="scss" scoped></style>
