import React, { useEffect } from "react";
import { Box, Button, Typography, Modal } from "@mui/material";
import CloseIcon from "@mui/icons-material/Close";
import { useForm, Controller } from "react-hook-form";
import { CustomIconButton, CustomTextField } from "../AddUserModal/styled.css";

interface EditAdminModalProps {
	open: boolean;
	handleClose: () => void;
	handleUpdate: (
		id: number,
		name: string,
		email: string,
		phone: string,
		registeredAddress: string
	) => void;
	editRow: any;
}

interface FormData {
	name: string;
	email: string;
	phone: string;
	registeredAddress: string;
}

const EditAdminModal: React.FC<EditAdminModalProps> = ({
	open,
	handleClose,
	handleUpdate,
	editRow,
}) => {
	const {
		control,
		handleSubmit,
		formState: { errors },
		setValue,
		reset,
	} = useForm<FormData>({
		defaultValues: {
			name: "",
			email: "",
			phone: "",
			registeredAddress: "",
		},
	});

	useEffect(() => {
		if (editRow) {
			setValue("name", editRow.name);
			setValue("email", editRow.email);
			setValue("phone", editRow.phone);
			setValue(
				"registeredAddress",
				editRow.Admins?.[0]?.registeredAddress || ""
			);
		} else {
			reset();
		}
	}, [editRow, setValue, reset]);


	const onSubmit = (data: FormData) => {
		if (editRow) {
			handleUpdate(
				editRow.id,
				data.name,
				data.email,
				data.phone,
				data.registeredAddress
			);
		}
		handleClose();
		reset();
	};


	return (
		<Modal
			open={open}
			onClose={(e, reason) => {
				if (reason === "backdropClick") return;
				handleClose();
			}}
			aria-labelledby="edit-admin-modal"
		>
			<Box
				component="form"
				onSubmit={handleSubmit(onSubmit)}
				sx={{
					position: "absolute",
					top: "50%",
					left: "50%",
					transform: "translate(-50%, -50%)",
					width: 400,
					bgcolor: "background.paper",
					boxShadow: 24,
					p: 3,
					borderRadius: 2,
				}}
			>
				{/* Header */}
				<Box
					sx={{
						display: "flex",
						justifyContent: "space-between",
						alignItems: "center",
					}}
				>
					<Typography variant="h6" fontWeight={600}>
						Edit Admin
					</Typography>
					<CustomIconButton onClick={handleClose}>
						<CloseIcon />
					</CustomIconButton>
				</Box>

				{/* Horizontal Line */}
				<Box sx={{ borderBottom: "1px solid #ddd", my: 2 }} />

				{/* Input Fields */}
				<Box sx={{ display: "flex", flexDirection: "column", gap: 2 }}>
					{/* Name */}
					<Box sx={{ display: "flex", flexDirection: "column" }}>
						<Typography variant="body2" fontWeight={500} mb={0.5}>
							Full Name
						</Typography>
						<Controller
							name="name"
							control={control}
							rules={{ required: "Name is required" }}
							render={({ field }) => (
								<CustomTextField
									{...field}
									fullWidth
									placeholder="Enter Full Name"
									size="small"
									error={!!errors.name}
									helperText={errors.name?.message}
								/>
							)}
						/>
					</Box>

					{/* Email */}
					<Box sx={{ display: "flex", flexDirection: "column" }}>
						<Typography variant="body2" fontWeight={500} mb={0.5}>
							Email
						</Typography>
						<Controller
							name="email"
							control={control}
							rules={{ required: "Email is required" }}
							render={({ field }) => (
								<CustomTextField
									{...field}
									type="email"
									fullWidth
									placeholder="Enter Email"
									size="small"
									error={!!errors.email}
									helperText={errors.email?.message}
								/>
							)}
						/>
					</Box>

					{/* Phone */}
					<Box sx={{ display: "flex", flexDirection: "column" }}>
						<Typography variant="body2" fontWeight={500} mb={0.5}>
							Phone Number
						</Typography>
						<Controller
							name="phone"
							control={control}
							rules={{ required: "Phone number is required" }}
							render={({ field }) => (
								<CustomTextField
									{...field}
									fullWidth
									placeholder="Enter Phone Number"
									size="small"
									error={!!errors.phone}
									helperText={errors.phone?.message}
								/>
							)}
						/>
					</Box>

					{/* Address */}
					<Box sx={{ display: "flex", flexDirection: "column" }}>
						<Typography variant="body2" fontWeight={500} mb={0.5}>
							Address
						</Typography>
						<Controller
							name="registeredAddress"
							control={control}
							rules={{ required: "Address is required" }}
							render={({ field }) => (
								<CustomTextField
									{...field}
									fullWidth
									placeholder="Enter Address"
									size="small"
									error={!!errors.registeredAddress}
									helperText={
										errors.registeredAddress?.message
									}
								/>
							)}
						/>
					</Box>
				</Box>

				{/* Submit Button */}
				<Box
					sx={{ display: "flex", justifyContent: "flex-end", mt: 3 }}
				>
					<Button
						type="submit"
						sx={{
							backgroundColor: "#52ACDF",
							color: "white",
							borderRadius: "8px",
							width: "117px",
							"&:hover": { backgroundColor: "#439BC1" },
						}}
					>
						Update Admin
					</Button>
				</Box>
			</Box>
		</Modal>
	);
};

export default EditAdminModal;
