import React, { FormEvent } from 'react'
import { Button, Label, Select } from 'flowbite-react'
import { DateType } from 'react-tailwindcss-datepicker'

import formTheme from '@/lib/themes/form'
import buttonTheme from '@/lib/themes/button'
import SearchButton from '@/lib/components/buttons/SearchButton'
import { FastboatName, FastboatRoute } from '@/lib/types/product/fastboat'
import DatePicker, { FormattedDate } from '@/lib/components/DatePicker'
import { SearchFormProps } from '../types'

interface Props {
  searchForm: SearchFormProps
  fastboatNames: FastboatName[]
  fastboatRoutes: FastboatRoute[]
  handleSearch: (e: FormEvent) => void
  setSearchForm: React.Dispatch<React.SetStateAction<SearchFormProps>>
}

export default function SearchForm(props: Props) {
  function resetSearchParams() {
    props.setSearchForm((searchForm) => ({
      ...searchForm,
      params: {
        startDate: null,
        endDate: null,
        fastboatNameId: null,
        fastboatRouteId: null,
        page: 1,
        limit: 10,
      },
    }))
  }

  return (
    <form className="mt-10" onSubmit={props.handleSearch}>
      <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 w-full gap-4 max-w-6xl">
        {/* Boat Name */}
        <div className="w-full">
          <div className="block mb-2">
            <Label htmlFor="fastboat_name_id" theme={formTheme.label}>
              Boat Name
            </Label>
          </div>

          <Select
            id="fastboat_name_id"
            theme={formTheme.select}
            value={props.searchForm.params.fastboatNameId || ''}
            className="w-full max-w-96"
            onChange={(e) => {
              props.setSearchForm({
                ...props.searchForm,
                params: {
                  ...props.searchForm.params,
                  fastboatNameId: e.target.value ? +e.target.value : null,
                },
              })
            }}
          >
            <option value="">All</option>
            {props.fastboatNames.map((name) => (
              <option key={name.id} value={name.id}>
                {name.name}
              </option>
            ))}
          </Select>
        </div>

        {/* Port Route */}
        <div className="w-full max-w-96">
          <div className="mb-2 block">
            <Label theme={formTheme.label} htmlFor="fastboat_route_id">
              Port Route
            </Label>
          </div>
          <Select
            id="fastboat_route_id"
            theme={formTheme.select}
            value={props.searchForm.params?.fastboatRouteId || ''}
            onChange={(e) => {
              props.setSearchForm({
                ...props.searchForm,
                params: {
                  ...props.searchForm.params,
                  fastboatRouteId: e.target.value ? +e.target.value : null,
                },
              })
            }}
          >
            <option value="">All</option>
            {props.fastboatRoutes?.map((route) => (
              <option key={route.id} value={route.id}>
                {route.departure_harbor} &#8596; {route.arrival_harbor}
              </option>
            ))}
          </Select>
        </div>

        {/* Disabled Date Range */}
        <div className="w-full max-w-96">
          <div className="mb-2 block">
            <Label htmlFor="disabled_date_range" theme={formTheme.label}>
              Disabled Date Range
            </Label>
          </div>

          <DatePicker
            inputId="disabled_date_range"
            value={{
              startDate: props.searchForm.params?.startDate as DateType,
              endDate: props.searchForm.params?.endDate as DateType,
            }}
            onDateChange={(formattedDate: FormattedDate) => {
              props.setSearchForm({
                ...props.searchForm,
                params: {
                  ...props.searchForm.params,
                  startDate: formattedDate?.startDate ?? null,
                  endDate: formattedDate?.endDate ?? null,
                },
              })
            }}
          />
        </div>
      </div>

      <div className="flex gap-4 mt-5">
        <SearchButton isProcessing={props.searchForm.searching} />
        <Button
          type="button"
          color="alternative"
          onClick={resetSearchParams}
          theme={buttonTheme.button}
        >
          Reset
        </Button>
      </div>
    </form>
  )
}
