import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
import { useManageNotificationContext } from '../hooks/useManageNotificationContext';
import { Button } from '@/components/ui/button';
import { useEffect, useState } from 'react';

const ListToolBar = () => {
  const { table, reload } = useDataGrid();
  const { handleAddDialog } = useManageNotificationContext();
  const [searchValue, setSearchValue] = useState<string>(
    (table.getColumn('content')?.getFilterValue() as string) ?? ''
  );

  const handleKeyDown = (event: React.KeyboardEvent) => {
    if (event.key === 'Enter') {
      handleSearch();
    }
  };

  const handleSearch = () => {
    table.getColumn('content')?.setFilterValue(searchValue);
    table.setPageIndex(0);
  };

  useEffect(() => {
    const timer = setTimeout(() => {
      table.getColumn('content')?.setFilterValue(searchValue);
      table.setPageIndex(0);
    }, 200);

    return () => clearTimeout(timer);
  }, [searchValue, table]);

  return (
    <div className="card-header flex-wrap gap-2 border-b-0 px-5">
      <div className="flex flex-wrap gap-2 lg:gap-5 w-full">
        <div className="flex justify-between w-full items-center">
          <div className="flex w-[50%] gap-3 items-center">
            <label className="input input-sm w-1/3">
              <KeenIcon icon="magnifier" />
              <input
                type="text"
                placeholder="Search notifications"
                value={searchValue}
                onChange={(event) => setSearchValue(event.target.value)}
              />
            </label>
            {/* <DefaultTooltip title={'Search'} placement={'top'}>
              <Button
                variant="outline"
                className="h-7.5 disabled:bg-gray-400"
                onClick={handleSearch}
              >
                <KeenIcon icon="magnifier" />
              </Button>
            </DefaultTooltip> */}
          </div>
          <div className="flex gap-3 items-center">
            <Button
              variant="outline"
              className="h-7.5 text-[0.8rem]"
              onClick={() => handleAddDialog(true)}
            >
              Add Data
            </Button>
            <DefaultTooltip title={'Refresh'} placement={'top'}>
              <Button variant="outline" className="h-7.5" onClick={() => reload()}>
                <KeenIcon icon="arrows-circle" />
              </Button>
            </DefaultTooltip>
          </div>
        </div>
      </div>
    </div>
  );
};

export { ListToolBar };
