import type { ActionType, ProColumns } from '@ant-design/pro-components';
import React, { useRef } from 'react';
import EnhancedProTable from '@/components/EnhancedProTable';
import ModalDescriptions, {
  type DescriptionsFormRef,
} from '@/components/ModalDescriptions';
import {
  getNotifyMessagePage,
  type NotifyMessageVO,
} from '@/services/system/message/notify/message';
import { baseTenantColumns, descriptionsColumns } from './config';

const SyStemMessageNotifyMessage = () => {
  const tableRef = useRef<ActionType>(null);
  const descriptionsRef = useRef<DescriptionsFormRef>(null);
  const onFetch = async (
    params: NotifyMessageVO & {
      pageSize?: number;
      current?: number;
    },
  ) => {
    const data = await getNotifyMessagePage({
      ...params,
      pageNo: params.current,
      pageSize: params.pageSize,
    });
    return {
      data: data.list,
      success: true,
      total: data.total,
    };
  };

  const handleDetail = (record: NotifyMessageVO) => {
    descriptionsRef.current?.open(record);
  };

  const actionColumns: ProColumns<NotifyMessageVO> = {
    title: '操作',
    dataIndex: 'option',
    valueType: 'option',
    fixed: 'right',
    width: 80,
    render: (text: React.ReactNode, record: NotifyMessageVO) => [
      <a key="editable" onClick={() => handleDetail(record)}>
        详情
      </a>,
    ],
  };
  const columns = [...baseTenantColumns, actionColumns];
  return (
    <>
      <EnhancedProTable<NotifyMessageVO>
        ref={tableRef}
        columns={columns}
        request={onFetch}
        headerTitle="登录日志"
        showIndex={false}
        showSelection={false}
      />
      <ModalDescriptions
        ref={descriptionsRef}
        title="登录日志详情"
        columns={descriptionsColumns()}
      />
    </>
  );
};

export default SyStemMessageNotifyMessage;
