"use client"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import { Separator } from "@/components/ui/separator"
import { DateTimePicker } from "@/components/ui/date-time-picker"
import { BuildSitesAdd, buildSitesAddSchema } from "./schema"
import { useAddBuildSitesMutation } from "./queries"

const BuildSitesForm = ({ refetchTable }: { refetchTable: () => void }) => {

    const form = useForm<BuildSitesAdd>({
        resolver: zodResolver(buildSitesAddSchema),
        defaultValues: {
            siteNo: "",
            siteName: "",
            expiryStarts: "",
            expiryEnds: "",
        },
    });

    const { handleSubmit } = form;

    const mutation = useAddBuildSitesMutation();

    function onSubmit(values: BuildSitesAdd) { mutation.mutate({ data: values }); setTimeout(() => refetchTable(), 400) };

    return (
        <Form {...form}>
            <form
                onSubmit={handleSubmit(onSubmit)}
                className="space-y-6 p-4"
            >
                {/* ROW 1 */}
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">

                    <FormField
                        control={form.control}
                        name="siteNo"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel>Site No</FormLabel>
                                <FormControl>
                                    <Input placeholder="12A" {...field} />
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />

                    <FormField
                        control={form.control}
                        name="siteName"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel>Door Number</FormLabel>
                                <FormControl>
                                    <Input placeholder="3" {...field} />
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
                </div>

                <Separator />

                {/* EXPIRY DATES */}
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">

                    <FormField
                        control={form.control}
                        name="expiryStarts"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel>Expiry Starts</FormLabel>
                                <FormControl>
                                    <DateTimePicker {...field} />
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />

                    <FormField
                        control={form.control}
                        name="expiryEnds"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel>Expiry Ends</FormLabel>
                                <FormControl>
                                    <DateTimePicker {...field} />
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
                </div>
                <Button type="submit" className="w-full">
                    Add Build Address
                </Button>

            </form>
        </Form>
    );
};

export { BuildSitesForm }
