import React, { useRef, useState } from 'react';
import { NotificationsReport } from '@gtpl/shared-models/gtpl';
import { Table } from "ant-table-extensions";
import { ProjectsReport } from '@gtpl/shared-models/gtpl';
import {SearchOutlined} from '@ant-design/icons'
import Highlighter from 'react-highlight-words';
import { ColumnProps } from 'antd/lib/table';
import './notifications-report-grid.css';
import Input from 'antd/lib/input';
import Button from 'antd/lib/button';

/* eslint-disable-next-line */
export interface NotificationsReportGridProps {
  NotificationsReportData: NotificationsReport[];
  // viewNotificationsReport: (NotificationsReport: NotificationsReport, viewOnly: boolean) => void;

}

export function NotificationsReportGrid(props: NotificationsReportGridProps) {
  const [searchText, setSearchText] = useState('');
  const [searchedColumn, setSearchedColumn] = useState('');
  const searchInput = useRef(null);
  const [page, setPage] = React.useState(1);
  const getColumnSearchProps = dataIndex => ({
    filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
      <div style={{ padding: 8 }}>
        <Input
          ref={ searchInput }
          placeholder={`Search ${dataIndex}`}
          value={selectedKeys[0]}
          onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
          onPressEnter={() => handleSearch(selectedKeys, confirm, dataIndex)}
          style={{ width: 188, marginBottom: 8, display: 'block' }}
        />
        <Button
          type="primary"
          onClick={() => handleSearch(selectedKeys, confirm, dataIndex)}
          icon={<SearchOutlined />}
          size="small"
          style={{ width: 90, marginRight: 8 }}
        >
          Search
        </Button>
        <Button onClick={() => handleReset(clearFilters)} size="small" style={{ width: 90 }}>
          Reset
        </Button>
      </div>
    ),
    filterIcon: filtered => (
      <SearchOutlined type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
    ),
    onFilter: (value, record) =>
    record[dataIndex]
    ? record[dataIndex]
       .toString()
        .toLowerCase()
        .includes(value.toLowerCase())
        : false,
    onFilterDropdownVisibleChange: visible => {
      if (visible) {    setTimeout(() => searchInput.current.select());   }
    },
    render: text =>
      text ?(
      searchedColumn === dataIndex ? (
        <Highlighter
          highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
          searchWords={[searchText]}
          autoEscape
          textToHighlight={text.toString()}
        />
      ) :text
      )
      : null
     
  });
  function handleSearch(selectedKeys, confirm, dataIndex) {
    confirm();
    setSearchText(selectedKeys[0]);
    setSearchedColumn(dataIndex);
  };

  function handleReset(clearFilters) {
    clearFilters();
    setSearchText('');
  };
  const sampleTypeColumns: ColumnProps<any>[] = [
    {
      title:'S.No',
      dataIndex:'empId',
      width: 10,
      render: (text, object, index) => (page-1) * 10 +(index+1)
      // fixed: 'left'
    },
    {
      title: 'Date',
      dataIndex: 'timestamp',
      fixed: 'right',
      width: 100,
      ...getColumnSearchProps('timestamp'),
      sorter: (a, b) => a.timestamp.length - b.timestamp.length,
      sortDirections: ['descend', 'ascend'],
      render: (text, rowData) => (<>{rowData.timestamp}</>)
      
    },
    {
      title: 'Activity',
      dataIndex: 'message',
      width: 100,
      ...getColumnSearchProps('message'),
      sorter: (a, b) => a.message.length - b.message.length,
      sortDirections: ['descend', 'ascend'],
      // fixed: 'left'
    },
    
    
    
  ] 
  return (
    <>
      <Table
        rowKey={record => record.Id}
        columns={props.NotificationsReportData.length>0?sampleTypeColumns:null}
        dataSource={props.NotificationsReportData}
        pagination={{ onChange(current) {setPage(current);} }}
        // scroll={{ x: 'calc(1000px + 50%)', y:440 }}
        size="small"
        bordered
        exportable 
        exportableProps={{ /*showColumnPicker: true,*/fileName: "Notifications report" }}
        >
      </Table>
    </>
  );
}

export default NotificationsReportGrid;
