"use client";
import React, { useEffect, useState } from "react";
import {
Text,
Title,
Box,
Image,
Card,
Checkbox,
Button,
Group,
Grid,
Container,
ActionIcon,
Divider,
Modal,
TextInput,
} from "@mantine/core";
import { Providers } from "@/app/providers";
import { useRouter } from "next/navigation";
import NextImage from "next/image";
import logo from "@/app/image/logo1.jpg";
import { IconLogout, IconX } from "@tabler/icons-react";
import { notifications } from "@mantine/notifications";
import { useMediaQuery } from "@mantine/hooks";
type Mandate = {
id: string;
category: string;
amount: number;
maxAmount: number;
name: string;
frequency: string;
firstCollection: string;
finalCollection: string;
};
const MandateCard = ({
mandate,
onAction,
}: {
mandate: Mandate;
onAction: (id: string, action: "accept" | "reject") => void;
}) => {
const [agreed, setAgreed] = useState(false);
const handleClick = (action: "accept" | "reject") => {
if (!agreed) {
notifications.show({
withBorder: true,
icon: ,
color: "red",
title: "Error",
message:
"Please agree to the debit of mandate processing charges first.",
autoClose: 4000,
});
return;
}
onAction(mandate.id, action);
};
return (
{mandate.category}
Amount: ₹{mandate.amount}
Max Amount: ₹{mandate.maxAmount}
Name: {mandate.name}
Frequency: {mandate.frequency}
First Collection: {mandate.firstCollection}
Final Collection: {mandate.finalCollection}
setAgreed(e.currentTarget.checked)}
/>
);
};
export default function MandatePage() {
const router = useRouter();
const [authorized, setAuthorized] = useState(null);
const [custname, setCustname] = useState(null);
const isMobile = useMediaQuery("(max-width: 768px)");
// OTP Modal states
const [otpModalOpen, setOtpModalOpen] = useState(false);
const [otp, setOtp] = useState("");
const [pendingAction, setPendingAction] = useState<{ id: string; action: "accept" | "reject" } | null>(null);
useEffect(() => {
const token = localStorage.getItem("mandate_token");
if (!token) {
setAuthorized(false);
router.push("/eMandate/login");
} else {
setAuthorized(true);
handleFetchUserName();
}
}, []);
const handleLogout = () => {
localStorage.removeItem("mandate_token");
localStorage.removeItem("user_name");
localStorage.removeItem("userMobNo");
localStorage.removeItem("Emendate_data");
localStorage.removeItem("Emendate_req_doc");
localStorage.removeItem("Emendate_type");
router.push("/eMandate/logout");
};
const handleFetchUserName = async () => {
try {
const token = localStorage.getItem("mandate_token");
const response = await fetch("/api/customer", {
method: "GET",
headers: {
"Content-Type": "application/json",
'X-Login-Type': 'eMandate',
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) throw new Error();
const data = await response.json();
if (Array.isArray(data) && data.length > 0) {
const name = data[0].custname;
const mobileNumber = data[0].mobileno;
localStorage.setItem("user_name", name);
localStorage.setItem("userMobNo", mobileNumber);
setCustname(name);
}
} catch {
notifications.show({
withBorder: true,
color: "red",
title: "Please try again later",
message: "Unable to Fetch, Please try again later",
autoClose: 5000,
});
}
};
// Dummy mandates
const [mandates] = useState([
{
id: "1",
category: "Insurance Premium",
amount: 11743,
maxAmount: 11743,
name: "LIFE INSURANCE CORPORATION",
frequency: "YEAR",
firstCollection: "2025-09-20",
finalCollection: "2038-03-28",
},
{
id: "2",
category: "Loan EMI",
amount: 8500,
maxAmount: 8500,
name: "XYZ BANK",
frequency: "MONTH",
firstCollection: "2025-10-01",
finalCollection: "2030-10-01",
},
]);
// STEP 1: When Accept/Reject pressed → call send OTP API
const handleMandateAction = async (id: string, action: "accept" | "reject") => {
try {
const response = await fetch("/api/otp/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
'X-Login-Type': 'eMandate',
},
body: JSON.stringify({
mobileNumber: localStorage.getItem("userMobNo"),
type: 'EMandate'
}),
});
const data = await response.json();
// console.log(data)
if (!response.ok) throw new Error("Failed to send OTP");
notifications.show({
withBorder: true,
color: "green",
title: "OTP Sent",
message: "An OTP has been sent to your registered mobile number.",
autoClose: 4000,
});
setPendingAction({ id, action });
setOtp("");
setOtpModalOpen(true);
} catch (err) {
notifications.show({
withBorder: true,
color: "red",
title: "Error",
message: "Failed to send OTP. Please try again.",
autoClose: 5000,
});
}
};
// Resend OTP
const handleResendOtp = async () => {
try {
const response = await fetch("/api/otp/send", {
method: "POST",
headers: { "Content-Type": "application/json",'X-Login-Type': 'eMandate' },
body: JSON.stringify({
mobileNumber: localStorage.getItem("userMobNo"),
type: "EMandate",
}),
});
if (!response.ok) throw new Error("Failed to resend OTP");
notifications.show({
withBorder: true,
color: "blue",
title: "OTP Resent",
message: "A new OTP has been sent to your registered mobile number.",
autoClose: 4000,
});
} catch {
notifications.show({
withBorder: true,
color: "red",
title: "Error",
message: "Failed to resend OTP. Please try again.",
autoClose: 5000,
});
}
};
// STEP 2: Verify OTP and complete action
const handleOtpSubmit = async () => {
if (!otp) {
notifications.show({
withBorder: true,
color: "red",
title: "Invalid Input",
message: "Please enter OTP before proceed",
autoClose: 5000,
});
}
if (!pendingAction) return;
try {
const response = await fetch(`/api/otp/verify?mobileNumber=${localStorage.getItem("userMobNo")}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
'X-Login-Type': 'eMandate',
},
body: JSON.stringify({
otp: otp,
}),
});
if (!response.ok) throw new Error("Invalid OTP");
notifications.show({
withBorder: true,
color: "green",
title: "Success",
message: `Mandate ${pendingAction.action}ed successfully!`,
autoClose: 4000,
});
setOtpModalOpen(false);
setPendingAction(null);
} catch {
notifications.show({
withBorder: true,
color: "red",
title: "Error",
message: "Invalid OTP. Please try again.",
autoClose: 4000,
});
}
};
if (!authorized) return null;
return (
{/* HEADER */}
{!isMobile && (
THE KANGRA CENTRAL CO-OPERATIVE BANK LTD.
)}
{isMobile ? (
) : (
{/* WELCOME BAR */}
Welcome, {custname ?? ""}
Last logged in at {new Date().toLocaleString()}
KCCB E-Mandate
{/* CONTENT */}
{mandates.map((mandate) => (
))}
{/* FOOTER */}
© 2025 The Kangra Central Co-Operative Bank Ltd.
{/* OTP MODAL */}
setOtpModalOpen(false)} title="OTP Verification" centered>
An OTP has been sent to your registered mobile number.
{
const input = e.currentTarget.value.replace(/\D/g, "");
if (input.length <= 6) setOtp(input);
}}
/>
If you did not receive the OTP in SMS, you can{" "}
click here to resend the SMS
.
);
}