import { Box, Button } from "@mui/material";
import React, { useState } from "react";
import NewCategory from "./Components/NewCategory";
import NewCategoryTable from "./Components/NewCategoryTable";
import AddIcon from "@mui/icons-material/Add";
import TableViewIcon from "@mui/icons-material/TableView";
import { useStyles } from "../../theme/styles";

const Product: React.FC = () => {
  const classes = useStyles();
  const [showNewCategory, setShowNewCategory] = useState(false);
  const [isDataLoaded, setIsDataLoaded] = useState(false);

  const handleToggle = () => {
    setShowNewCategory(!showNewCategory);
  };

  const handleDataLoaded = () => {
    setIsDataLoaded(true);
  };

  return (
    <Box sx={{ p: 2,}} className={classes.indexBox}>
      {isDataLoaded && (
        <Box
          sx={{
            textAlign: "end",
          }}
        >
          <Button
            variant="contained"
            startIcon={
              showNewCategory ? (
                <TableViewIcon sx={{ fontSize: 40 }} />
              ) : (
                <AddIcon sx={{ fontSize: 40 }} />
              )
            }
            className={classes.indexButton}
            onClick={handleToggle}
          >
            {showNewCategory ? "Category Table" : "New Category"}
          </Button>
        </Box>
      )}
      <Box sx={{ height: "40px" }} />{" "}
      {showNewCategory ? (
        <NewCategory />
      ) : (
        <NewCategoryTable onDataLoaded={handleDataLoaded} />
      )}
    </Box>
  );
};

export default Product;
