import { Autocomplete, Button, TextField } from "@mui/material";
import React from "react";
import { useEffect } from "react";
import { useState } from "react";
import { useDispatch } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { PropTypes } from "prop-types";

import {
  DRAWER,
  LOADING_END,
  LOADING_START,
} from "../../../../lib/redux/slices/appSlice";
import { provinceGetKillHouses } from "../../../province/services/province-get-kill-houses";
import { inspectorUpdateUserProfile } from "../../services/inspector-update-user-profile";
import { useContext } from "react";
import { AppContext } from "../../../../contexts/AppContext";
import { provinceGetUserByKey } from "../../../province/services/province-get-user-by-key";
import { SPACING } from "../../../../data/spacing";

export const InspectorEditKillHouseVet = ({ id }) => {
  const [openNotif] = useContext(AppContext);

  const dispatch = useDispatch();
  const [killHouseData, setKillHouseData] = useState();
  const [killHouseKey, setKillHouseKey] = useState();
  useEffect(() => {
    dispatch(LOADING_START());
    dispatch(provinceGetKillHouses()).then((r) => {
      setKillHouseData(r.payload.data);
      dispatch(LOADING_END());
    });
  }, []);

  return (
    <>
      <Grid>
        <Grid container gap={SPACING.SMALL} direction="column">
          <Grid>
            <Autocomplete
              disablePortal
              id="province"
              options={
                killHouseData
                  ? killHouseData.map((i) => ({
                      id: i.key,
                      label: i.name,
                    }))
                  : []
              }
              onChange={(e, value) => {
                setKillHouseKey(value.id);
              }}
              renderInput={(params) => (
                <TextField {...params} label="انتخاب کشتارگاه" />
              )}
            />
          </Grid>

          <Button
            disabled={!killHouseKey}
            fullWidth
            color="primary"
            variant="contained"
            onClick={() => {
              dispatch(LOADING_START());
              dispatch(
                inspectorUpdateUserProfile({
                  type: "KillHouseVet",
                  kill_house_key: killHouseKey,
                  vet_key: id,
                })
              ).then((r) => {
                if (r.error) {
                  if (r.error.message.includes("403")) {
                    openNotif({
                      vertical: "top",
                      horizontal: "center",
                      msg: "مشکلی پیش آمده است!",
                      severity: "error",
                    });
                  } else {
                    openNotif({
                      vertical: "top",
                      horizontal: "center",
                      msg: "مشکلی پیش آمده است!",
                      severity: "error",
                    });
                  }
                } else {
                  openNotif({
                    vertical: "top",
                    horizontal: "center",
                    msg: "عملیات با موفقیت انجام شد.",
                    severity: "success",
                  });
                }
                dispatch(LOADING_END());
                dispatch(provinceGetUserByKey(id));
                dispatch(
                  DRAWER({ right: false, bottom: false, content: null })
                );
              });
            }}
          >
            ارسال اطلاعات
          </Button>
        </Grid>
      </Grid>
    </>
  );
};

InspectorEditKillHouseVet.propTypes = {
  id: PropTypes.any,
};
