import { Button, CircularProgress, Typography } from "@mui/material";
import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { Grid } from "../../components/grid/Grid";
import { getRoleFromUrl } from "../../utils/getRoleFromUrl";

export const KillProcessAllocationStats = ({ province }) => {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);
  const { authToken } = useSelector((state) => state.userSlice);

  useEffect(() => {
    fetch(
      `${province}province_kill_request_killing_process/?role=${getRoleFromUrl()}`,
      {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      }
    )
      .then((response) => response.json())
      .then((data) => {
        setData(data);
        setLoading(false);
      })
      .catch((error) => {
        console.error("Error fetching data:", error);
        setLoading(false);
      });
  }, []);

  return (
    <>
      {loading ? (
        <CircularProgress />
      ) : (
        <Grid container direction="column" className="stats-wrap">
          <Typography my={2} className="stats-title" fontWeight={"bold"}>
            تخصیصات
          </Typography>
          <Grid
            container
            className="stats-content"
            direction="column"
            alignItems="start"
          >
            <Typography>
              تعداد کشتارگاه: {data?.kill_house_quantity?.toLocaleString()} واحد
            </Typography>
            <Typography>
              تعداد: {data?.province_kill_request_quantity?.toLocaleString()}{" "}
              قطعه
            </Typography>
            <Typography>
              وزن : {data?.province_kill_request_index_weight?.toLocaleString()}{" "}
              کیلوگرم
            </Typography>
            <a href={`${province}allocated_excel/`} style={{ width: "100%" }}>
              <Button
                fullWidth
                variant="outlined"
                style={{ marginTop: "10px" }}
              >
                دانلود اکسل
              </Button>
            </a>
          </Grid>
        </Grid>
      )}
    </>
  );
};
