<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <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 === 'action'">
          <TableAction
            :actions="[
              {
                label: '查看',
                onClick: () => {
                  handleView(record);
                },
              },
              {
                label: '编辑',
                onClick: () => {
                  handleEdit(record);
                },
              },
              {
                label: '删除',
                color: 'error',
                onClick: () => {
                  handleDelete(record);
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" />
    <Modal @register="registerModal" @submit="submit" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useDrawer } from '@/components/Drawer';
  import { useModal } from '@/components/Modal';
  // 子组件
  import Drawer from './drawer.vue';
  import Modal from './modal.vue';
  // ts
  import { searchFormSchema, dbsourcesColumns } from './dbsource.data';
  // api
  import { fun_LoadDataBaseInfo, fun_DeleteEntity } from '@/api/demo/dbsource';

  const { createConfirm, createMessage } = useMessage();

  const isUpdate = ref(false);

  // 抽屉
  const [registerDrawer, { openDrawer }] = useDrawer();
  // 窗口
  const [registerModal, { openModal }] = useModal();
  // 表格
  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
    api: fun_LoadDataBaseInfo,
    columns: dbsourcesColumns,
    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 handleView(record) {
    openDrawer(true, record);
  }
  // 新增-打开窗口
  function handleAdd() {
    isUpdate.value = false;
    openModal(true, {
      isUpdate,
    });
  }
  // 编辑-打开窗口
  function handleEdit(record) {
    isUpdate.value = true;
    openModal(true, {
      isUpdate,
      record,
    });
  }

  // 删除
  async function handleDelete(record) {
    const query = { keyValue: record.id };
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前表单吗？',
      onOk: async () => {
        const data = await fun_DeleteEntity(query);
        if (data) {
          createMessage.success('删除成功');
        } else {
          createMessage.warn('删除失败');
        }
        reload();
      },
    });
  }

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