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 { getLoginLogPage, type LoginLogVO } from '@/services/system/log/login';
import { baseTenantColumns, descriptionsColumns } from './config';

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

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

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

export default SyStemLogLogin;
