import { Typography } from "@mui/material";
import { Grid } from "../../../../components/grid/Grid";
import { SimpleTable } from "../../../../components/simple-table/SimpleTable";
import { SPACING } from "../../../../data/spacing";
import { useContext, useEffect, useState } from "react";
import { SlaughterManageInventoryAllocationOperations } from "../../../slaughter-house/components/slaughter-manage-inventory-allocation-operations/SlaughterManageInventoryAllocationOperations";
import { formatJustDate } from "../../../../utils/formatTime";
import { useDispatch, useSelector } from "react-redux";
import { slaughterManageInventoryAllocationsService } from "../../../slaughter-house/services/salughter-manage-inventory-allocations";
import { AppContext } from "../../../../contexts/AppContext";
import { useParams } from "react-router-dom";

export const ProvinceManageStewardAllocations = () => {
  const dispatch = useDispatch();
  const { id } = useParams();
  const [inventoryAllocationsDataTable, setInventoryAllocationsDataTable] =
    useState([]);

  const { slaughterManageInventoryAllocations } = useSelector(
    (state) => state.slaughterSlice
  );

  const [, , selectedDate1] = useContext(AppContext);

  useEffect(() => {
    dispatch(
      slaughterManageInventoryAllocationsService({
        kill_house_key: id,
        date: selectedDate1,
      })
    );
  }, []);

  useEffect(() => {
    const inventoryAllocationsData = slaughterManageInventoryAllocations?.map(
      (item, i) => {
        let sellType, sellerType;
        if (item.sellerType === "guilds") {
          sellerType = "صنف";
        } else if (item.sellerType === "steward") {
          sellerType = "مباشر";
        }

        if (item.sellType === "free") {
          sellType = "آزاد";
        } else {
          if (item.type === "manual") {
            sellType = "اختصاصی (دستی)";
          } else {
            sellType = "اختصاصی (اتوماتیک)";
          }
        }

        return [
          i + 1,
          item?.guilds
            ? item?.guilds?.guildsId
            : item?.steward?.guilds?.guildsId,
          formatJustDate(item.date),
          sellerType,
          sellType,
          item?.guilds
            ? item?.guilds?.guildsName
            : item?.steward?.guilds?.guildsName,
          item?.guilds
            ? item?.guilds?.user.fullname
            : item?.steward?.guilds?.user.fullname,
          item?.guilds
            ? item?.guilds?.user.nationalId
            : item?.steward?.guilds?.user.nationalId,
          item?.guilds
            ? item?.guilds?.user.mobile
            : item?.steward?.guilds?.user.mobile,
          item?.guilds
            ? item?.guilds?.typeActivity
            : item?.steward?.guilds?.typeActivity,
          item?.guilds
            ? item?.guilds?.areaActivity
            : item?.steward?.guilds?.areaActivity,
          item?.guilds
            ? item?.guilds?.licenseNumber
            : item?.steward?.guilds?.licenseNumber,
          item?.guilds
            ? item?.guilds?.address.city.name
            : item?.steward?.guilds?.address.city.name,
          item?.numberOfCarcasses,
          item?.weightOfCarcasses,
          item?.loggedRegistrationCode ? item.loggedRegistrationCode : "-",
          <SlaughterManageInventoryAllocationOperations item={item} key={i} />,
        ];
      }
    );

    setInventoryAllocationsDataTable(inventoryAllocationsData);
  }, [slaughterManageInventoryAllocations]);

  return (
    <Grid width="100%">
      <SimpleTable
        name={
          <Grid
            container
            alignItems="center"
            gap={SPACING.SMALL}
            justifyContent="space-between"
          >
            <Typography>مدیریت کل تخصیصات</Typography>
          </Grid>
        }
        columns={[
          "ردیف",
          "شناسه صنف",
          "تاریخ ثبت",
          "ماهیت",
          "نوع تخصیص",
          "نام واحد صنفی",
          "نام شخص/شرکت",
          "کدملی",
          "موبایل",
          "نوع فعالیت",
          "حوزه فعالیت",
          "شماره مجوز",
          "شهرستان",
          "حجم لاشه",
          "وزن لاشه",
          "کداحراز",
          "عملیات",
        ]}
        data={inventoryAllocationsDataTable}
      />
    </Grid>
  );
};
