import React, { useState, useEffect, useRef } from 'react';
import {  Divider, Table, Popconfirm, Card, Tooltip, Switch,Input,Button, } from 'antd';
import Highlighter from 'react-highlight-words';
import { EmployeeDto } from '@gtpl/shared-models/gtpl';
import { ColumnProps } from 'antd/lib/table';
import { FormattedMessage } from 'react-intl';
import { AlertMessages } from '@gtpl/shared-utils/alert-messages';
import { useIntl } from 'react-intl';
import {RightSquareOutlined,EyeOutlined,EditOutlined,SearchOutlined} from '@ant-design/icons';
export interface IEmployeeGridProps {
  EmployeeData: EmployeeDto[];
  viewEmployee: (Employee: EmployeeDto, viewOnly: boolean) => void;
  deleteEmployee: (Employee: EmployeeDto) => void;
}

export const EmployeeGrid = (props: IEmployeeGridProps) => {
  // const { formatMessage: fm } = useIntl();
  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',
      key: 'sno',
      width: '70px',
      render: (text, object, index) => (page-1) * 10 +(index+1)    
    }, 
    {
      title: 'Adhar Number',
      dataIndex: 'empCode',
      ...getColumnSearchProps('empCode')
    },
    {
      title: 'Name As on Adhar',
      dataIndex: 'empName',
      ...getColumnSearchProps('empName')
    },
    // {
    //   title: 'Email',
    //   dataIndex:'emailId',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('emailId')
    // },
    {
      title: 'Mobile No',
      dataIndex:'mobileNo',
      responsive: ['lg'],
      ...getColumnSearchProps('mobileNo')
    },
    // {
    //   title: 'Gender ',
    //   dataIndex:'Gender',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('Gender')
    // },
    // {
    //   title: 'Marital Status',
    //   dataIndex:'maritalStatus',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('maritalStatus')
    // },
    {
      title: 'Employment Mode',
      dataIndex:'employmentMode',
      responsive: ['lg'],
      ...getColumnSearchProps('employmentMode')
    },
    // {
    //   title: 'Project',
    //   dataIndex:'projectId',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('projectId')
    // },
    // {
    //   title: 'Sub Contractor',
    //   dataIndex:'subContractorId',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('subContractorId')
    // },
    // {
    //   title: 'Ot Eligiblity',
    //   dataIndex:'otApplicable',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('otApplicable')
    // },
    // {
    //   title: 'Designation',
    //   dataIndex:'designationId',
    //   responsive: ['lg'],
    //   ...getColumnSearchProps('designationId')
    // },
    {
      title: 'Status',
      dataIndex: 'status',
      render: (text, rowData) => (<>{rowData.status===true?'Active':'InActive'}</>),
      filters: [
        {
          text: 'Active',
          value: true,
        },
        {
          text: 'InActive',
          value: false,
        },
      ],
      filterMultiple: false,
      onFilter: (value, record) => 
      {
        console.log(typeof value) // the result is string
        // === is not work
        return record.status === value;
      },
    },
    {
      title:`Action     `,
      dataIndex: 'action',
      render: (text, rowData) => (
        <span>
          
            {/* <EyeOutlined   className={'viewSamplTypeIcon'} type="eye" 
              onClick={() => { props.viewEmployee(rowData, true); }}
              style={{ color: '#1890ff', fontSize: '14px' }}
            /> */}
         
          {/* <Divider type="vertical" /> */}
          
            <EditOutlined  className={'editSamplTypeIcon'}  type="edit" 
              onClick={() => {
                if (rowData.status) {
                  props.viewEmployee(rowData, false);
                } else {
                  AlertMessages.getErrorMessage('You Cannot Edit Deactivated State');
                }
              }}
              style={{ color: '#1890ff', fontSize: '14px' }}
            />
          
          <Divider type="vertical" />
            <Popconfirm onConfirm={e => { props.deleteEmployee(rowData);  }}
            title={
              rowData.status
                ? 'Are you sure to Deactivate State ?'
                :  'Are you sure to Activate State ?'
            }
          >
            <Switch  size="default"
                className={ rowData.status ? 'toggle-activated' : 'toggle-deactivated' }
                checkedChildren={<RightSquareOutlined type="check" />}
                unCheckedChildren={<RightSquareOutlined type="close" />}
                checked={rowData.status}
              />
            
          </Popconfirm>
        </span>
      )
    }
  ];
  function onChange(pagination, filters, sorter, extra) {
    console.log('params', pagination, filters, sorter, extra);
  }

  return (
    <Table
      rowKey={record => record.empId}
      columns={sampleTypeColumns}
      dataSource={props.EmployeeData}
      pagination={{ onChange(current) {setPage(current);} }}
      onChange={onChange}
      bordered
    />
  );

};


export default EmployeeGrid;
