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

const ProductBrand: React.FC = () => {
  const classes = useStyles();
  const [newProductBrand, { isLoading }] = useCreateProductBrandMutation();
  const { showErrorMessage, showMessage } = useNotifier();

  const formik = useFormik({
    initialValues: {
      productBrandName: "",
      isActive: false,
    },
    onSubmit: async (values, { resetForm }) => {
      try {
        const temData = {
          productBrandName: values.productBrandName,
          isActive: values.isActive,
        };

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

  const formValid = useMemo(() => {
    return formik.values.productBrandName === "" ||
      formik.values.productBrandName === 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 Product Brand
                </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 Product Brand Name"
                      size="small"
                      {...formik.getFieldProps("productBrandName")}
                      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}
                    >
                      Active
                    </Typography>
                  </Grid>
                  <Grid item lg={9} md={9} sm={12} xs={12}>
                    {/* <IOSSwitch
                      color="primary"
                      sx={{ mr: 2 }}
                      {...formik.getFieldProps("isActive")}
                      checked={formik.values.isActive}
                    /> */}
                    <CustomSwitch
                      color="primary"
                      sx={{ mr: 2 }}
                      formik={formik}
                      name="isActive"
                    />
                  </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 ProductBrand;
