import { Box, Button, Modal, Typography, Divider, Grid } from "@mui/material";
import { RootState } from "../../../redux/store/store";
import { useSelector } from "react-redux";
import { useEffect, useState } from "react";
import CloseIcon from "@mui/icons-material/Close";

type Props = {
	open: boolean;
	setViewModal: Function;
	handleView: (id: string | undefined) => void;
	id?: string;
};

const style = {
	position: "absolute",
	top: "50%",
	left: "50%",
	transform: "translate(-50%, -50%)",
	width: 450,
	borderRadius: 2,
	boxShadow: "0px 4px 20px rgba(0, 0, 0, 0.15)",
	p: 0,
	display: "flex",
	flexDirection: "column",
	alignItems: "center",
};

export default function ViewModal({ open, setViewModal, id }: Props) {
	const { admins } = useSelector((state: RootState) => state.adminReducer);
	const [selectedAdmin, setSelectedAdmin] = useState<any>(null);

	useEffect(() => {
		if (id) {
			const admin = admins.find((admin) => admin.id === id);
			setSelectedAdmin(admin);
		}
	}, [id, admins]);

	return (
		<Modal
			open={open}
			aria-labelledby="modal-title"
			aria-describedby="modal-description"
		>
			<Box sx={style}>
				<Box
					sx={{
						display: "flex",
						justifyContent: "space-between",
						alignItems: "center",
						backgroundColor: "#000000",
						color: "#D0E1E9",
						padding: "20px 24px",
						borderRadius: "10px 10px 0 0",
						width: "100%",
						boxSizing: "border-box",
					}}
				>
					<Typography
						id="modal-title"
						variant="h5"
						fontWeight="bold"
						sx={{ width: "100%" }}
					>
						<Box
							sx={{
								display: "flex",
								alignItems: "center",
								justifyContent: "space-between",
								width: "100%",
							}}
						>
							<Box
								sx={{
									flex: 1,
									textAlign: "center",
									color: "#D0E1E9",
								}}
							>
								{selectedAdmin?.name || "Admin"}'s Details
							</Box>
							<Box
								onClick={() => setViewModal(false)}
								sx={{
									cursor: "pointer",
									display: "flex",
									alignItems: "center",
									color: "#D0E1E9",
								}}
							>
								<CloseIcon />
							</Box>
						</Box>
					</Typography>
				</Box>
				{/* <Divider sx={{ width: "100%" }} /> */}
				<Box
					sx={{
						backgroundColor: "#b5c4cb",
						padding: "20px",
						borderRadius: "0 0 10px 10px", // Rounded bottom corners
					}}
				>
					{selectedAdmin ? (
						<Grid container spacing={3} sx={{ width: "100%" }}>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Name:</strong>
									<Typography variant="body2" color="#454545">
										{selectedAdmin.name}
									</Typography>
								</Typography>
							</Grid>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Phone:</strong>
									<Typography variant="body2" color="#454545">
										{selectedAdmin.phone}
									</Typography>
								</Typography>
							</Grid>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Email:</strong>
									<Typography variant="body2" color="#454545">
										{selectedAdmin.email}
									</Typography>
								</Typography>
							</Grid>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Address:</strong>

									<Typography variant="body2" color="#454545">
										{selectedAdmin?.Admins?.[0]
											?.registeredAddress ?? "N/A"}
									</Typography>
								</Typography>
							</Grid>
						</Grid>
					) : (
						<Typography align="center" color="#454545">
							No admin found with this ID
						</Typography>
					)}
					<Box
						sx={{
							display: "flex",
							justifyContent: "flex-end",
							mt: 3,
						}}
					>
						<Button
							onClick={() => setViewModal(false)}
							sx={{
								backgroundColor: "#000000",
								color: "#D0E1E9",
								borderRadius: "8px",
								fontSize: "16px",
								width: "117px",
								"&:hover": { backgroundColor: "#454545" },
							}}
						>
							Close
						</Button>
					</Box>
				</Box>
			</Box>
		</Modal>
	);
}
