import React, { useEffect, useState } from 'react'
import { Form, Formik } from "formik";
import * as Yup from "yup";
// import LoginImg from '../../assets/bg/login.svg'

import { Link } from 'react-router-dom'
import siteLinks from '../../links/siteLinks'
import { useMutation } from '@tanstack/react-query'
import { recoverPWD } from '../../services/services';

const validationSchema = Yup.object().shape({
    username: Yup.string()
      .email("Wrong email format")
    //   .matches(
    //     /^[^0-9][a-zA-Z0-9._%+-]+@[a-zA-Z]+(\.[a-zA-Z]+)+$/,
    //     "Invalid email format"
    //   )
      .min(3, "Minimum 3 characters")
      .max(50, "Maximum 50 characters")
      .required("Email is required"),
  })

  const initialValues = {
    username: ''
  };

export default function Forgetpwd2() {

  const mutation = useMutation({
    mutationFn: (fields) => {
        return recoverPWD(fields)
    },
    // onSuccess: (res) => {
    //     console.log('res', res)
    // }
  })

  const recoverPWDSubmit = (values) => {
    mutation.mutate(values)
  }

  return (
    <div className="app">
        <div className="app-wrap">
            <div className="app-contant">
                <div className="bg-white">
                    <div className="container-fluid p-0">
                        <div className="row no-gutters">
                            <div className="col-sm-6 col-lg-5 col-xxl-3  align-self-center order-2 order-sm-1">
                                <div className="d-flex align-items-center h-100-vh">
                                    <div className="login p-50">
                                        <h1 className="mb-2">Recover Password</h1>
                                        <p>Please enter your email.</p>
                                        <Formik
                                        initialValues={initialValues}
                                        validationSchema={validationSchema}
                                        onSubmit={recoverPWDSubmit}
                                        >
                                            {(props) => {
                                            return (
                                                <Form className='mt-2 mt-sm-5'>
                                                    <div className="row">
                                                    {!mutation.isSuccess ?
                                                        <>
                                                            <div className="col-12">
                                                                <div className="form-group">
                                                                    <label className={`text-black fw-bold control-label ${(props.errors.username && props.touched.username) && 'text-danger'}`}>Email*</label>
                                                                    <input type="email" name='username' className="form-control" placeholder="Email" value={props.values.username} onChange={props.handleChange} />
                                                                </div>
                                                            </div>
                                                            {mutation.error &&
                                                            <>
                                                            <div className="col-12  mt-3">
                                                                <p className='text-danger'>{mutation.error.message}</p>
                                                            </div>
                                                            </>
                                                            }
                                                            <div className="col-12 mt-3 text-end">
                                                                <button type='submit' className="btn btn-primary text-uppercase">{mutation.isPending ? 'loading...' : 'Send'}</button>
                                                            </div>
                                                        </>
                                                    :
                                                    <div className='col-12'>
                                                        <div className="rounded-2 d-flex flex-column justify-content-between align-items-center" style={{height: '200px', backgroundColor: '#F2FAF7'}}>
                                                            <h4 className='p-4 text-black'>Check your email to continue password reset.</h4>
                                                            <Link to={siteLinks.login} className='p-2 text-primary' style={{color: '#6FCAEF'}}>Home</Link>
                                                        </div>
                                                    </div>
                                                    }
                                                        <div className="col-12  mt-3">
                                                            <p>Go <Link to={siteLinks.home}> Back</Link></p>
                                                        </div>
                                                    </div>
                                                </Form>
                                            );
                                            }}
                                        </Formik>
                                    </div>
                                </div>
                            </div>
                            <div className="custom-bg col-sm-6 col-xxl-9 col-lg-7 b-gradient o-hidden order-1 order-sm-2">
                                <div className="row align-items-center h-100">
                                    <div className="col-7 mx-auto ">
                                        {/* <img className="img-fluid" src={LoginImg} alt="" /> */}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
  )
}
