import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import {
  Accordion,
  AccordionDetails,
  AccordionSummary,
  Box,
  Button,
  IconButton,
  Tooltip,
  Typography,
} from "@mui/material";
import {
  convertToIranianTime,
  formatJustDate,
  formatTime,
} from "../../../../utils/formatTime";
import { CheckCleanceCode } from "../../../../components/check-clearance-code/ChechClearanceCode";
import { BackButton } from "../../../../components/back-button/BackButton";
import ResponsiveTable from "../../../../components/responsive-table/ResponsiveTable";
import { cityHatchingShowTableDetail } from "../../services/city-hatching-show-table-detail";
import {
  LOADING_END,
  LOADING_START,
  OPEN_MODAL,
} from "../../../../lib/redux/slices/appSlice";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import axios from "axios";
import { ImFilePdf } from "react-icons/im";
import { ProvinceBarDifferencesModal } from "../../../province/components/province-bar-differences-modal/ProvinceBarDifferencesModal";

export const CityHatchingShowTableDetail = ({ keyItem }) => {
  const [data, setData] = useState(null);
  const [totalData, setTotalData] = useState([]);
  const [slaughterInformation, setSlaughterInformation] = useState([]);
  const [barInData, setbarInData] = useState([]);
  const [barOutData, setbarOutData] = useState([]);
  const [chainData, setChainData] = useState([]);
  const [differnceBarData, setDiffernceBarData] = useState([]);
  const [combinedCargoData, setCombinedCargoData] = useState([]);
  const [returnedAllocatedData, setReturnedAllocatedData] = useState([]);
  const [differenceData, setDifferenceData] = useState([]);
  const [evacuationReportsData, setEvacuationReportsData] = useState([]);
  const dispatch = useDispatch();

  useEffect(() => {
    dispatch(LOADING_START());
    dispatch(cityHatchingShowTableDetail({ keyItem })).then((r) => {
      setData(r.payload.data);
      dispatch(LOADING_END());
    });
  }, []);

  useEffect(() => {
    const newTotalData = [
      { title: "نام فارم", value: data?.poultry?.unitName || "-" },
      {
        title: "مرغدار",
        value: data?.poultry?.user
          ? `${data.poultry?.user.fullname} (${data.poultry?.user?.mobile})`
          : "-",
      },
      { title: "بهره برداری", value: data?.InteractTypeName || "-" },
      { title: "تاریخ جوجه ریزی", value: formatJustDate(data?.date) || "-" },
      {
        title: "حجم جوجه ریزی",
        value: data?.quantity?.toLocaleString() || "-",
      },
      { title: "سن", value: data?.chickenAge || "-" },
      { title: "نژاد", value: data?.chickenBreed || "-" },
      { title: "وضعیت", value: data?.violation ? "پیگیری" : "عادی" },
      { title: "شماره مجوز جوجه ریزی", value: data?.licenceNumber || "-" },
      { title: "شناسه یکتا", value: data?.poultry?.breedingUniqueId || "-" },
      { title: "مجوز بهداشتی جوجه ریزی", value: data?.CertId || "-" },
      { title: "ارتباط", value: data?.UnionTypeName || "-" },
      {
        title: "استان",
        value: data?.poultry?.address?.province?.name || "-",
      },
      {
        title: "دامپزشک فارم",
        value: data?.vetFarm
          ? `${data?.vetFarm?.vetFarmFullName} (${data?.vetFarm?.vetFarmMobile})`
          : "-",
      },
      { title: "سالن", value: data?.hall || "-" },
      { title: "دوره جوجه ریزی", value: data?.period || "-" },
      {
        title: "تاریخ ثبت جوجه ریزی",
        value: formatJustDate(data?.createDate) || "-",
      },
      {
        title: "میانگین سن کشتار",
        value: data?.killingAveAge?.toLocaleString() || "-",
      },
      {
        title: "پیش بینی تاریخ کشتار",
        value: formatJustDate(data?.predicateDate) || "-",
      },
      { title: "مالکیت", value: data?.PersonTypeName || "-" },
    ];

    const newSlaughterInformation = [
      {
        title: " تایید تخلیه در رصدیار",
        value: `%${(
          ((data?.killedQuantity + data?.totalLosses) * 100) /
          data?.quantity
        ).toFixed(0)}`,
      },
      {
        title: " تایید تخلیه در سماصط",
        value: data?.samasatDischargePercentage
          ? `%${data?.samasatDischargePercentage}`
          : "-",
      },
      {
        title: "حجم کشتار شده",
        value: data?.killedQuantity
          ? `${data?.killedQuantity?.toLocaleString()} (%${(
              (data?.killedQuantity * 100) /
              data?.quantity
            ).toFixed(0)})`
          : "0 (0%)",
      },
      // {
      //   title: "وزن بازگشتی",
      //   value: data?.killingInfo?.totalReturnWeight?.toLocaleString() || "0",
      // },
      // {
      //   title: "حجم بازگشتی",
      //   value: data?.killingInfo?.totalReturnQuantity?.toLocaleString() || "0",
      // },
      {
        title: "حجم تخصیصات بازگشتی",
        value:
          data?.killingInfo?.returnProvinceKillRequestsQuantity?.toLocaleString() ||
          "0",
      },
      {
        title: "وزن تخصیصات بازگشتی",
        value:
          data?.killingInfo?.returnProvinceKillRequestsWeight?.toLocaleString() ||
          "0",
      },
      {
        title: "حجم بارهای بازگشتی",
        value:
          data?.killingInfo?.returnKillHouseQuantity?.toLocaleString() || "0",
      },
      {
        title: "وزن بارهای بازگشتی",
        value:
          data?.killingInfo?.returnKillHouseWeight?.toLocaleString() || "0",
      },
      {
        title: "حجم مانده در سالن",
        value: data?.leftOver
          ? `${data?.leftOver.toLocaleString()} (%${(
              (data?.leftOver * 100) /
              data?.quantity
            ).toFixed(0)})`
          : "0 (0%)",
      },
      {
        title: "تلفات کل",
        value: data?.totalLosses
          ? `${data?.totalLosses.toLocaleString()} (%${(
              (data?.totalLosses * 100) /
              data?.quantity
            ).toFixed(0)})`
          : "0 (0%)",
      },
      {
        title: "وزن کل کشتار",
        value: data?.totalKilledWeight?.toLocaleString() || "0",
      },
      {
        title: "تعداد بارها",
        value: data?.killingInfo?.killHouseRequests?.toLocaleString() || "0",
      },
      {
        title: "حجم بارها",
        value:
          data?.killingInfo?.killHouseRequestsFirstQuantity?.toLocaleString() ||
          "0",
      },
      {
        title: "وزن بارها",
        value:
          data?.killingInfo?.killHouseRequestsFirstWeight?.toLocaleString() ||
          "0",
      },
      {
        title: "افت بارها",
        value:
          `${data?.killingInfo?.wareHouseBarsWeightLose?.toFixed()}%` || "0",
      },
      {
        title: "حجم فروش به داخل استان",
        value:
          data?.killingInfo?.totalSaleInProvinceQuantity?.toLocaleString() ||
          "0",
      },
      {
        title: "وزن فروش به داخل استان",
        value:
          data?.killingInfo?.totalSaleInProvinceWeight?.toLocaleString() || "0",
      },
      {
        title: "حجم فروش به خارج استان",
        value: data?.outProvinceKilledQuantity?.toLocaleString() || "0",
      },
      {
        title: "وزن فروش به خارج استان",
        value: data?.outProvinceKilledWeight?.toLocaleString() || "0",
      },
      {
        title: "حجم اختلاف کشتار",
        value: data?.barDifferenceRequestQuantity || "0",
      },
      {
        title: "وزن اختلاف کشتار",
        value: data?.barDifferenceRequestWeight || "0",
      },
      {
        title: "تعداد بارهای تحویلی",
        value:
          data?.killingInfo?.barCompleteWithKillHouse?.toLocaleString() || "0",
      },
      {
        title: "حجم بارهای تحویلی",
        value:
          data?.killingInfo?.acceptedRealQuantityFinal?.toLocaleString() || "0",
      },
      {
        title: "وزن بارهای تحویلی",
        value:
          data?.killingInfo?.acceptedRealWightFinal?.toLocaleString() || "0",
      },
      {
        title: "بارهای ورودی به انبار",
        value: data?.killingInfo?.wareHouseBars?.toLocaleString() || "0",
      },
      {
        title: "حجم لاشه های انبار",
        value:
          data?.killingInfo?.wareHouseBarsQuantity?.toLocaleString() || "0",
      },
      {
        title: "وزن لاشه های انبار",
        value: data?.killingInfo?.wareHouseBarsWeight?.toLocaleString() || "0",
      },
      {
        title: "حجم تعهد دولتی",
        value: data?.totalCommitmentQuantity?.toLocaleString() || "0",
      },
      {
        title: "حجم کشتار دولتی",
        value: data?.governmentalQuantity?.toLocaleString() || "0",
      },
      {
        title: "حجم تعهد آزاد",
        value: data?.totalFreeCommitmentQuantity?.toLocaleString() || "0",
      },

      {
        title: "حجم کشتار آزاد",
        value: data?.freeQuantity?.toLocaleString() || "0",
      },
      {
        title: "تخصیصات بدون بار",
        value: data?.killingInfo?.provinceKillRequests?.toLocaleString() || "0",
      },
      {
        title: "حجم تخصیصات بدون بار",
        value:
          data?.killingInfo?.provinceKillRequestsQuantity?.toLocaleString() ||
          "0",
      },
      {
        title: "وزن تخصیصات بدون بار",
        value:
          data?.killingInfo?.provinceKillRequestsWeight?.toLocaleString() ||
          "0",
      },
      {
        title: "وزن تعهد دولتی",
        value: data?.totalCommitment?.toLocaleString() || "0",
      },
      {
        title: "میانگین وزن کشتار",
        value: data?.totalAverageKilledWeight?.toLocaleString() || "0",
      },

      {
        title: "تعداد کشتار فعال",
        value: data?.activeKill?.activeKill ? "دارد" : "ندارد",
      },
      {
        title: "تعداد درخواست کشتار",
        value: data?.activeKill?.countOfRequest || "0",
      },
      {
        title: "حجم افزایشی",
        value: data?.increaseQuantity?.toLocaleString() || "0",
      },
      {
        title: "تلفات دامپزشک",
        value: data?.losses
          ? `${data?.losses.toLocaleString()} (%${(
              (data?.losses * 100) /
              data?.quantity
            ).toFixed(0)})`
          : "0 (0%)",
      },

      {
        title: "تلفات اتحادیه",
        value: data?.directLosses
          ? `${data?.directLosses.toLocaleString()} (%${(
              (data?.directLosses * 100) /
              data?.quantity
            ).toFixed(0)})`
          : "0 (0%)",
      },

      {
        title: "آخرین تغییر",
        value: data?.latestHatchingChange
          ? `${data?.latestHatchingChange?.fullName} در تاریخ ${formatTime(
              data?.latestHatchingChange?.date
            )}`
          : "-",
      },
    ];

    const newBarInData = data?.bars?.map((bar, index) => {
      return [
        index + 1,
        bar?.barCode,
        bar?.status || "-",
        bar?.poultryRequest?.freeSaleInProvince ? "آزاد" : "دولتی",
        formatJustDate(bar?.date),
        bar?.buyer?.name || "-",
        bar?.buyer?.killHouseOperator?.user?.mobile || "-",
        bar?.killerInfo
          ? `${bar?.killerInfo?.killHouseOperator?.user?.fullname}(${bar?.killerInfo?.killHouseOperator?.user?.mobile})`
          : "-",
        bar?.poultryRequest?.age || "-",
        bar?.product || "-",
        `${bar?.buyer?.killHouseOperator?.address?.province?.name}(${bar?.buyer?.killHouseOperator?.address?.city?.name})` ||
          "-",
        <CheckCleanceCode
          clearanceCode={bar?.clearanceCode || "-"}
          key={index}
        />,
        bar?.quarantineQuantity || "-",
        bar?.acceptedRealQuantity?.toLocaleString() || "-",
        bar?.acceptedRealWeight?.toLocaleString() || "-",
        bar?.wareHouseAcceptedRealQuantity?.toLocaleString() || "-",
        bar?.wareHouseAcceptedRealWeight?.toLocaleString() || "-",
      ];
    });

    const newBarOutData = data?.outBars?.map((bar, index) => {
      return [
        index + 1,
        bar?.orderCode,
        formatJustDate(bar?.sendDate),
        bar?.buyerFullname || "-",
        bar?.buyerMobile || "-",
        "مرغ گرم",
        `${bar?.buyerProvince}(${bar?.buyerProvince})` || "-",
        <CheckCleanceCode
          clearanceCode={bar?.quarantineCode || "-"}
          key={index}
        />,
        bar?.quarantinQuantity || "-",
        bar?.killingAge || "-",
        bar?.quantity?.toLocaleString() || "-",
        bar?.weight?.toLocaleString() || "-",
      ];
    });
    const newDiffernceBarData = data?.differentBars?.map((bar, index) => {
      return [
        index + 1,
        bar?.ResideDatePersian || "-",
        bar?.GoodAmount?.toLocaleString() || "-",
        bar?.Out ? "خارج استان" : "داخل استان",
        bar?.Age || "-",
        bar?.TrackingStatusDescription || "-",
        bar?.DesUnitName || "-",
        bar?.GoodName || "-",
        bar?.City || "-",
        bar?.TrackingCode || "-",
      ];
    });
    // Combine nonReceipt and returnKillHouseRequest data

    const newCombinedCargoData = data?.returnKillHouseRequest?.map(
      (item, index) => {
        let state = "";
        if (item.state === "pending") {
          state = "در انتظار تایید";
        } else if (item.state === "accepted") {
          state = "تایید شده";
        } else if (item.state === "rejected") {
          state = "رد شده";
        }

        let requestType = "";
        if (item?.poultryRequest?.market) {
          requestType = "پنل معاملات";
        } else if (item?.poultryRequest?.directBuying) {
          requestType = "خرید مستقیم";
        } else if (item?.warehouse) {
          requestType = "انبار";
        } else {
          requestType = "اتحادیه";
        }

        let killType = "";
        if (item?.poultryRequest?.freezing) {
          killType = "انجماد";
        } else if (item?.poultryRequest?.export) {
          killType = "صادرات";
        } else {
          killType = "عادی";
        }

        // Determine the type based on return_trash field or dataType

        return [
          index + 1,
          item?.barCode || "-",
          item?.poultryRequest?.orderCode || "-",
          requestType,
          killType,
          item?.poultryRequest?.poultry?.unitName || "-",
          `${item?.poultryRequest?.poultry?.user?.fullname || "-"} (${
            item?.poultryRequest?.poultry?.user?.mobile || "-"
          })`,
          item?.poultryRequest?.age,
          item?.poultryRequest?.poultry?.address?.city?.name || "-",
          formatTime(item?.killRequest?.reciveDate) ||
            formatJustDate(item?.date) ||
            "-",
          item?.quantity?.toLocaleString() || "-",
          formatTime(item?.createDate) || "-",
          item?.killhouseUser?.name ||
            item?.killer?.name ||
            item?.buyer?.name ||
            "-",
          item?.killhouseUser?.killHouseOperator?.user?.city?.name ||
            item?.killer?.killHouseOperator?.user?.city?.name ||
            `${item?.buyer?.killHouseOperator?.address?.province?.name || ""}(${
              item?.buyer?.killHouseOperator?.address?.city?.name || ""
            })` ||
            "-",
          item?.weightInfo?.weight?.toLocaleString() || "-",
          item?.weightInfo?.indexWeight?.toLocaleString() || "-",
          item?.acceptedRealQuantity?.toLocaleString() || "-",
          item?.acceptedRealWeight?.toLocaleString() || "-",
          item?.poultryRequest?.amount?.toLocaleString() + " ﷼" || "-",
          item?.weightInfo?.killHousePrice?.toLocaleString() + " ﷼" || "-",
          state || "-",
          item?.car?.pelak || "-",
          item?.car?.driverName || item?.addCar?.driver?.driverName || "-",
          <CheckCleanceCode
            clearanceCode={item?.clearanceCode || "-"}
            key={index}
          />,
          item?.quarantineQuantity || "-",
          formatTime(item?.modifyDate),
          item?.dataType === "returned"
            ? (item?.nonReceipt && item?.mainNonReceipt ? `کاربر` : "سیستم") +
              " " +
              formatTime(item?.modifyDate)
            : "-",
        ];
      }
    );

    const newChainData = data?.chainAllocation?.map((item, index) => {
      return [
        index + 1,
        item?.state === "accepted"
          ? "تایید شده"
          : item?.state === "pending"
          ? "در انتظار تایید"
          : "رد شده",

        formatJustDate(item?.date),
        item?.outProvince ? "خارج استان" : "داخل استان",
        `${item?.registerer?.fullname} (${item?.registerer?.mobile})`,
        item?.killHouse?.killHouseOperator?.user.fullname
          ? `${item?.killHouse?.killHouseOperator?.user.fullname} (${item?.killHouse?.killHouseOperator?.user.mobile})`
          : `${item?.buyerName} (${item?.buyerMobile})`,
        `${item?.companyName} (${item?.companyUserMobile})`,

        item?.healthCode,
        item?.quarantineCode || "-",
        item?.driverName ? `${item?.driverName} (${item?.driverMobile})` : "-",
        item?.typeCar,
        item?.pelak,
        item?.quantity,
        item?.indexWeight,
        item?.weight,
      ];
    });

    // Map returnProvinceRequest data (Returned Allocated)
    const newReturnedAllocatedData = data?.returnProvinceRequest?.map(
      (item, index) => {
        let state = "";
        if (item.state === "pending") {
          state = "در انتظار تایید";
        } else if (item.state === "accepted") {
          state = "تایید شده";
        } else if (item.state === "rejected") {
          state = "رد شده";
        }

        let requestType = "";
        if (item?.market) {
          requestType = "پنل معاملات";
        } else if (item?.directBuying) {
          requestType = "خرید مستقیم";
        } else if (item?.warehouse) {
          requestType = "انبار";
        } else {
          requestType = "اتحادیه";
        }

        return [
          index + 1,
          item?.orderCode || "-",
          requestType,
          item?.poultryUnitName,
          `${item?.poultryFullname} (${item?.poultryMobile})`,
          item?.poultryRequest?.age,
          item?.poultryCity,
          formatJustDate(item?.killingDate),
          item?.poultryRequestQuantity?.toLocaleString(),
          formatJustDate(item?.createDate),
          item?.killhouseUser?.name || "-",
          item?.killhouseUser?.city || "-",
          item?.poultryAmount?.toLocaleString() + " ﷼",
          item?.killHousePrice?.toLocaleString() + " ﷼",
          item?.quantity?.toLocaleString(),
          state,
          item?.firstCarAllocatedQuantity > 0 ? "دارد" : "ندارد",
          (item?.quantity - item?.totalKilledQuantity)?.toLocaleString(),
          (item?.returner
            ? `${item?.returner.fullname} (${item?.returner?.mobile})`
            : "سیستم") +
            " " +
            convertToIranianTime(item?.modifyDate),
        ];
      }
    );

    const newDifferenceData = data?.barDiffrentRequets?.map((item, index) => {
      return [
        index + 1,
        item?.registerFullname,
        formatJustDate(item?.createDate),
        `${item?.hatching?.poultry?.unitName || ""} (${
          item?.hatching?.poultry?.user?.mobile || ""
        })`,
        `${item?.killHouse?.name || ""} (${
          item?.killHouse?.killHouseOperator?.user?.mobile || ""
        })`,
        item?.barInfo?.totalQuantity?.toLocaleString(),
        item?.barInfo?.totalWeight?.toLocaleString(),
        item?.barInfo?.firstTotalQuantity?.toLocaleString(),
        item?.barInfo?.differenceQuantity?.toLocaleString(),
        item?.quantity?.toLocaleString(),
        item?.weight?.toLocaleString(),

        item?.acceptorImages?.[0] ||
        item?.acceptorMessage ||
        item?.registerMessage ||
        item?.violationImage?.[0] ? (
          <Grid container key={item?.id + "assign"}>
            <IconButton
              variant="contained"
              onClick={() => {
                dispatch(
                  OPEN_MODAL({
                    content: <ProvinceBarDifferencesModal item={item} />,
                    title: "پیوست",
                  })
                );
              }}
            >
              ✉️
            </IconButton>
          </Grid>
        ) : (
          "-"
        ),

        item.state === "pending"
          ? "در انتظار تایید"
          : item.state === "rejected"
          ? "رد شده"
          : "تایید شده",
      ];
    });

    const newEvacuationReportsData = data?.evacuationReports?.map(
      (item, index) => {
        return [
          index + 1,
          item?.MoReportId || "-",
          item?.ReportTypeString || "-",
          item?.MoDateShamsi ?? "-",
          item?.GoodCount?.toLocaleString() || "-",
          item?.RegDateShamsiWithTime ?? "-",
        ];
      }
    );

    setTotalData(newTotalData);
    setSlaughterInformation(newSlaughterInformation);
    setbarInData(newBarInData);
    setbarOutData(newBarOutData);
    setChainData(newChainData);
    setDiffernceBarData(newDiffernceBarData);
    setCombinedCargoData(newCombinedCargoData);
    setReturnedAllocatedData(newReturnedAllocatedData);
    setDifferenceData(newDifferenceData);
    setEvacuationReportsData(newEvacuationReportsData);
  }, [data]);

  return (
    <Grid container xs={12} justifyContent="center" alignItems="center" gap={2}>
      <Grid container xs={12}>
        <BackButton />
      </Grid>
      <Grid container xs={12}>
        <Tooltip title="خروجی پی دی اف">
          <a
            href={`${axios.defaults.baseURL}poultry_hatching_detail_pdf/?key=${keyItem}
            `}
            rel="noreferrer"
          >
            <Button color="error">
              <ImFilePdf size={32} />
            </Button>
          </a>
        </Tooltip>
      </Grid>

      <Grid sx={{ width: "100%" }}>
        <Box
          sx={{
            display: "flex",
            alignItems: "flex-end",
            position: "relative",
          }}
        >
          <Typography
            variant="subtitle1"
            color="#2D5FFF"
            sx={{
              bgcolor: "#E0E7FF",
              px: 2,
              py: 1,
              borderRadius: "4px 0 0 4px",
              position: "relative",
              zIndex: 2,
              fontSize: "0.9rem",
            }}
          >
            اطلاعات واحد گله
          </Typography>
          <Box
            sx={{
              flexGrow: 1,
              height: "2px",
              bgcolor: "#2D5FFF",
              position: "absolute",
              bottom: 8,
              left: 0,
              right: 0,
            }}
          />
        </Box>

        <Box
          sx={{
            display: "grid",
            gridTemplateColumns: "repeat(auto-fill, minmax(200px, 1fr))",
            gap: 1,
          }}
        >
          {totalData.map((item, index) => (
            <Box
              key={index}
              sx={{
                display: "flex",
                justifyContent: "start",
                alignItems: "center",
                py: 1,
              }}
            >
              <Typography
                variant="body2"
                color="text.secondary"
                sx={{ fontSize: "0.8rem", fontWeight: 500 }}
              >
                {item?.title}:
              </Typography>
              <Typography
                variant="body2"
                sx={{ fontSize: "0.8rem", fontWeight: 600 }}
                px={1}
              >
                {item?.value || "-"}
              </Typography>
            </Box>
          ))}
        </Box>
      </Grid>

      <Grid sx={{ width: "100%" }}>
        <Box
          sx={{
            display: "flex",
            alignItems: "flex-end",
            position: "relative",
          }}
        >
          <Typography
            variant="subtitle1"
            color="#2D5FFF"
            sx={{
              bgcolor: "#E0E7FF",
              px: 2,
              py: 1,
              borderRadius: "4px 0 0 4px",
              position: "relative",
              zIndex: 2,
              fontSize: "0.9rem",
            }}
          >
            اطلاعات کشتار و تلفات
          </Typography>
          <Box
            sx={{
              flexGrow: 1,
              height: "2px",
              bgcolor: "#2D5FFF",
              position: "absolute",
              bottom: 8,
              left: 0,
              right: 0,
            }}
          />
        </Box>

        <Box
          sx={{
            display: "grid",
            gridTemplateColumns: "repeat(auto-fill, minmax(200px, 1fr))",
            gap: 1,
          }}
        >
          {slaughterInformation?.map((item, index) => {
            const isRedItem = [
              "حجم تخصیصات بازگشتی",
              "وزن تخصیصات بازگشتی",
              "حجم بارهای بازگشتی",
              "وزن بارهای بازگشتی",
              "وزن اختلاف کشتار",
              "حجم اختلاف کشتار",
            ].includes(item?.title);

            return (
              <Box
                key={index}
                sx={{
                  display: "flex",
                  justifyContent: "start",
                  alignItems: "center",
                  py: 1,
                }}
              >
                <Typography
                  variant="body2"
                  sx={{
                    fontSize: "0.8rem",
                    fontWeight: 500,
                    color: isRedItem ? "red" : "text.secondary",
                  }}
                >
                  {item?.title}:
                </Typography>
                <Typography
                  variant="body2"
                  sx={{
                    fontSize: "0.8rem",
                    fontWeight: 600,
                    color: isRedItem ? "red" : "inherit",
                  }}
                  px={1}
                >
                  {item?.value || "-"}
                </Typography>
              </Box>
            );
          })}
        </Box>
      </Grid>
      <Box
        sx={{
          width: "100%",
          height: "2px",
          bgcolor: "#2D5FFF",
          mt: 2,
          borderRadius: "2px",
        }}
      />

      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>اطلاعات بارهای داخل استان</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                title="اطلاعات بار داخل استان"
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "کدبار",
                  "وضعیت",
                  "نوع فروش",
                  "تاریخ کشتار",
                  "خریدار",
                  "تلفن",
                  "کشتارکن",
                  "سن مرغ",
                  "محصول",
                  "استان/شهر",
                  "کدقرنطینه",
                  "استعلام قرنطینه",
                  "حجم زنده",
                  "وزن زنده (کیلوگرم)",
                  "حجم وارد شده به انبار",
                  "وزن وارد شده به انبار",
                ]}
                // hasSum
                // hasSumColumn="کدبار"
                data={barInData}
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>اطلاعات بارهای قرنطینه</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ overflowX: "auto" }}>
              <ResponsiveTable
                title="اطلاعات بارهای قرنطینه"
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "تاریخ کشتار",
                  "حجم",
                  "نوع بار",
                  "سن مرغ",
                  "وضعیت",
                  "خریدار",
                  "محصول",
                  "شهر",
                  "کدقرنطینه",
                ]}
                // hasSum
                // hasSumColumn="تاریخ کشتار"
                data={differnceBarData}
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>اطلاعات بارهای بازگشتی</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                title="اطلاعات بارهای بازگشتی"
                paginated
                columns={[
                  "ردیف",
                  "کد بار",
                  "کدسفارش",
                  "نوع درخواست",
                  "نوع کشتار",
                  "نام فارم",
                  "نام مرغدار",
                  "سن مرغ",
                  "شهر مرغدار",
                  "تاریخ درخواست کشتار",
                  "تعداد بار",
                  "تاریخ ثبت بار",
                  "نام کشتارگاه",
                  "شهر کشتارگاه",
                  "وزن",
                  "میانگین وزنی",
                  "حجم نهایی",
                  "وزن نهایی",
                  "قیمت مرغدار",
                  "قیمت کشتارگاه",
                  "وضعیت تایید",
                  "پلاک خودرو",
                  "نام راننده",
                  "کدقرنطینه",
                  "استعلام قرنطینه",
                  "تاریخ برگشت",
                  "نوع برگشت",
                ]}
                data={combinedCargoData}
                // hasSum
                // hasSumColumn="کد بار"
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>اطلاعات تخصیصات بازگشتی</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                title="اطلاعات تخصیصات بازگشتی"
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "کدسفارش",
                  "نوع درخواست",
                  "نام فارم",
                  "نام مرغدار",
                  "سن مرغ",
                  "شهر مرغدار",
                  "تاریخ کشتار",
                  "تعداد درخواست",
                  "تاریخ ثبت تخصیص",
                  "نام کشتارگاه",
                  "شهر کشتارگاه",
                  "قیمت مرغدار",
                  "قیمت کشتارگاه",
                  "تعداد تخصیص",
                  "وضعیت تایید",
                  "وضعیت تخصیص ماشین",
                  "مانده قابل تخصیص",
                  "برگشت دهنده",
                ]}
                data={returnedAllocatedData}
                // hasSum
                // hasSumColumn="کدسفارش"
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>اطلاعات بارهای خارج استان</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                title="اطلاعات بار خارج استان"
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "کد سفارش",
                  "تاریخ",
                  "خریدار",
                  "تلفن",
                  "محصول",
                  "استان/شهر",
                  "کدقرنطینه",
                  "استعلام قرنطینه",
                  "سن مرغ",
                  "حجم زنده",
                  "وزن زنده (کیلوگرم)",
                ]}
                data={barOutData}
                // hasSum
                // hasSumColumn="کد سفارش"
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>

      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>مدیریت بار زنجیره</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                title="مدیریت بار زنجیره"
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "وضعیت",
                  "تاریخ سفارش",
                  "نوع فروش",
                  "ثبت کننده سفارش",
                  "خریدار",
                  "شرکت زنجیره",
                  "کد بهداشتی",
                  "کد قرنطینه",
                  "راننده",
                  "نوع خودرو",
                  "پلاک",
                  "حجم",
                  "میانگین وزن",
                  "وزن",
                ]}
                data={chainData}
                // hasSum
                // hasSumColumn="وضعیت"
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>اختلاف کشتار</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                data={differenceData}
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "ثبت کننده",
                  "تاریخ ثبت",
                  "مرغدار",
                  "کشتارگاه",
                  "حجم کم شده از سالن مرغدار",
                  "وزن تقریبی کشتار (کیلوگرم)",
                  "حجم سفارشات دریافتی توسط کشتارگاه",
                  " اختلاف کشتار(حجم)",
                  "اختلاف حجم",
                  "اختلاف وزن",
                  "پیوست",
                  "وضعیت",
                ]}
                // hasSum
                // hasSumColumn="ردیف"
                title="اختلاف کشتار"
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
      <Grid item xs={12}>
        <Accordion sx={{ width: "100%" }}>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography>گزارش تلفات</Typography>
          </AccordionSummary>
          <AccordionDetails>
            <Grid sx={{ width: "100%", overflowX: "auto" }}>
              <ResponsiveTable
                data={evacuationReportsData}
                paginated
                customWidth={"100%"}
                columns={[
                  "ردیف",
                  "شماره گزارش",
                  "نوع گزارش",
                  "تاریخ معدوم سازی",
                  "تعداد",
                  "تاریخ ثبت",
                ]}
                // hasSum
                // hasSumColumn="ردیف"
                title="گزارش تلفات"
              />
            </Grid>
          </AccordionDetails>
        </Accordion>
      </Grid>
    </Grid>
  );
};
