<template>
  <div>
    <BasicTable
      class="w-4/4 xl:w-5/5"
      @register="registerTable"
      @fetch-success="onFetchSuccess"
      :searchInfo="searchInfo"
    >
      <template #toolbar>
        <a-button type="primary" @click="handleAdd">新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'type'">
          {{ record.type ? '空间表' : '普通表' }}
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '查看',
                onClick: () => {
                  handleView(record);
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <Modal @register="registerModal" @submit="submit" />
  </div>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useModal } from '@/components/Modal';
  // 子组件
  import Modal from './modal/index.vue';
  // ts
  import { searchFormSchema, graphicaltableColumns } from './index.data';
  // api
  import { loadTableRecordInfo } from '@/api/database/index';

  // 是否是修改
  const isUpdate = ref(false);
  // 是否是详情
  const isDetail = ref(false);

  // 窗口
  const [registerModal, { openModal }] = useModal();
  // 表格
  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
    api: loadTableRecordInfo,
    columns: graphicaltableColumns,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    // 序号列
    showIndexColumn: true,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    actionColumn: {
      width: 180,
      title: '操作',
      dataIndex: 'action',
    },
    handleSearchInfoFn(info) {
      return info;
    },
  });

  // 新增-打开窗口
  function handleAdd() {
    isUpdate.value = false;
    isDetail.value = false;
    openModal(true, {
      isUpdate,
      isDetail,
    });
  }
  // 查看-打开窗口
  function handleView(record) {
    isUpdate.value = false;
    isDetail.value = true;
    openModal(true, {
      isUpdate,
      isDetail,
      record,
    });
  }
  // 编辑-打开窗口
  function handleEdit(record) {
    isUpdate.value = true;
    isDetail.value = false;
    openModal(true, {
      isUpdate,
      isDetail,
      record,
    });
  }

  // 新增、编辑、删除-提交后刷新
  function submit() {
    clearSelectedRowKeys();
    reload();
  }
</script>
