"use client";

import { useActionState } from "react";
import { submitQuoteRequest } from "@/app/actions/quote";

import { textCopy, countryOptions } from "../_data/quote-data";

export function QuoteForm() {
  const [state, quoteAction, isPending] = useActionState(
    submitQuoteRequest,
    null,
  );

  console.log(state);

  return (
    <>
      <form
        className="flex relative w-full lg:w-1/2 mx-auto justify-center pt-20"
        action={quoteAction}
      >
        {state && state.message && (
          <div
            className={`before:content-[''] before:absolute before:top-0 before:bottom-0 before:left-0 before:h-full before:w-1 ${state.success ? "before:bg-primary" : "before:bg-red-600"} overflow-hidden absolute top-0 ${state.success ? "text-primary" : "text-red-600"} w-full mt-2 p-4 ${state.success ? "bg-accent/30" : "bg-red-50"} font-medium rounded-sm`}
          >
            {state.message}
          </div>
        )}
        <div className="w-full flex flex-col gap-8 ">
          <div className="flex gap-10">
            <div className="flex flex-col w-full">
              <label htmlFor="name" className="text-xs text-black/50 font-bold">
                {textCopy.formLabels.name}
              </label>
              <input
                className="mt-0.5 w-full h-10 border-[0.05px] border-black/50 rounded-xs bg-stone-200/70 outline-none hover:bg-stone-200/80 focus:bg-stone-200/80 px-2"
                type="text"
                id="name"
                name="name"
                defaultValue={state?.data?.name}
              />
              <div className="text-red-600">
                {state?.fieldErrors?.name && state.fieldErrors.name.length > 0
                  ? state?.fieldErrors.name[0]
                  : ""}
              </div>
            </div>
            <div className="flex flex-col w-full">
              <label
                htmlFor="email"
                className="text-xs text-black/50 font-bold"
              >
                {textCopy.formLabels.email}
              </label>
              <input
                required
                className="mt-0.5 w-full h-10 border-[0.05px] border-black/50 rounded-xs bg-stone-200/70 outline-none hover:bg-stone-200/80 focus:bg-stone-200/80 px-2"
                type="email"
                id="email"
                name="email"
                defaultValue={state?.data?.email}
              />
              <div className="text-red-600">
                {state?.fieldErrors?.email && state.fieldErrors.email.length > 0
                  ? state?.fieldErrors.email[0]
                  : ""}
              </div>
            </div>
          </div>
          <div className="flex gap-10">
            <div className="flex flex-col w-full">
              <label
                htmlFor="country"
                className="text-xs text-black/50 font-bold"
              >
                {textCopy.formLabels.country}
              </label>
              <div className="flex items-center mt-0.5 w-full h-10 border-[0.05px] border-black/50 rounded-xs bg-stone-200/70 outline-none hover:bg-stone-200/80 focus:bg-stone-200/80 px-2">
                <select
                  required
                  className="w-full outline-none bg-stone-200/70"
                  id="country"
                  name="country"
                  defaultValue={state?.data?.country || ""}
                >
                  <option value="" disabled>
                    {textCopy.formPlaceholders.countrySelect}
                  </option>
                  {countryOptions.map((opt) => (
                    <option key={opt.value} value={opt.value}>
                      {opt.label}
                    </option>
                  ))}
                </select>
              </div>
              <div className="text-red-600">
                {state?.fieldErrors?.country &&
                state.fieldErrors.country.length > 0
                  ? state?.fieldErrors.country[0]
                  : ""}
              </div>
            </div>
            <div className="flex flex-col w-full">
              <label
                htmlFor="quantity"
                className="text-xs text-black/50 font-bold"
              >
                {textCopy.formLabels.quantity}
              </label>
              <input
                className="mt-0.5 w-full h-10 border-[0.05px] border-black/50 rounded-xs bg-stone-200/70 outline-none hover:bg-stone-200/80 focus:bg-stone-200/80 px-2"
                type="number"
                id="quantity"
                name="quantity"
                defaultValue={state?.data?.quantity}
              />
              <div className="text-red-600">
                {state?.fieldErrors?.quantity &&
                state.fieldErrors.quantity.length > 0
                  ? state?.fieldErrors.quantity[0]
                  : ""}
              </div>
            </div>
          </div>
          <div className="flex gap-10">
            <div className="flex flex-col w-full">
              <label
                htmlFor="additionalInfo"
                className="text-xs text-black/50 font-bold"
              >
                {textCopy.formLabels.additionalInfo}
              </label>
              <textarea
                className="mt-0.5 py-1 w-full min-h-28 border-[0.05px] border-black/50 rounded-xs bg-stone-200/70 outline-none hover:bg-stone-200/80 focus:bg-stone-200/80 px-2"
                id="additionalInfo"
                name="additionalInfo"
                rows={6}
                placeholder={textCopy.formPlaceholders.additionalInfo}
                defaultValue={state?.data?.additionalInfo}
              />
              <div className="text-red-600">
                {state?.fieldErrors?.additionalInfo &&
                state.fieldErrors.additionalInfo.length > 0
                  ? state?.fieldErrors.additionalInfo[0]
                  : ""}
              </div>
            </div>
          </div>
          <div className="flex gap-10">
            <div className="flex items-center w-full gap-2">
              <input
                required
                className="border-[0.05px] border-black/50 rounded-xs bg-stone-200/70 outline-none hover:bg-stone-200/80 focus:bg-stone-200/80 size-3"
                type="checkbox"
                id="agreement"
                name="agreement"
              />
              <span className="text-red-600">*</span>
              <label
                htmlFor="agreement"
                className="text-sm text-black/50 font-bold"
              >
                {textCopy.formLabels.agreement}
              </label>
            </div>
            <div className="text-red-600">
              {state?.fieldErrors?.agreement &&
              state.fieldErrors.agreement.length > 0
                ? state?.fieldErrors.agreement[0]
                : ""}
            </div>
          </div>
          <p className="text-xs text-gray-500">{textCopy.termsNotice}</p>
          <button
            type="submit"
            disabled={isPending}
            className="text-white bg-primary px-6 py-2 rounded-xs self-start hover:bg-accent hover:cursor-pointer disabled:opacity-50 transition-colors duration-500"
          >
            {isPending ? "Submitting..." : "Submit"}
          </button>
        </div>
      </form>
    </>
  );
}
