/*
 * @Author: your name
 * @Date: 2022-01-11 16:03:24
 * @LastEditTime: 2023-03-03 16:12:49
 * @LastEditors: code4eat awesomedema@gmail.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /KC-MiddlePlatform/src/components/kcTable/index.tsx
 */

import React, { useState, useEffect, useRef } from 'react';
import ProTable, { ActionType, ProColumns } from '@ant-design/pro-table';
import { KCTableType } from './typing';
import './style.less';

const KCTable: React.FC<KCTableType.KCTableProps<any, {}>> = ({
  columns,
  reload,
  newVer,
  ...restProps
}) => {
  const [tableColumns, setTableColumns] = useState<ProColumns[]>([]);
  const actionRef = useRef<ActionType>();

  useEffect(() => {
    setTableColumns(
      columns.map((t) => ({
        hideInSearch: true,
        ...t,
      })),
    );
  }, [columns]);

  useEffect(() => {
    if (reload) {
      actionRef.current?.reload();
    }
  }, [reload]);

  if(newVer){
    return (
      <ProTable
        tableStyle={{
          border: '1px solid #DAE2F2',
          borderRadius:4
        }}
        toolBarRender={false}
        search={false}
        className="KCTable"
        actionRef={actionRef}
        columns={tableColumns}
        pagination={{ defaultPageSize: 10 }}
        {...restProps}
      />
    );
  }else{
    return (
      <ProTable
        tableStyle={{
          border: '1px solid #DAE2F2',
          borderRadius:4
        }}
        className="KCTable"
        actionRef={actionRef}
        columns={tableColumns}
        pagination={{ defaultPageSize: 10 }}
        {...restProps}
      />
    );
  }

};

export default KCTable;
