import React, { useEffect, useState } from "react";
import { Tab, Tabs } from "@mui/material";
import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";
import { Grid } from "../../../../components/grid/Grid";
import ResponsiveTable from "../../../../components/responsive-table/ResponsiveTable";
import {
  provinceJahadGetDistributionInfoService,
  provinceJahadGetInventoryDataService,
} from "../../services/province-jahad-get-distribution-info";
import { SPACING } from "../../../../data/spacing";
import { useDispatch } from "react-redux";
import { ProvinceJahadBranDistributionsInventoryAllocations } from "../province-jahad-bran-distributions-inventory-allocation/ProvinceJahadBranDistributionsInventoryAllocation";
import { ProvinceJahadBranDistributionsUnionAllocations } from "../province-jahad-bran-distributions-union-allocation/ProvinceJahadBranDistributionsUnionAllocation";

export const ProvinceJahadBranDistributionsAllocation = ({ product }) => {
  const [dashboardData, setDashboardData] = useState([]);
  const [inventoryData, setInventoryData] = useState([]);

  const dispatch = useDispatch();
  const getDashboardData = () => {
    dispatch(
      provinceJahadGetDistributionInfoService({
        name: product?.key,
        search: "filter",
        role: getRoleFromUrl(),
      })
    ).then((r) => {
      setDashboardData(r.payload.data);
    });
    dispatch(
      provinceJahadGetInventoryDataService({
        name: product?.key,
        search: "filter",
        role: getRoleFromUrl(),
      })
    ).then((r) => {
      setInventoryData(r.payload.data);
    });
  };

  // const updateTable = (event) => {
  //   event.preventDefault();
  //   if (childRef.current) {
  //     childRef.current.handleSubmit(event);
  //   }
  // };

  useEffect(() => {
    getDashboardData();
  }, [dispatch]);

  const [selectedTab, setSelectedTab] = useState(0);

  const handleTabChange = (event, newValue) => {
    setSelectedTab(newValue);
  };

  return (
    <Grid container xs={12} justifyContent="center" alignItems="center" gap={2}>
      <Grid container mt={2} mb={4} isDashboard xs={12}>
        <ResponsiveTable
          noPagination
          isDashboard
          columns={
            getRoleFromUrl() === "LiveStockProvinceJahad"
              ? [
                  "ورودی به انبار",
                  "تخصیص جهاد به اتحادیه",
                  "تخصیص جهاد به تعاونی ها",
                  "مجموع تخصیصات",
                  "مجموع تخصیص اتحادیه به تعاونی",
                  "مجموع تخصیص به تعاونی ها",
                  "توزیع به دامداران",
                  "مجموع توزیع به دامداران",
                  "مانده انبار جهاد استان",
                  "مانده انبار اتحادیه",
                  "مانده انبار تعاونی ها",
                ]
              : getRoleFromUrl() === "Union"
              ? [
                  "ورودی به انبار",
                  "تخصیص به تعاونی ها",
                  // "توزیع مستقیم به دامداران",
                  "مجموع تخصیص و توزیع",
                  "مانده انبار",
                ]
              : [
                  "سهمیه دریافتی",
                  "مانده سهمیه دریافتی",
                  "ورودی به انبار",
                  "توزیع به دامداران",
                  "مانده انبار",
                ]
          }
          data={
            getRoleFromUrl() === "LiveStockProvinceJahad"
              ? [
                  [
                    dashboardData?.totalWeight?.toLocaleString(),
                    dashboardData?.jahadToUnion?.toLocaleString(),
                    dashboardData?.jahatToCooperative?.toLocaleString(),
                    dashboardData?.allocationCount?.toLocaleString(),
                    dashboardData?.unionToCooperative?.toLocaleString(),
                    parseInt(
                      dashboardData?.unionToCooperative +
                        dashboardData?.jahatToCooperative
                    )?.toLocaleString(),
                    0,
                    0,
                    dashboardData?.totalRemainWeightJahad?.toLocaleString(),
                    dashboardData?.totalRemainWeightUnion?.toLocaleString(),
                    dashboardData?.totalRemainWeightCooperative?.toLocaleString(),
                  ],
                ]
              : getRoleFromUrl() === "Union"
              ? [
                  [
                    dashboardData?.jahadToUnion?.toLocaleString(),
                    dashboardData?.unionToCooperative?.toLocaleString(),
                    // 0,
                    parseInt(
                      0 + dashboardData?.unionToCooperative
                    )?.toLocaleString(),
                    dashboardData?.totalRemainWeight?.toLocaleString(),
                  ],
                ]
              : [
                  [
                    dashboardData?.roleProductCooperativeReceiptWeight?.toLocaleString(),
                    parseInt(
                      dashboardData?.roleProductCooperativeReceiptWeight -
                        dashboardData?.totalWeight
                    )?.toLocaleString(),
                    dashboardData?.totalWeight?.toLocaleString(),
                    0,
                    dashboardData?.totalRemainWeight?.toLocaleString(),
                  ],
                ]
          }
          title={`اطلاعات انبار و تخصیصات ${product?.label}`}
        />
      </Grid>

      <Grid
        container
        justifyContent="center"
        alignItems="center"
        mb={SPACING.MEDIUM}
        mt={SPACING.SMALL}
        xs={12}
      >
        <Tabs
          scrollButtons="auto"
          variant="scrollable"
          allowScrollButtonsMobile
          value={selectedTab}
          onChange={handleTabChange}
        >
          <Tab label="انبار" />
          <Tab label="تخصیصات" />
        </Tabs>
      </Grid>
      {selectedTab === 0 && (
        <ProvinceJahadBranDistributionsUnionAllocations
          inventoryData={inventoryData}
          getDashboardData={getDashboardData}
          product={product}
          maxAllow={dashboardData?.roleProductCooperativeReceiptWeight}
        />
      )}

      {selectedTab === 1 && (
        <ProvinceJahadBranDistributionsInventoryAllocations
          inventoryData={inventoryData}
          getDashboardData={getDashboardData}
          maxAllow={dashboardData?.roleProductCooperativeReceiptWeight}
          product={product}
        />
      )}
    </Grid>
  );
};
