'use client'

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

import SearchButton from '@/lib/components/buttons/SearchButton'
import formTheme from '@/lib/themes/form'
import DatePicker, { FormattedDate } from '@/lib/components/DatePicker'
import { PaymentMethodCode, PaymentStatus } from '@/lib/types/payment'
import {
  PAYMENT_METHODS_OPTIONS,
  PAYMENT_STATUS_OPTIONS,
} from '@/lib/constants/payment'
import { SearchFormProps } from '../types'

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 gap-4 max-w-6xl">
        {/* Transaction Date */}
        <div className="w-full max-w-96">
          <div className="mb-2 block">
            <Label htmlFor="transaction_date" theme={formTheme.label}>
              Transaction Date
            </Label>
          </div>

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

        {/* Invoice Number */}
        <div className="w-full">
          <div className="mb-2 block">
            <Label htmlFor="invoice_number" theme={formTheme.label}>
              Invoice Number
            </Label>
          </div>
          <TextInput
            theme={formTheme.textInput}
            id="invoice_number"
            type="text"
            className="w-full max-w-96"
            placeholder="Invoice Number"
            value={searchForm.params.invoiceNumber}
            onChange={(e) =>
              setSearchForm({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  invoiceNumber: e.target.value,
                },
              })
            }
          />
        </div>

        {/* Billing Name */}
        <div className="w-full">
          <div className="mb-2 block">
            <Label theme={formTheme.label} htmlFor="billing_name">
              Billing Name
            </Label>
          </div>
          <TextInput
            theme={formTheme.textInput}
            id="billing_name"
            type="text"
            className="w-full max-w-96"
            placeholder="Billing Name"
            value={searchForm.params.billingName}
            onChange={(e) =>
              setSearchForm({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  billingName: e.target.value,
                },
              })
            }
          />
        </div>

        {/* Payment Method */}
        <div className="w-full">
          <div className="block mb-2">
            <Label htmlFor="payment_method" theme={formTheme.label}>
              Payment Method
            </Label>
          </div>

          <Select
            id="payment_method"
            theme={formTheme.select}
            className="w-full max-w-96"
            defaultValue={searchForm.params.paymentMethod as PaymentMethodCode}
            onChange={(e) => {
              setSearchForm({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  paymentMethod: e.target.value as PaymentMethodCode,
                },
              })
            }}
          >
            <option value="">All</option>
            {PAYMENT_METHODS_OPTIONS.map((paymentMethod) => (
              <option key={paymentMethod.name} value={paymentMethod.value}>
                {paymentMethod.name}
              </option>
            ))}
          </Select>
        </div>

        {/* Payment Status */}
        <div className="w-full">
          <div className="block mb-2">
            <Label htmlFor="payment_status" theme={formTheme.label}>
              Payment Status
            </Label>
          </div>

          <Select
            id="payment_status"
            theme={formTheme.select}
            className="w-full max-w-96"
            defaultValue={searchForm.params.paymentStatus as PaymentStatus}
            onChange={(e) => {
              setSearchForm({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  paymentStatus: e.target.value as PaymentStatus,
                },
              })
            }}
          >
            <option value="">All</option>
            {PAYMENT_STATUS_OPTIONS.map((paymentStatus) => (
              <option key={paymentStatus.name} value={paymentStatus.value}>
                {paymentStatus.name}
              </option>
            ))}
          </Select>
        </div>
      </div>

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