import { Field, Input, Label, Select } from '@headlessui/react'
import { MagnifyingGlassIcon } from '@heroicons/react/24/solid'
import { useState } from 'react'

interface SearchFilterProperties {
  title: string
  columns?: string[]
  onSearch: (value: string) => void
  onStatusFilter?: (value: string) => Promise<void>
}
export const TableSearchFilter: React.FC<SearchFilterProperties> = ({
  onSearch,
  onStatusFilter,
  title,
}: SearchFilterProperties) => {
  const [searchTerm, setSearchTerm] = useState<string>('')
  const [statusFilter, setStatusFilter] = useState<string>('')

  const handleSearch = (searchValue: React.ChangeEvent<HTMLInputElement>) => {
    setSearchTerm(searchValue.target.value)
    onSearch(searchValue.target.value)
  }

  const handleStatusFilter = (
    searchValue: React.ChangeEvent<HTMLSelectElement>,
  ) => {
    setStatusFilter(searchValue.target.value)
    onStatusFilter?.(searchValue.target.value)
  }

  return (
    <div className="flex items-center gap-5 rounded-lg bg-gray-50 text-[#363636]">
      <div className="w-[400px]">
        <Field>
          <Label className="mb-2 block text-sm font-medium">Cari {title}</Label>
          <div className="relative">
            <Input
              type="text"
              value={searchTerm}
              onChange={handleSearch}
              placeholder={`Cari Nama ${title}`}
              className="w-full rounded-lg bg-white p-2 pr-10 pl-4 shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
            />
            <div className="absolute inset-y-0 right-0 flex items-center pr-3">
              <MagnifyingGlassIcon className="h-5 w-5 text-[#363636]" />
            </div>
          </div>
        </Field>
      </div>
      {onStatusFilter && (
        // will handel if filter have dropdown status
        <div className="w-[235px]">
          <Field>
            <Label className="mb-2 block text-sm font-medium">Status</Label>
            <Select
              value={statusFilter}
              onChange={handleStatusFilter}
              className="w-full rounded-lg bg-white p-2 shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
            >
              <option>Pilih Status</option>
              <option>Aktif</option>
              <option>Nonaktif</option>
            </Select>
          </Field>
        </div>
      )}
    </div>
  )
}
