import React, { useState, useEffect, useRef } from "react";
import { Box, Button } from "@mui/material";
import { DataGrid, GridToolbarContainer } from "@mui/x-data-grid";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faEllipsisV } from "@fortawesome/free-solid-svg-icons";
import "./MenuMaintance.css";
const api = process.env.REACT_APP_API_BASE_URL;
function CustomToolbar({ apiRef, handleThreeDotsClick, handleModal }) {
  const handleGoToPage1 = () => {
    if (apiRef.current) {
      apiRef.current.setPage(1);
    }
  };
  return (
    
      
      
    
  );
}
function MenuMaintenance() {
  const [menuItems, setMenuItems] = useState([]);
  const [selectedMenuItem, setSelectedMenuItem] = useState(null);
  const [isModalOpen, setIsModalOpen] = useState(false);
  const apiRef = useRef(null);
  useEffect(() => {
    const fetchData = async () => {
      const token = localStorage.getItem("token");
      try {
        const response = await fetch(`${api}/api1/submenu1`, {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        });
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();
        // Flatten the nested subMenus array
        const flattenedData = data.flatMap((menuItem) => [
          menuItem,
          ...menuItem.subMenus,
        ]);
        // Set unique IDs for each menu item
        const menuItemsWithIds = flattenedData.map((menuItem, index) => ({
          ...menuItem,
          id: index + 1,
        }));
        setMenuItems(menuItemsWithIds);
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    };
    fetchData();
  }, []);
  const handleThreeDotsClick = (menuItemId) => {
    setSelectedMenuItem(menuItemId === selectedMenuItem ? null : menuItemId);
  };
  const columns = [
    {
      field: "menuItemId",
      headerName: "Menu Item ID",
      width: 200,
      headerClassName: "custom-header",
      cellClassName: "custom-cell",
    },
    {
      field: "menuItemDesc",
      headerName: "Menu Item Description",
      width: 250,
      headerClassName: "custom-header",
      cellClassName: "custom-cell",
    },
    {
      field: "moduleName",
      headerName: "Module Name",
      width: 200,
      headerClassName: "custom-header",
      cellClassName: "custom-cell",
    },
    {
      field: "main_menu_action_name",
      headerName: "Main Menu Action",
      width: 200,
      headerClassName: "custom-header",
      cellClassName: "custom-cell",
    },
    {
      field: "main_menu_icon_name",
      headerName: "Main Menu Icon",
      width: 200,
      headerClassName: "custom-header",
      cellClassName: "custom-cell",
    },
    {
      field: "actions",
      headerName: "Actions",
      width: 150,
      renderCell: ({ row }) => (
        
           handleThreeDotsClick(row.menuItemId)}
          >
            
          
          {selectedMenuItem === row.menuItemId && (
            
              {/* Implement your actions buttons here */}
            
          )}
        
 
      ),
    },
  ];
  return (
    
      
        Menu Maintenance
      
      
         (
               setIsModalOpen(true)}
              />
            ),
          }}
          pageSize={10}
          onGridReady={(gridApi) => {
            apiRef.current = gridApi;
          }}
          sx={{
            "& .MuiDataGrid-columnHeaders": {
              backgroundColor: "rgba(107, 114, 128, 0.5)", // Tailwind CSS bg-gray-400 with opacity
            },
            "& .MuiDataGrid-columnHeaderTitle": {
              fontWeight: "bold",
            },
          }}
          className=" border border-gray-200 shadow-lg rounded-lg bg-gray-400"
        />
      
      {/* Your modals and other components */}
      {isModalOpen && (
        
          
            
Modal Title
            {/* Modal content here */}
            
          
         
      )}
    
 
  );
}
export default MenuMaintenance;