import { Card, IconButton } from "@mui/material";
import { useEffect, useState } from "react";
import { AdvancedTable } from "../../../../components/advanced-table/AdvancedTable";
import PlagiarismIcon from "@mui/icons-material/Plagiarism";
import { useNavigate } from "react-router-dom";
import { ROUTE_AVICULTURE_FILE } from "../../../../routes/routes";
import { useDispatch, useSelector } from "react-redux";
import { avicultureGetRequests } from "../../services/aviculture-requests";
import { formatJustDate } from "../../../../utils/formatTime";

export const AvicultureRejectedRequests = () => {
  const navigate = useNavigate();
  const [dataTable, setDataTable] = useState([]);
  const { avicultureRequests } = useSelector((state) => state.avicultureSlice);

  const dispatch = useDispatch();

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

  useEffect(() => {
    const filteredData = avicultureRequests?.filter(
      (item) =>
        item.stateProcess === "rejected" || item.provinceState === "rejected"
    );
    const d = filteredData?.map((item, i) => {
      return [
        i + 1,
        item.orderCode,
        formatJustDate(item?.createDate),
        formatJustDate(item?.sendDate),
        item?.process?.poultry?.poultryName,
        item?.process?.poultry?.poultryMobile,
        item?.process?.poultry?.poultryCity,
        item?.process?.poultry?.poultryProvince,
        item?.process?.poultry?.age,
        item?.process?.poultry?.poultryQuantity,
        <IconButton
          key={item?.orderCode}
          aria-label="delete"
          color="primary"
          onClick={() =>
            navigate(
              ROUTE_AVICULTURE_FILE + item?.process?.poultry?.poultryRequestId
            )
          }
        >
          <PlagiarismIcon />
        </IconButton>,
      ];
    });

    setDataTable(d);
  }, [avicultureRequests]);

  const [tableDataCol] = useState([
    "ردیف",
    "کد سفارش",
    "تاریخ ثبت درخواست",
    "تاریخ درخواست",
    "مرغدار",
    "تلفن مرغدار",
    "شهر",
    "استان",
    "سن مرغ",
    "تعداد",
    "مشاهده",
  ]);
  return (
    <Card>
      <AdvancedTable
        name={"درخواست های رد شده"}
        columns={tableDataCol}
        data={dataTable}
      />
    </Card>
  );
};
