import {
Button,
IconButton,
TextField,
Tooltip,
Typography,
} from "@mui/material";
import axios from "axios";
import { useContext, useEffect, useState } from "react";
import { RiFileExcel2Fill, RiSearchLine } from "react-icons/ri";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { formatTime, formatJustDate } from "../../../../utils/formatTime";
import { getFaUserRole } from "../../../../utils/getFaUserRole";
import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";
import { CityManageHatchingsArchiveActions } from "../city-manage-hatchings-operations/CityManageHatchingsOperations";
import { useDispatch, useSelector } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import { cityGetHatchingInfoFull } from "../../services/city-get-hatching-info-full";
import ResponsiveTable from "../../../../components/responsive-table/ResponsiveTable";
import {
// DRAWER,
LOADING_END,
LOADING_START,
OPEN_MODAL,
} from "../../../../lib/redux/slices/appSlice";
import { SimpleTable } from "../../../../components/simple-table/SimpleTable";
import ShowImage from "../../../../components/show-image/ShowImage";
import RemoveRedEyeIcon from "@mui/icons-material/RemoveRedEye";
import ArticleIcon from "@mui/icons-material/Article";
import {
ROUTE_ADMINXـHATCHINGS,
ROUTE_CITY_JIHADـHATCHINGS,
ROUTE_CITY_POULTRYـHATCHINGS,
ROUTE_PROVINCE_SUPERVISORـHATCHINGS,
ROUTE_PROVINCEـHATCHINGS,
ROUTE_SUPER_ADMINـHATCHINGS,
ROUTE_SUPPORTERـHATCHINGS,
} from "../../../../routes/routes";
import { useNavigate } from "react-router-dom";
// import { CityHatchingShowTableDetail } from "../city-hatching-show-table-detail/CityHatchingShowTableDetail";
export const CityHatchingUnassigned = ({ readOnly }) => {
const dispatch = useDispatch();
const isReadOnly = readOnly || false;
const [selectedAge1, setSelectedAge1] = useState(0);
const [selectedAge2, setSelectedAge2] = useState(0);
const [data, setData] = useState([]);
const [totalRows, setTotalRows] = useState(0);
const [perPage, setPerPage] = useState(10);
const [textValue, setTextValue] = useState("");
const [page, setPage] = useState(1);
const [tableData, setTableData] = useState([]);
const userKey = useSelector((state) => state.userSlice.userProfile.key);
const navigate = useNavigate();
const [openNotif] = useContext(AppContext);
const handleTextChange = (event) => {
setTextValue(event.target.value);
};
const hatchingAdded = useSelector((state) => state.citySlice.hatchingAdded);
useEffect(() => {
fetchApiData();
}, [hatchingAdded]);
const fetchApiData = async (pageParam = page, perPageParam = perPage) => {
dispatch(LOADING_START());
const response = await axios.get("poultry_hatching/", {
params: {
unknown: true,
search: "filter",
value: textValue,
role: getRoleFromUrl(),
page: pageParam,
page_size: perPageParam,
age1: selectedAge1 || 0,
age2: selectedAge2 || 0,
},
});
dispatch(LOADING_END());
setData(response.data.results);
setTotalRows(response.data.count);
};
const handlePageChange = (page) => {
setPage(page);
fetchApiData(page, perPage);
};
const handlePerRowsChange = (perRows) => {
setPerPage(perRows);
setPage(1);
};
useEffect(() => {
fetchApiData();
}, [perPage]);
const updateTable = () => {
fetchApiData();
};
// const killedNumber = (item) => {
// let killedNumber = "";
// killedNumber = item.quantity - item.losses - item.leftOver;
// return killedNumber;
// };
useEffect(() => {
const d = data?.map((item, i) => {
return [
,
{
navigate(
getRoleFromUrl() === "AdminX"
? `${ROUTE_ADMINXـHATCHINGS}/${item.key}`
: getRoleFromUrl() === "Supporter"
? `${ROUTE_SUPPORTERـHATCHINGS}/${item.key}`
: getRoleFromUrl() === "SuperAdmin"
? `${ROUTE_SUPER_ADMINـHATCHINGS}/${item.key}`
: getRoleFromUrl() === "CityPoultry"
? `${ROUTE_CITY_POULTRYـHATCHINGS}/${item.key}`
: getRoleFromUrl() === "ProvinceSupervisor"
? `${ROUTE_PROVINCE_SUPERVISORـHATCHINGS}/${item.key}`
: getRoleFromUrl() === "ProvinceOperator"
? `${ROUTE_PROVINCEـHATCHINGS}/${item.key}`
: getRoleFromUrl() === "CityJahad"
? `${ROUTE_CITY_JIHADـHATCHINGS}/${item.key}`
: ""
);
}}
>
,
page === 1 ? i + 1 : i + perPage * (page - 1) + 1,
{item?.violation ? "پیگیری" : "عادی"}
,
item?.licenceNumber,
item?.poultry?.breedingUniqueId,
item?.CertId,
// item?.commitmentType === "free" ? "آزاد" : "دولتی",
item?.poultry?.unitName || "-",
`${item?.poultry?.userprofile?.fullName ?? "-"} (${
item?.poultry?.userprofile?.mobile ?? "-"
}) ${item?.violationReport ? "✉️" : ""}`,
item?.InteractTypeName ? (
{item?.InteractTypeName}
) : (
"-"
),
item?.PersonTypeName,
item?.UnionTypeName,
`${item?.poultry?.address?.city?.name ?? "-"}/${
item?.poultry?.cityOperator
? item?.poultry?.cityOperator
: "بدون تعاونی"
}`,
item?.vetFarm?.vetFarmMobile
? `${item?.vetFarm?.vetFarmFullName} (${item?.vetFarm?.vetFarmMobile})`
: "-",
item.hall,
item.period,
formatTime(item?.createDate),
formatTime(item?.date),
item?.poultry?.killingAveAge?.toLocaleString(),
item?.predicateDate ? formatJustDate(item?.predicateDate) : "-",
item.chickenBreed,
item.age,
item?.quantity?.toLocaleString(),
item?.increaseQuantity?.toLocaleString(),
`${item.losses} (%${((item.losses * 100) / item.quantity).toFixed(0)})`,
,
`${item?.totalLosses?.toLocaleString()} (%${(
(item.totalLosses * 100) /
item.quantity
).toFixed(0)})`,
`${item?.totalCommitmentQuantity?.toLocaleString()}`,
`${item?.totalFreeCommitmentQuantity?.toLocaleString()}`,
`${item?.governmentalQuantity?.toLocaleString()}`,
`${item?.governmentalKilledQuantity?.toLocaleString()}`,
`${item?.freeQuantity?.toLocaleString()}`,
`${item?.freeKilledQuantity?.toLocaleString()}`,
`${item?.outProvinceKilledQuantity?.toLocaleString()}`,
`${item?.outProvinceKilledWeight?.toLocaleString()}`,
`${item?.barDifferenceRequestQuantity?.toLocaleString()}`,
`${item?.barDifferenceRequestWeight?.toLocaleString()}`,
`${item?.killingInfo?.provinceKillRequests?.toLocaleString()}`,
`${item?.killingInfo?.provinceKillRequestsQuantity?.toLocaleString()}`,
`${item?.killingInfo?.provinceKillRequestsWeight?.toLocaleString()}`,
item?.killedQuantity?.toLocaleString() +
` (%${((item?.killedQuantity * 100) / item.quantity).toFixed(0)})`,
item?.leftOver?.toLocaleString(),
`%${((item?.leftOver * 100) / item?.quantity).toFixed(0)}`,
// item?.totalCommitment?.toLocaleString(),
`%${((item.totalLosses * 100) / item.quantity).toFixed(0)}`,
`%${((item?.killedQuantity * 100) / item.quantity).toFixed(0)}`,
`%${((item?.leftOver * 100) / item?.quantity).toFixed(0)}`,
`%${(
((item?.killedQuantity + item?.totalLosses) * 100) /
item?.quantity
).toFixed(0)}`,
item?.samasatDischargePercentage
? `%${item?.samasatDischargePercentage}`
: "-",
item?.totalCommitment?.toLocaleString(),
item?.governmentalKilledQuantity?.toLocaleString(),
item?.freeKilledQuantity?.toLocaleString(),
item?.totalAverageKilledWeight?.toLocaleString(),
item?.totalKilledWeight?.toLocaleString(),
item?.activeKill?.activeKill ? "دارد" : "ندارد",
item?.activeKill?.countOfRequest ? item.activeKill.countOfRequest : "-",
item?.killingInfo?.killHouseRequests?.toLocaleString(),
item?.killingInfo?.killHouseRequestsFirstQuantity?.toLocaleString(),
item?.killingInfo?.killHouseRequestsFirstWeight?.toLocaleString(),
item?.killingInfo?.barCompleteWithKillHouse?.toLocaleString(),
item?.killingInfo?.acceptedRealWightFinal?.toLocaleString(),
item?.chainKilledQuantity?.toLocaleString(),
item?.chainKilledWeight?.toLocaleString(),
item?.exportKilledQuantity?.toLocaleString(),
item?.exportKilledWeight?.toLocaleString(),
item?.killingInfo?.wareHouseBars?.toLocaleString(),
item?.killingInfo?.wareHouseBarsQuantity?.toLocaleString(),
item?.killingInfo?.wareHouseBarsWeight?.toLocaleString(),
item?.killingInfo?.wareHouseBarsWeightLose?.toFixed(2),
item.lastChange
? `${item.lastChange.fullName} (${getFaUserRole(
item.lastChange.role
)}) در تاریخ ${formatTime(item.lastChange.date)}`
: "-",
item.latestHatchingChange
? `${item.latestHatchingChange.fullName} (${getFaUserRole(
item.latestHatchingChange.role
)}) در تاریخ ${formatTime(item.latestHatchingChange.date)}`
: "-",
item?.violationReport ? (
{
dispatch(
OPEN_MODAL({
title: "گزارش ",
content: (
{item?.violationImage?.map((option, index) => (
))}
,
],
]}
/>
),
})
);
}}
>
) : (
"-"
),
];
});
setTableData(d);
}, [data]);
const handleSubmit = async (event) => {
event.preventDefault();
dispatch(LOADING_START());
dispatch(
cityGetHatchingInfoFull({
age1: selectedAge1,
age2: selectedAge2,
tab: "unknown",
textValue: textValue,
})
);
try {
const response = await axios.get(
`poultry_hatching/?role=${getRoleFromUrl()}&age1=${
selectedAge1 ? selectedAge1 : 0
}&age2=${
selectedAge2 ? selectedAge2 : 0
}&search=filter&value=${textValue}&page=${1}&page_size=${perPage}&unknown=true`
);
setData(response.data.results);
setTotalRows(response.data.count);
dispatch(LOADING_END());
} catch (error) {
console.error("Error fetching data:", error);
}
};
const handleRemoveFilter = async (event) => {
event.preventDefault();
setSelectedAge1(0);
setSelectedAge2(0);
dispatch(LOADING_START());
setTextValue("");
dispatch(
cityGetHatchingInfoFull({
age1: 0,
age2: 0,
tab: "unknown",
textValue: textValue,
})
);
try {
const response = await axios.get(
`poultry_hatching?role=${getRoleFromUrl()}&page=${page}&page_size=${perPage}&search=filter&value=${textValue}&unknown=true`
);
setData(response.data.results);
setTotalRows(response.data.count);
} catch (error) {
console.error("Error fetching data:", error);
} finally {
dispatch(LOADING_END());
}
};
const [lastUpdateData, setLastUpdateData] = useState();
useEffect(() => {
async function fetchData() {
try {
const response = await axios.get(`last_update/?type=poultry_hatching`);
setLastUpdateData(response.data);
} catch (error) {
console.error("Error fetching data:", error);
}
}
fetchData();
}, []);
const tableTitle = (
);
const { hatchingInfoFull } = useSelector((state) => state.citySlice);
useEffect(() => {
dispatch(
cityGetHatchingInfoFull({
age1: selectedAge1,
age2: selectedAge2,
tab: "unknown",
textValue: textValue,
})
);
}, []);
return (
{tableTitle}
);
};