import { Card, IconButton } from "@mui/material";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { AdvancedTable } from "../advanced-table/AdvancedTable";
import PlagiarismIcon from "@mui/icons-material/Plagiarism";
import { getRoleFromUrl } from "../../utils/getRoleFromUrl";
import { useDispatch, useSelector } from "react-redux";
import { getRequestsAwaitingInspection } from "./service";
import { format } from "date-fns-jalali";

export const RequestsAwaitingInspections = () => {
  const { awaitingInspectionRequests } = useSelector(
    (state) => state.generalSlice
  );

  const navigate = useNavigate();
  const [dataTable, setDataTable] = useState([]);
  const urlRole = window.location.pathname.split("/")[1];

  const dispatch = useDispatch();

  const role = getRoleFromUrl();

  useEffect(() => {
    dispatch(getRequestsAwaitingInspection(role + "&inspector"));
  }, []);

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

    const d = awaitingInspectionRequests?.map((item, i) => {
      return [
        i + 1,
        item.orderCode,
        format(new Date(item.createDate), "yyyy/MM/dd"),
        format(new Date(item.sendDate), "yyyy/MM/dd"),
        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);
  }, [awaitingInspectionRequests]);

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