import React, { useEffect, useState } from "react";
import { Box, Button, Typography } from "@mui/material";
import AddEditCategoryModal from "../../components/AddEditCategoryModal";
import { useForm } from "react-hook-form";
import CustomTable, { Column } from "../../components/CustomTable";
import { useDispatch, useSelector } from "react-redux";
import { createUser, updateUser, userList } from "../../redux/slices/userSlice";
import { AppDispatch, RootState } from "../../redux/store/store";
import { string } from "prop-types";
import { adminList, updateAdmin } from "../../redux/slices/adminSlice";
import AddUserModal from "../../components/AddUserModel";

export default function UserList() {
	const [modalOpen, setModalOpen] = useState(false);
	const { reset } = useForm();

	const [deleteModal, setDeleteModal] = React.useState<boolean>(false);
	const [viewModal, setViewModal] = React.useState<boolean>(false);
	const [rowData, setRowData] = React.useState<any | null>(null);

	const dispatch = useDispatch<AppDispatch>();

	const users = useSelector((state: RootState) => state.userReducer.users);

	useEffect(() => {
		dispatch(userList());
	}, [dispatch]);

	const handleClickOpen = () => {
		setRowData(null); // Reset row data when opening for new admin
		setModalOpen(true);
	};

	const handleCloseModal = () => {
		setModalOpen(false);
		setRowData(null);
		reset();
	};

	const handleCreate = async (data: {
		name: string;
		email: string;
		phone: string;
		
	}) => {
		try {
			await dispatch(createUser(data));
			await dispatch(userList()); // Refresh the list after creation
			handleCloseModal();
		} catch (error) {
			console.error("Creation failed", error);
		}
	};

	const handleUpdate = async (
		id: string,
		name: string,
		email: string,
		phone: string
	) => {
		try {
			await dispatch(
				updateUser({
					id,
					name,
					email,
					phone,
					
				})
			);
			await dispatch(userList());
		} catch (error) {
			console.error("Update failed", error);
		}
	};

	const categoryColumns: Column[] = [
		{ id: "srno", label: "Sr No" },
		{ id: "name", label: "Name" },
		{ id: "email", label: "Email" },
		{ id: "phone", label: "Phone" },
		// { id: "location", label: "Location" },
		// { id: "managerAssigned", label: "ManagerAssigned" },
		// { id: "vehicle", label: "Vehicle" },

		{ id: "action", label: "Action", align: "center" },
	];

	const categoryRows = users?.length
		? users?.map(function (
				user: {
					id: string;
					name: string;
					email: string;

					phone: string;
					// location?: string;
					// managerAssigned?: string;
					// vehicle?: string;
				},
				index: number
		  ) {
				return {
					id: user?.id,
					srno: index + 1,
					name: user?.name,
					email: user?.email,
					phone: user?.phone,
					// location: user?.location,
					// managerAssigned: user?.managerAssigned,
					// vehicle: user?.vehicle,
				};
		  })
		: [];

	return (
		<>
			<CustomTable
				columns={categoryColumns}
				rows={categoryRows}
				setDeleteModal={setDeleteModal}
				deleteModal={deleteModal}
				setViewModal={setViewModal}
				viewModal={viewModal}
				setRowData={setRowData}
				setModalOpen={setModalOpen}
				tableType="user"
				handleClickOpen={handleClickOpen}
			/>
			<AddUserModal
				open={modalOpen}
				handleClose={handleCloseModal}
				handleCreate={handleCreate}
				handleUpdate={handleUpdate}
				editRow={rowData}
			/>
		</>
	);
}
