import ThumbUpAltIcon from "@mui/icons-material/ThumbUpAlt";
import ThumbDownIcon from "@mui/icons-material/ThumbDown";
import {Box, IconButton, Tooltip} from "@mui/material";
import {useCallback, useState} from "react";
import ConfirmForm from "./Form/ConfirmForm";
import RejectForm from "./Form/RejectForm";
import {useTranslations} from "next-intl";
import axios from "axios";
import Notifications from "@/core/components/notifications";
import useUser from "@/lib/app/hooks/useUser";
import useDirection from "@/lib/app/hooks/useDirection";

const TableRowActions = ({row, mutate, fetchUrl}) => {
    const t = useTranslations();
    const {token} = useUser();
    const {directionApp} = useDirection();

    // Confirm
    const [openConfirmDialog, setOpenConfirmDialog] = useState(false);
    const handleCloseConfirmDialog = () => {
        setOpenConfirmDialog(false);
    };
    const confirmData = useCallback((url, rowID, values) => {
        Notifications(directionApp, t, undefined)
        axios
            .post(`${url}/${rowID}`, values, {
                headers: {authorization: `Bearer ${token}`},
            })
            .then((response) => {
                Notifications(directionApp, t, response);
                mutate(fetchUrl)
            })
            .catch((error) => {
                Notifications(directionApp, t, error.response);
            });
    }, []);
    // End Confirm

    // Reject
    const [openRejectDialog, setOpenRejectDialog] = useState(false);
    const handleCloseRejectDialog = () => {
        setOpenRejectDialog(false);
    };

    const rejectData = useCallback((url, rowID, values) => {
        Notifications(directionApp, t)
        axios
            .post(`${url}/${rowID}`, values, {
                headers: {authorization: `Bearer ${token}`},
            })
            .then((response) => {
                Notifications(directionApp, t, response);
                mutate(fetchUrl)
            })
            .catch((error) => {
                Notifications(directionApp, t, error.response);
            });
    }, []);
    // End Reject

    return (
        <Box sx={{display: "flex", flexWrap: "nowrap", gap: "8px"}}>
            <Tooltip title={t("ConfirmDialog.confirm")}>
                <IconButton
                    color="primary"
                    onClick={() => {
                        setOpenConfirmDialog(true)
                    }}
                >
                    <ThumbUpAltIcon/>
                </IconButton>
            </Tooltip>
            <ConfirmForm
                rowId={row.getValue("id")}
                open={openConfirmDialog}
                handleClose={handleCloseConfirmDialog}
                confirmData={confirmData}
            />
            <Tooltip title={t("RejectDialog.reject")}>
                <IconButton color="primary" onClick={() => setOpenRejectDialog(true)}>
                    <ThumbDownIcon/>
                </IconButton>
            </Tooltip>
            <RejectForm
                rowId={row.getValue("id")}
                open={openRejectDialog}
                handleClose={handleCloseRejectDialog}
                rejectData={rejectData}
            />
        </Box>
    );
};

export default TableRowActions;
