import { useState, useEffect } from "react";

export const BasicRequestsFilters = ({
  filters = {},
  onFilterChange
}: {
  filters?: any,
  onFilterChange?: (f: any) => void
} = {}) => {
  const [searchValue, setSearchValue] = useState(filters.search || "");

  useEffect(() => {
    setSearchValue(filters.search || "");
  }, [filters.search]);

  const handleSearchChange = (val: string) => {
    setSearchValue(val);
    if (onFilterChange) onFilterChange({ ...filters, search: val });
  };

  return (
    <>
      <div className="relative w-full flex-1 my-2 md:my-0">
        <input
          type="text"
          placeholder="المدينة او الحي او اسم الشارع"
          value={searchValue}
          onChange={(e) => handleSearchChange(e.target.value)}
          className="w-full rounded-full border border-slate-300 bg-[#F8FAFC] py-3.5 px-9 text-right leading-tight text-slate-700 placeholder-slate-400 focus:border-slate-400 focus:outline-none focus:ring-2 focus:ring-slate-200"
        />
        <svg
          xmlns="http://www.w3.org/2000/svg"
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
          className="w-5 h-5 absolute right-3 top-1/2 -translate-y-1/2 text-slate-400"
        >
          <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" />
          <circle cx="12" cy="10" r="3" />
        </svg>
        {searchValue && (
          <svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2"
            strokeLinecap="round"
            strokeLinejoin="round"
            className="w-5 h-5 absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 cursor-pointer"
            onClick={() => handleSearchChange("")}
          >
            <line x1="18" y1="6" x2="6" y2="18" />
            <line x1="6" y1="6" x2="18" y2="18" />
          </svg>
        )}
      </div>
    </>
  )
};