import { Layout, theme } from "antd";
import React, { useEffect, useState, useRef } from "react";
import { useNavigate } from "react-router-dom/dist";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import HomeIcon from "@mui/icons-material/Home";
import { useDispatch, useSelector } from "react-redux";
import {
Box,
Button,
Card,
CardContent,
Typography,
CircularProgress,
} from "@mui/material";
import { updateEvQrcodeList } from "../redux/actions/actions";
import SystemNumberDialog from "./SystemNumberDialog";
import { toast, ToastContainer } from "react-toastify";
import LoadingContainer from "./LoadingContainer";
const { Content, Header } = Layout;
function EvQrcode() {
const [isLoading, setIsLoading] = useState(false);
const [showSystemNoContainer, setShowSystemNoContainer] = useState(false);
const [anomalyData, setAnomalyData] = useState([]);
const [currentIndex, setCurrentIndex] = useState(0);
const inputRef = useRef(null);
const navigate = useNavigate();
const evQrcodeList = useSelector((state) => state?.evQrcodeList);
console.log("evQrcodeList = ", evQrcodeList);
const reduxSystemNo = useSelector((state) => state?.systemNumber);
console.log("systemno: ", reduxSystemNo);
const dispatch = useDispatch();
const {
token: { colorBgContainer },
} = theme.useToken();
useEffect(() => {
if (!reduxSystemNo) {
setShowSystemNoContainer(true);
} else {
if (evQrcodeList.length > 0) {
setAnomalyData(evQrcodeList);
} else {
fetchAnomalyData();
}
}
}, [reduxSystemNo]);
useEffect(() => {
if (evQrcodeList.length > 0) {
setAnomalyData(evQrcodeList);
}
}, [evQrcodeList]);
const handleSystemNoChange = () => {
console.log("System No Change is called");
setShowSystemNoContainer(true);
dispatch(updateEvQrcodeList([]));
};
const updateSystemReservationStatus = async (systemRecords) => {
const payload = {
systemRecords,
sysNo: reduxSystemNo,
};
try {
fetch(
`${
import.meta.env.VITE_REACT_APP_BACKEND_URL
}/updateSystemReservationStatusEvQrcode`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
)
.then((response) => response.json())
.then((responseData) => {
console.log("response from updation : ", responseData);
});
} catch (error) {
throw new Error("Error in update system records : ", systemRecords);
}
};
const fetchAnomalyData = async () => {
setIsLoading(true);
try {
const response = await fetch(
`${import.meta.env.VITE_REACT_APP_BACKEND_URL}/getEvRecords`,
{
method: "POST",
body: JSON.stringify({
sysno: reduxSystemNo,
}),
headers: {
"Content-Type": "application/json",
},
}
);
const responseData = await response.json();
var systemRecords = responseData?.data;
console.log("System record ====== ", responseData.systemRecord);
if (!responseData.systemRecord) {
systemRecords = getRecordsBySystemId(responseData?.data, reduxSystemNo);
}
console.log("System records : ", systemRecords);
dispatch(updateEvQrcodeList(systemRecords));
updateSystemReservationStatus(systemRecords);
} catch (error) {
console.error("Error fetching data: ", error);
} finally {
setIsLoading(false);
}
};
function getRecordsBySystemId(records, systemId) {
const new_data = [];
for (var i = 0; i < records.length; i++) {
var count = i % 5;
if (count === systemId - 1) {
new_data.push(records[i]);
}
}
return new_data;
}
const handleNext = () => {
setIsLoading(true);
setAnomalyData([]);
setTimeout(() => {
const newItems = anomalyData.filter((_, index) => index !== currentIndex);
setAnomalyData(newItems);
if (currentIndex >= newItems.length) {
setCurrentIndex(0);
}
setIsLoading(false)
},1000);
};
const handleUpdate = async () => {
try {
const inputValue = document.getElementById("qrcodeInput").value;
console.log("inputvalu = ", inputValue);
const payload = {
imageName: currentItem.image_name,
qrvalue: inputValue,
};
console.log("payload=", payload);
const response = await fetch(
`${import.meta.env.VITE_REACT_APP_BACKEND_URL}/updateEvRecord`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);
const responseData = await response.json();
console.log("responsedata: ", responseData);
if (responseData?.status_code === 200) {
toast.success("Record Updated Successfully");
handleNext();
document.getElementById("qrcodeInput").value = "";
} else {
toast.error("Updation Failed");
}
} catch (error) {
console.error("Error fetching data: ", error);
} finally {
setIsLoading(false);
}
};
const handleArrowBack = () => {
dispatch(updateEvQrcodeList([]));
navigate(-1);
};
if (isLoading) {
return ;
}
const currentItem = anomalyData[currentIndex];
const imageUrl = currentItem
? `https://docs.exampaper.vidh.ai/${currentItem.s3_path}`
: "";
const handleKeyDown = (e) => {
if (e.key === "Enter") {
console.log("Updating ......");
handleUpdate();
}
};
return (
{reduxSystemNo && (
System No : {reduxSystemNo}
)}
{currentItem ? (
Records Count: {anomalyData.length}
Image Name: {currentItem.image_name}
{/* S3 Path: {currentItem.s3_path} */}
Qrcode Value
) : (
No items to display
)}
{showSystemNoContainer && (
)}
{isLoading && }
);
}
export default EvQrcode;