import { useForm } from "@tanstack/react-form";
import axios from "axios";
import { useState } from "react";
import { toast } from "sonner";

import { Button } from "@/components/ui/button";
import { CardContent, CardHeader } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { LstCard } from "../../../extendedUi/LstCard";

export default function Relocate() {
	const [bookingIn, setBookingIn] = useState(false);
	const form = useForm({
		defaultValues: { runningNr: " ", lane: "" },
		onSubmit: async ({ value }) => {
			// Do something with form data
			setBookingIn(true);

			try {
				const res = await axios.post("/lst/old/api/ocp/bookin", {
					runningNr: parseInt(value.runningNr),
				});

				if (res.data.success) {
					toast.success(res.data.message);
					form.reset();
					setBookingIn(false);
				} else {
					console.log(res.data.data.errors);
					toast.error(res.data.data.errors[0]?.message);
					form.reset();
					setBookingIn(false);
				}
			} catch (error) {
				console.log(error);
				toast.error(
					"There was an error booking in pallet please validate you entered the correct info and try again.",
				);
				setBookingIn(false);
			}
		},
	});
	return (
		<LstCard>
			<CardHeader>
				<p>Relocate a pallet to another lane</p>
			</CardHeader>
			<form
				onSubmit={(e) => {
					e.preventDefault();
					e.stopPropagation();
				}}
			>
				<CardContent>
					<form.Field
						name="runningNr"
						validators={{
							// We can choose between form-wide and field-specific validators
							onChange: ({ value }) =>
								value.length > 2
									? undefined
									: "Please enter a valid running number",
						}}
						children={(field) => {
							return (
								<div className="">
									<Label htmlFor="runningNr" className="mb-2">
										Runnning Number
									</Label>
									<Input
										name={field.name}
										value={field.state.value}
										onBlur={field.handleBlur}
										type="number"
										onChange={(e) => field.handleChange(e.target.value)}
									/>
									{field.state.meta.errors.length ? (
										<em>{field.state.meta.errors.join(",")}</em>
									) : null}
								</div>
							);
						}}
					/>
					<form.Field
						name="lane"
						validators={{
							// We can choose between form-wide and field-specific validators
							onChange: ({ value }) =>
								value.length > 2
									? undefined
									: "Please enter a valid running number",
						}}
						children={(field) => {
							return (
								<div className="">
									<Label htmlFor="runningNr" className="mb-2">
										Enter lane
									</Label>
									<Input
										name={field.name}
										value={field.state.value}
										onBlur={field.handleBlur}
										//type="number"
										onChange={(e) => field.handleChange(e.target.value)}
									/>
									{field.state.meta.errors.length ? (
										<em>{field.state.meta.errors.join(",")}</em>
									) : null}
								</div>
							);
						}}
					/>
					<div className="flex mt-2 justify-end">
						<Button onClick={form.handleSubmit} disabled={bookingIn}>
							Relocate
						</Button>
					</div>
				</CardContent>
			</form>
		</LstCard>
	);
}
