import React, { useRef, useState } from 'react';
import { ColumnProps } from 'antd/lib/table';
import {  Divider, Popconfirm, Card, Tooltip, Switch,Input,Button, } from 'antd';
import {CaretRightOutlined , SearchOutlined,EditOutlined, RightSquareOutlined} from '@ant-design/icons'
import Highlighter from 'react-highlight-words';
import { Table } from "ant-table-extensions";
import './user-projects-grid.css';
import { AlertMessages } from '@gtpl/shared-utils/alert-messages';
import { CreateUserDto, ProjectMappingDto } from '@gtpl/shared-models/gtpl';



/* eslint-disable-next-line */
export interface UserProjectsGridProps {
  employeeProjectsData: any;
  removeproject: (user: ProjectMappingDto) => void;
}

export function UserProjectsGrid(
  props: UserProjectsGridProps
) {
  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: 'Project Name',
      dataIndex: 'projectName',
      width: 100,
      ...getColumnSearchProps('projectName'),
      sorter: (a, b) => a.projectName.length - b.projectName.length,
      sortDirections: ['descend', 'ascend'],
      // fixed: 'left'
    },
    {
      title: 'Assigned Date',
      dataIndex: 'assignedDate',
      width: 100,
      ...getColumnSearchProps('assignedDate'),
      sorter: (a, b) => a.assignedDate.length - b.assignedDate.length,
      sortDirections: ['descend', 'ascend'],
      // fixed: 'left'
    },
    {
      title: 'Employee Status',
      dataIndex: 'isActive',
      width: 100,
      // sorter: (a, b) => a.PresentSundays.length - b.PresentSundays.length,
      sortDirections: ['descend', 'ascend'],
      render: (text, rowData) => (<>{rowData.isActive==true?'Active':'In Active'}</>)
      // fixed: 'right'
    },
    
    {
      title: 'Project Status',
      dataIndex: 'ProjectStatus',
      fixed: 'right',
      width: 100,
      sorter: (a, b) => a.ProjectStatus.length - b.ProjectStatus.length,
      sortDirections: ['descend', 'ascend'],
      render: (text, rowData) => (<>{rowData.ProjectStatus==true?'Active':'In Active'}</>)

      
    },
    {
      title:`Action     `,
      dataIndex: 'action',
      render: (text, rowData) => (
        <span>
          {/* <EditOutlined  className={'editSamplTypeIcon'}  type="edit" 
              onClick={() => {
                if (rowData.status) {
                  console.log(rowData);
                  props.viewuser(rowData, false);
                } else {
                  AlertMessages.getErrorMessage('You Cannot Edit Deactivated user');
                }
              }}
              style={{ color: '#1890ff', fontSize: '14px' }}
            /> */}
             {/* <Divider type="vertical" /> */}
            <Popconfirm onConfirm={e => { props.removeproject(rowData);  }}
            title={
              rowData.isActive
                ? 'Are you sure to Deactivate user?'
                :  'Are you sure to Activate user?'
            }
            >
               <Switch  size="default"
                className={ rowData.isActive ? 'toggle-activated' : 'toggle-deactivated' }
                checkedChildren={<RightSquareOutlined type="check" />}
                unCheckedChildren={<RightSquareOutlined type="close" />}
                checked={rowData.isActive}
              />
            
          </Popconfirm>
        </span>
      )
    }
    
  ] 
  return (
    <>
        <Table
          rowKey={record => record.Id}
          columns={props.employeeProjectsData.length>0?sampleTypeColumns:null}
          dataSource={props.employeeProjectsData}
          pagination={{ onChange(current) {setPage(current);} }}
          size="small"
          bordered
           >
        </Table>
      </>
  );
}

export default UserProjectsGrid;
