import React, { useEffect } from "react";
import { Grid } from "../../../../components/grid/Grid";
import ResponsiveTable from "../../../../components/responsive-table/ResponsiveTable";
import { SlaughterShowProducts } from "../slaughter-show-products/SlaughterShowProducts";
import { useSelector, useDispatch } from "react-redux";
import { SPACING } from "../../../../data/spacing";
import { Button } from "@mui/material";
import { DRAWER } from "../../../../lib/redux/slices/appSlice";
import { SlaughterSubmitOperations } from "../slaughter-submit-operations/SlaughterSubmitOperations";
import { fetchSlaughterBroadcastAndProducts } from "../../services/handle-fetch-slaughter-products";
import { checkPathStartsWith } from "../../../../utils/checkPathStartsWith";

export const SlaughterInventorySummary = ({ priceInfo }) => {
  const { distributionInfo, slaughterProducts } = useSelector(
    (state) => state.slaughterSlice
  );
  const selectedSubUser = useSelector(
    (state) => state.userSlice.selectedSubUser
  );
  const dispatch = useDispatch();

  const updateTable = () => {
    dispatch(
      fetchSlaughterBroadcastAndProducts({
        role_key: checkPathStartsWith("slaughter")
          ? selectedSubUser?.key || ""
          : "",
      })
    );
  };

  useEffect(() => {
    updateTable();
  }, [selectedSubUser?.key]);

  return (
    <Grid container width="100%" gap={SPACING.SMALL} mb={2}>
      <Button
        disabled={!slaughterProducts}
        variant="contained"
        color="primary"
        size="large"
        onClick={() => {
          dispatch(
            DRAWER({
              right: !(window.innerWidth <= 600),
              bottom: window.innerWidth <= 600,
              title: "ثبت عملیات",
              size: window.innerWidth <= 600 ? "auto" : 450,
              content: (
                <SlaughterSubmitOperations
                  updateTable={updateTable}
                  priceInfo={priceInfo}
                  slaughterProducts={slaughterProducts}
                />
              ),
            })
          );
        }}
      >
        ثبت عملیات
      </Button>
      <SlaughterShowProducts />
      <ResponsiveTable
        title="اطلاعات پخش"
        noPagination
        isDashboard
        data={[
          [
            distributionInfo?.totalGovernmentalInputWeight?.toLocaleString(),
            distributionInfo?.totalFreeInputWeight?.toLocaleString(),
            distributionInfo?.totalGovernmentalOutputWeight?.toLocaleString(),
            distributionInfo?.totalFreeOutputWeight?.toLocaleString(),
            distributionInfo?.totalKillHouseAllocationsWeight?.toLocaleString(),
            distributionInfo?.totalKillHouseFreeSale_barCarcassesWeight?.toLocaleString(),
            distributionInfo?.segmentationsWeight?.toLocaleString(),
            distributionInfo?.coldHouseAllocationsWeight?.toLocaleString(),
            distributionInfo?.totalGovernmentalRemainWeight?.toLocaleString(),
            distributionInfo?.totalFreeRemainWeight?.toLocaleString(),
          ],
        ]}
        columns={[
          "وزن دولتی (کیلوگرم)",
          "وزن آزاد (کیلوگرم)",
          "فروش دولتی (کیلوگرم)",
          "فروش آزاد (کیلوگرم)",
          "فروش و توزیع داخل استان (کیلوگرم)",
          "فروش و توزیع خارج استان (کیلوگرم)",
          "وزن قطعه بندی (کیلوگرم)",
          "وزن انجماد (کیلوگرم)",
          "مانده دولتی (کیلوگرم)",
          " مانده آزاد  (کیلوگرم)",
        ]}
      />
      <ResponsiveTable
        title="تعهدات"
        noPagination
        isDashboard
        data={[
          [
            distributionInfo?.totalCommitmentSellingInProvinceGovernmentalWeight?.toLocaleString(),
            distributionInfo?.totalSellingInProvinceGovernmentalWeight?.toLocaleString(),
            distributionInfo?.totalCommitmentSellingInProvinceGovernmentalRemainWeight?.toLocaleString(),
            distributionInfo?.totalCommitmentSellingInProvinceFreeWeight?.toLocaleString(),
            distributionInfo?.totalSellingInProvinceFreeWeight?.toLocaleString(),
            distributionInfo?.totalCommitmentSellingInProvinceFreeRemainWeight?.toLocaleString(),
          ],
        ]}
        columns={[
          "تعهد دولتی توزیع داخل استان",
          "توزیع دولتی داخل استان",
          "باقیمانده تعهد دولتی توزیع داخل استان",
          "تعهد آزاد توزیع داخل استان",
          "توزیع آزاد داخل استان",
          "باقیمانده تعهد آزاد توزیع داخل استان",
        ]}
        allColors={{ color: "green", text: "#332a3d" }}
      />
    </Grid>
  );
};
