"use client";
import { getLocation } from "@/app/actions";
import { useState } from "react";
import { useFormStatus } from "react-dom";

export default function LocationSearch(props: any) {
  const [searchLocation, setSearchLocation] = useState("");
  const setGeoLocation = props.setGeoLocation;
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearchLocation(e.target.value);
  };
  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    const coordinates = getLocation(searchLocation);
    setGeoLocation(coordinates);
  };
  const { pending } = useFormStatus();
  return (
    <>
      <form onSubmit={handleSubmit}>
        <input
          id="location"
          name="locationSearch"
          type="text"
          placeholder="enter a location to get the weather"
          value={searchLocation}
          onChange={handleChange}
        />
        <button type="submit" disabled={pending}>
          {pending ? "Searching..." : "Search"}
        </button>
        <label htmlFor="location" className="hidden">
          Enter location to get the weather!
        </label>
      </form>
      <p>{searchLocation}</p>
    </>
  );
}
