"use client";
import React, { useState, useEffect, Suspense } from "react";
import { Text, Button, TextInput, PasswordInput, Title, Card, Group, Box, Image } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { Providers } from "@/app/providers";
import { useRouter } from "next/navigation";
import NextImage from "next/image";
import logo from '@/app/image/logo1.jpg';
import frontPage from '@/app/image/EMandate.jpg';
import { generateCaptcha } from '@/app/captcha';
import styles from './Login.module.css';
import { useSearchParams } from "next/navigation";
function LoginEmandate() {
const router = useRouter();
const [CIF, SetCIF] = useState("");
const [psw, SetPsw] = useState("");
const [captcha, setCaptcha] = useState("");
const [inputCaptcha, setInputCaptcha] = useState("");
const [isLogging, setIsLogging] = useState(false);
const searchParams = useSearchParams();
const data = searchParams.get("data");
const mandateReqDoc = searchParams.get("mandateReqDoc");
const mndtType = searchParams.get("mndtType");
// useEffect(() => {
// if (data) {
// console.log("URL parameter 'data':", data);
// localStorage.setItem("Emendate_data", data);
// localStorage.setItem("Emendate_req_doc", mandateReqDoc || "");
// localStorage.setItem("Emendate_type", mndtType || "");
// }
// }, [data]);
useEffect(() => {
if (data && mandateReqDoc && mndtType) {
console.log("eMandate parameters found");
localStorage.setItem("Emendate_data", data);
localStorage.setItem("Emendate_req_doc", mandateReqDoc);
localStorage.setItem("Emendate_type", mndtType);
} else {
console.log("eMandate parameters missing — clearing localStorage");
localStorage.removeItem("Emendate_data");
localStorage.removeItem("Emendate_req_doc");
localStorage.removeItem("Emendate_type");
}
}, [data, mandateReqDoc, mndtType]);
useEffect(() => {
const loadCaptcha = async () => {
const newCaptcha = await generateCaptcha();
setCaptcha(newCaptcha);
};
loadCaptcha();
}, []);
const regenerateCaptcha = () => {
// setCaptcha(generateCaptcha());
const loadCaptcha = async () => {
const newCaptcha = await generateCaptcha();
setCaptcha(newCaptcha);
};
loadCaptcha();
setInputCaptcha("");
};
async function handleMandateLogin(e: React.FormEvent) {
e.preventDefault();
const onlyDigit = /^\d{11}$/;
if (!onlyDigit.test(CIF)) {
// setError('Input value must be 11 digit');
notifications.show({
withBorder: true,
color: "red",
title: "Invalid UserId",
message: "UserID must be 11 digit",
autoClose: 5000,
});
return;
}
if (!inputCaptcha) {
notifications.show({
withBorder: true,
color: "red",
title: "Invalid Captcha",
message: "Please fill the Captcha filed",
autoClose: 5000,
});
return;
}
if (inputCaptcha !== captcha) {
notifications.show({
withBorder: true,
color: "red",
title: "Captcha Error",
message: "Please enter the correct captcha",
autoClose: 5000,
});
regenerateCaptcha();
return;
}
if (!CIF || !psw) {
notifications.show({
withBorder: true,
color: "red",
title: "Invalid Input",
message: "Please fill UserId and Password",
autoClose: 5000,
});
return;
}
try {
setIsLogging(true);
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Login-Type': 'eMandate',
},
body: JSON.stringify({
customerNo: CIF,
password: psw,
}),
});
const data = await response.json();
if (!response.ok) {
notifications.show({
withBorder: true,
color: "red",
title: "Error",
message: data?.error || "Internal Server Error",
autoClose: 5000,
});
regenerateCaptcha()
localStorage.removeItem("mandate_token");
localStorage.clear();
sessionStorage.clear();
setIsLogging(false);
return;
}
if (response.ok) {
// console.log(data);
const token = data.token;
localStorage.setItem("mandate_token", token);
// localStorage.setItem("pswExpiryDate", data.loginPswExpiry);
if (data.FirstTimeLogin === true) {
notifications.show({
withBorder: true,
color: "red",
title: "Error",
message: "Please go to Internet Banking, set your credentials, and then try logging in here again.",
autoClose: 5000,
});
setIsLogging(false);
}
else {
console.log("Start to validate soft tech data");
const response = await fetch('/api/e-mandate/validation', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Login-Type': 'eMandate',
'Authorization': `Bearer ${localStorage.getItem("mandate_token")}`
},
body: JSON.stringify({
data: localStorage.getItem("Emendate_data"),
mandateRequest: localStorage.getItem("Emendate_req_doc"),
mandateType: localStorage.getItem("Emendate_type"),
// customer_no:CIF
}),
});
const result = await response.json();
localStorage.setItem("Validate_data", result.data);
console.log("Validate Result : ", result);
if (response.ok) {
router.push("/eMandate/otp_page");
}
else {
console.log("validation failed: response",result);
notifications.show({
withBorder: true,
color: "red",
title: "Validation failed",
message: "Failed to validate.Please try again later.",
autoClose: 5000,
});
setIsLogging(false);
}
}
}
else {
regenerateCaptcha();
setIsLogging(false);
notifications.show({
withBorder: true,
color: "red",
title: "Wrong User Id or Password",
message: "Wrong User Id or Password",
autoClose: 5000,
});
}
}
catch (error: any) {
notifications.show({
withBorder: true,
color: "red",
title: "Error",
message: "Internal Server Error,Please try again Later",
autoClose: 5000,
});
setIsLogging(false);
return;
}
}
if (data && mandateReqDoc && mndtType) {
return (