'use client'

import React from 'react'
import { Label, Select, TextInput } from 'flowbite-react'

import SearchButton from '@/lib/components/buttons/SearchButton'
import formTheme from '@/lib/themes/form'
import { SearchFormProps } from '../types'
import { PartnerCategory } from '@/lib/types/enums/partner'
import MerchantSelector from '@/lib/components/forms/MerchantSelector'

interface Props {
  searchForm: SearchFormProps
  handleSearch: (e?: React.FormEvent) => void
  setSearchForm: React.Dispatch<React.SetStateAction<SearchFormProps>>
}

export default function SearchForm({
  searchForm,
  handleSearch,
  setSearchForm,
}: Props) {
  return (
    <form onSubmit={handleSearch} className="mt-10">
      <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 w-full max-w-6xl gap-4">
        {/* Search */}
        <div className="w-full max-w-96">
          <div className="mb-2 block">
            <Label htmlFor="search">Search Travel Partner</Label>
          </div>

          <TextInput
            id="search"
            theme={formTheme.textInput}
            placeholder="Search travel partner..."
            value={searchForm.params.search || ''}
            onChange={(e) =>
              setSearchForm({
                ...searchForm,
                params: { ...searchForm.params, search: e.target.value },
              })
            }
          />
        </div>

        {/* Partner Category */}
        <div className="w-full max-w-96">
          <div className="mb-2 block">
            <Label htmlFor="partner_category">Partner Category</Label>
          </div>

          <Select
            id="partner_category"
            theme={formTheme.select}
            value={searchForm.params.partner_category || ''}
            onChange={(e) =>
              setSearchForm({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  partner_category: e.target.value,
                },
              })
            }
          >
            <option value="">-- All Categories --</option>
            <option value={PartnerCategory.HotelVilla}>Hotel/Villa</option>
            <option value={PartnerCategory.Restaurant}>Restaurant</option>
            <option value={PartnerCategory.Driver}>Driver</option>
          </Select>
        </div>

        {/* Guest Market */}
        <div className="w-full max-w-96">
          <div className="mb-2 block">
            <Label htmlFor="guest_market">Guest Market</Label>
          </div>

          <TextInput
            id="guest_market"
            theme={formTheme.textInput}
            placeholder="Enter guest market..."
            value={searchForm.params.guest_market || ''}
            onChange={(e) =>
              setSearchForm({
                ...searchForm,
                params: { ...searchForm.params, guest_market: e.target.value },
              })
            }
          />
        </div>

        {/* Has Referral Links */}
        <div className="w-full">
          <div className="mb-2 block">
            <Label htmlFor="has_links">Has Referral Links</Label>
          </div>

          <Select
            id="has_links"
            name="has_links"
            theme={formTheme.select}
            value={
              searchForm.params.has_links === undefined
                ? ''
                : searchForm.params.has_links
                  ? 'yes'
                  : 'no'
            }
            onChange={(e) =>
              setSearchForm(() => ({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  has_links:
                    e.target.value === ''
                      ? undefined
                      : e.target.value === 'yes'
                        ? true
                        : false,
                },
              }))
            }
          >
            <option value="">All</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
          </Select>
        </div>

        {/* Merchants */}
        <MerchantSelector
          id="merchant_id"
          label="Merchant"
          value={searchForm.params.merchant_id}
          onChange={(merchant) =>
            setSearchForm(() => ({
              ...searchForm,
              params: {
                ...searchForm.params,
                merchant_id: merchant ? merchant.id : undefined,
              },
            }))
          }
        />
      </div>

      <SearchButton isProcessing={searchForm.searching} className="mt-5" />
    </form>
  )
}
