import React, { useMemo } from "react";
import { Box, Button, Card, Grid, MenuItem, TextField, Typography } from "@mui/material";
import { useFormik } from "formik";
import SaveAltIcon from "@mui/icons-material/SaveAlt";
import CloseIcon from "@mui/icons-material/Close";
import { useCreateTaxMutation } from "../../../Api/attoDeskApi";
import { useNotifier } from "../../../Core/Notifier";
import { appColor } from "../../../theme/appColor";
import { useStyles } from "../../../theme/styles";

const Taxes: React.FC = () => {
  const classes = useStyles();
  const [newTax, { isLoading }] = useCreateTaxMutation();
  const { showErrorMessage, showMessage } = useNotifier();

  const formik = useFormik({
    initialValues: {
      taxName: "",
      taxType: "",
      applyTo: "",
      percentage: "",
      taxCode: "",
    },
    onSubmit: async (values, { resetForm }) => {
      try {
        const temData = {
          taxName: values.taxName,
          taxType: values.taxType,
          applyTo: values.applyTo,
          percentage: values.percentage,
          taxCode: values.taxCode,
        };

        const addCompanyResponse = await newTax(temData).unwrap();
        if (!addCompanyResponse.status) {
          showErrorMessage(addCompanyResponse.message);
        } else {
          showMessage("Tax Created successfully");
          resetForm();
        }
      } catch (error) {
        showErrorMessage("Something went wrong");
      }
    },
  });

  const formValid = useMemo(() => {
    return formik.values.taxName === "" ||
      formik.values.taxName === undefined ||
      formik.values.taxCode === "" ||
      formik.values.taxCode === undefined
      ? false
      : true;
  }, [formik]);

  return (
    <form onSubmit={formik.handleSubmit}>
      <Grid container>
        <Grid item lg={12} md={12} sm={12} xs={12} className={classes.grid1}>
          <Card
            sx={{
              p: 2,
              maxWidth: 900,
              boxShadow: "none",
              pb: 2,
            }}
          >
            <Grid container justifyContent="space-between">
              <Grid
                item
                lg={12}
                md={12}
                sm={12}
                xs={12}
                sx={{ borderBottom: 1 }}
                className={classes.cardTitleGrid}
              >
                <Typography className={classes.cardTitleTypography}>
                  New Tax
                </Typography>
              </Grid>

              <Grid item lg={12} md={10} sm={12} xs={12} py={2}>
                <Grid container direction="row" alignItems="center" spacing={2}>
                  <Grid item lg={3} md={3} sm={12} xs={12}>
                    <Typography
                      variant="subtitle1"
                      className={classes.typography}
                    >
                      Name
                    </Typography>
                  </Grid>
                  <Grid item lg={9} md={9} sm={12} xs={12}>
                    <TextField
                      placeholder="Enter Tax Name"
                      size="small"
                      {...formik.getFieldProps("taxName")}
                      sx={{ width: "100%" }}
                      InputProps={{
                        sx: {
                          fontSize: 14,
                        },
                      }}
                      InputLabelProps={{
                        sx: {
                          fontSize: 14,
                        },
                      }}
                    />
                  </Grid>
                </Grid>

                <Grid
                  container
                  direction="row"
                  alignItems="center"
                  spacing={2}
                  sx={{ mt: 1 }}
                >
                  <Grid item lg={3} md={3} sm={12} xs={12}>
                    <Typography
                      variant="subtitle1"
                      className={classes.typography}
                    >
                      Tax Code
                    </Typography>
                  </Grid>
                  <Grid item lg={9} md={9} sm={12} xs={12}>
                    <TextField
                      placeholder="Enter Tax Code"
                      size="small"
                      {...formik.getFieldProps("taxCode")}
                      sx={{ width: "100%" }}
                      InputProps={{
                        sx: {
                          fontSize: 14,
                        },
                      }}
                      InputLabelProps={{
                        sx: {
                          fontSize: 14,
                        },
                      }}
                    />
                  </Grid>
                </Grid>
                <Grid
                  container
                  direction="row"
                  alignItems="center"
                  spacing={2}
                  sx={{ mt: 1 }}
                >
                  <Grid item lg={3} md={3} sm={12} xs={12}>
                    <Typography
                      variant="subtitle1"
                      className={classes.typography}
                    >
                      Tax Type
                    </Typography>
                  </Grid>
                  <Grid item lg={9} md={9} sm={12} xs={12}>
                    <TextField
                      select
                      size="small"
                      sx={{ width: "100%" }}
                      {...formik.getFieldProps("taxType")}
                      defaultValue=""
                      InputLabelProps={{ shrink: true }}
                    >
                      <MenuItem value="" disabled color="gray">
                        Select an option
                      </MenuItem>
                      <MenuItem value="Percentage">Percentage</MenuItem>
                    </TextField>
                  </Grid>
                </Grid>
                <Grid
                  container
                  direction="row"
                  alignItems="center"
                  spacing={2}
                  sx={{ mt: 1 }}
                >
                  <Grid item lg={3} md={3} sm={12} xs={12}>
                    <Typography
                      variant="subtitle1"
                      className={classes.typography}
                    >
                      Apply To
                    </Typography>
                  </Grid>
                  <Grid item lg={9} md={9} sm={12} xs={12}>
                    <TextField
                      select
                      size="small"
                      sx={{ width: "100%" }}
                      {...formik.getFieldProps("applyTo")}
                      defaultValue=""
                      InputLabelProps={{ shrink: true }}
                    >
                      <MenuItem value="" disabled color="gray">
                        Select Apply To
                      </MenuItem>
                      <MenuItem value="Goverment">Goverment</MenuItem>
                        <MenuItem value="Private">Private</MenuItem>
                    </TextField>
                  </Grid>
                </Grid>
                <Grid
                  container
                  direction="row"
                  alignItems="center"
                  spacing={2}
                  sx={{ mt: 1 }}
                >
                  <Grid item lg={3} md={3} sm={12} xs={12}>
                    <Typography
                      variant="subtitle1"
                      className={classes.typography}
                    >
                      Percentage
                    </Typography>
                  </Grid>
                  <Grid item lg={9} md={9} sm={12} xs={12}>
                    <TextField
                      placeholder="Enter Percentage"
                      size="small"
                      {...formik.getFieldProps("percentage")}
                      sx={{ width: "100%" }}
                      InputProps={{
                        sx: {
                          fontSize: 14,
                        },
                      }}
                      InputLabelProps={{
                        sx: {
                          fontSize: 14,
                        },
                      }}
                    />
                  </Grid>
                </Grid>
              </Grid>
              <Grid
                item
                lg={12}
                md={12}
                sm={12}
                xs={12}
                sx={{ display: "flex", justifyContent: "flex-end" }}
              >
                <Button
                  variant="contained"
                  startIcon={<CloseIcon />}
                  className={classes.cancelButton}
                  onClick={() => formik.resetForm()}
                >
                  Cancel
                </Button>
                <Box m={0.5}></Box>
                <Button
                  variant="contained"
                  startIcon={<SaveAltIcon />}
                  className={classes.saveButton}
                  onClick={() => formik.handleSubmit()}
                  disabled={!formValid || isLoading}
                >
                  Save
                </Button>
              </Grid>
            </Grid>
          </Card>
        </Grid>
      </Grid>
    </form>
  );
};

export default Taxes;
