<template>
  <div class="p-16">
    <div class="mb-16">
      <bz-button class="mr-8" @click="reloadTable"> 还原 </bz-button>
      <bz-button class="mr-8" @click="changeLoading"> 开启loading </bz-button>
      <bz-button class="mr-8" @click="changeColumns"> 更改Columns </bz-button>
      <bz-button class="mr-8" @click="getColumn"> 获取Columns </bz-button>
      <bz-button class="mr-8" @click="getTableData"> 获取表格数据 </bz-button>
      <bz-button class="mr-8" @click="getTableRawData"> 获取接口原始数据 </bz-button>
      <bz-button class="mr-8" @click="setPaginationInfo"> 跳转到第2页 </bz-button>
    </div>
    <div class="mb-16">
      <bz-button class="mr-8" @click="getSelectRowList"> 获取选中行 </bz-button>
      <bz-button class="mr-8" @click="getSelectRowKeyList"> 获取选中行Key </bz-button>
      <bz-button class="mr-8" @click="setSelectedRowKeyList"> 设置选中行 </bz-button>
      <bz-button class="mr-8" @click="clearSelect"> 清空选中行 </bz-button>
      <bz-button class="mr-8" @click="getPagination"> 获取分页信息 </bz-button>
    </div>
    <BzTable
      :canResize="false"
      title="RefTable示例"
      titleHelpMessage="使用Ref调用表格内方法"
      ref="tableRef"
      :api="api"
      :columns="columns"
      rowKey="id"
      :rowSelection="{ type: 'checkbox' }"
    />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { BzTable, TableActionType } from '/@/index';
  import { getBasicColumns, getBasicShortColumns } from './tableData';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { demoListApi } from './tableData';
  export default defineComponent({
    components: { BzTable },
    setup() {
      const tableRef = ref<Nullable<TableActionType>>(null);
      const { createMessage } = useMessage();

      function getTableAction() {
        const tableAction = unref(tableRef);
        if (!tableAction) {
          throw new Error('tableAction is null');
        }
        return tableAction;
      }
      function changeLoading() {
        getTableAction().setLoading(true);
        setTimeout(() => {
          getTableAction().setLoading(false);
        }, 1000);
      }
      function changeColumns() {
        getTableAction().setColumns(getBasicShortColumns());
      }
      function reloadTable() {
        getTableAction().setColumns(getBasicColumns());

        getTableAction().reload({
          page: 1,
        });
      }
      function getColumn() {
        createMessage.info('请在控制台查看！');
        console.log(getTableAction().getColumns());
      }

      function getTableData() {
        createMessage.info('请在控制台查看！');
        console.log(getTableAction().getDataSource());
      }
      function getTableRawData() {
        createMessage.info('请在控制台查看！');
        console.log(getTableAction().getRawDataSource());
      }

      function getPagination() {
        createMessage.info('请在控制台查看！');
        console.log(getTableAction().getPaginationRef());
      }

      function setPaginationInfo() {
        getTableAction().setPagination({
          current: 2,
        });
        getTableAction().reload();
      }
      function getSelectRowList() {
        createMessage.info('请在控制台查看！');
        console.log(getTableAction().getSelectRows());
      }
      function getSelectRowKeyList() {
        createMessage.info('请在控制台查看！');
        console.log(getTableAction().getSelectRowKeys());
      }
      function setSelectedRowKeyList() {
        getTableAction().setSelectedRowKeys(['0', '1', '2']);
      }
      function clearSelect() {
        getTableAction().clearSelectedRowKeys();
      }

      return {
        tableRef,
        api: demoListApi,
        columns: getBasicColumns(),
        changeLoading,
        changeColumns,
        reloadTable,
        getColumn,
        getTableData,
        getTableRawData,
        getPagination,
        setPaginationInfo,
        getSelectRowList,
        getSelectRowKeyList,
        setSelectedRowKeyList,
        clearSelect,
      };
    },
  });
</script>
