import { Link } from "react-router-dom"
import { Input } from "../common/input"
import { Controller, SubmitHandler, useForm } from "react-hook-form"
import { LoginWithCodeInterface } from "../../types"
import { yupResolver } from "@hookform/resolvers/yup"
import { LoginWithCodeSchema } from "../../schema"
import { ErrorComponent } from "../common/error"
import { Button } from "@headlessui/react"

export const LoginWithCodeForm = () => {
    const { handleSubmit, formState: { errors }, control } = useForm<LoginWithCodeInterface>({
        resolver: yupResolver(LoginWithCodeSchema)
    })
    const handleLoginWithCode: SubmitHandler<LoginWithCodeInterface> = async (data) => {
        console.log("data=>", data)
    }
    return (
        <form onSubmit={handleSubmit(handleLoginWithCode)}
            className="w-fit xl:w-1/2 xl:p-0 p-10 flex flex-col bg-auth-form justify-center items-center gap-y-[42px] rounded-3xl">
            <img src="/svgs/logo/logo.svg" alt="logo" className="auth-logo-size" />
            <div className="flex flex-col xl:-mr-24">
                <div className="flex flex-col gap-y-5">
                    <span className="font-medium text-lg md:text-2xl xl:text-3xl text-primary-text-color">ورود با رمز یکبار مصرف</span>
                    <div className="flex flex-row gap-x-0.5 items-center">
                        <p className="text-secondary-text-color font-normal text-xs md:text-sm">لطفا شماره موبایل خود را وارد کنید</p>
                    </div>
                </div>
                <div className="mt-9 mb-[42px]">
                    <Controller control={control} name="phoneNumber" render={({ field }) => (
                        <div>
                            <Input type="number" placeholder="شماره موبایل" field={field} />
                            <ErrorComponent show={errors.phoneNumber} message={errors.phoneNumber?.message} />
                        </div>
                    )} />
                </div>
                <div className="flex flex-col gap-y-[27px] items-center">
                    <Button type="submit">دریافت کد تایید</Button>
                    <Link to="/auth/login">
                        <p className="font-medium text-base text-primary-color">بازگشت</p>
                    </Link>
                </div>
            </div>
        </form>
    )
}