<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <c-button
          v-show="hasPermission('monitor:datalog:compare')"
          type="primary"
          preIcon="ant-design:plus-outlined"
          @click="handleCompare"
          style="margin-right: 5px"
          >数据比较</c-button
        >
      </template>
    </BasicTable>
    <DataLogCompareModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="monitor-datalog" setup>
  import { BasicTable, useListPage, Button as CButton, useModal } from '@jdlinker/ui';
  import DataLogCompareModal from './DataLogCompareModal.vue';
  import { getDataLogList } from './datalog.api';
  import { columns, searchFormSchema } from './datalog.data';
  import { useMessage, usePermission } from '@jdlinker/func';

  const { hasPermission } = usePermission();
  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'datalog-template',
    tableProps: {
      title: '数据日志列表',
      api: getDataLogList,
      columns,
      formConfig: {
        schemas: searchFormSchema
      },
      actionColumn: false
    }
  });

  const [registerTable, { reload }, { rowSelection, selectedRows }] = tableContext;

  function handleCompare() {
    let obj = selectedRows.value;
    console.info('sfsfsf', obj);
    if (!obj || obj.length != 2) {
      createMessage.warning('请选择两条数据!');
      return false;
    }
    if (obj[0].dataId != obj[1].dataId) {
      createMessage.warning('请选择相同的数据库表和数据ID进行比较!');
      return false;
    }
    openModal(true, {
      selectedRows,
      isUpdate: true
    });
  }
</script>
