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 useAvicultureRequests from "../../hooks/useAvicultureRequests";
import { formatJustDate } from "../../../../utils/formatTime";

export const AvicultureArchivedRequests = () => {
  const navigate = useNavigate();
  const [dataTable, setDataTable] = useState([]);
  const avicultureRequests = useAvicultureRequests("Poultry");
  const urlRole = window.location.pathname.split("/")[1];

  useEffect(() => {
    const fileUrl = "/" + urlRole + "/file/";

    const filteredData = avicultureRequests?.filter(
      (item, i) => item.finalState === "archive"
    );
    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(fileUrl + item?.process?.poultry?.poultryRequestId)
          }
        >
          <PlagiarismIcon />
        </IconButton>,
      ];
    });

    setDataTable(d);
  }, []);

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