import { Card } 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_PROVINCE_FINANCIAL_FILE } from "../../../../routes/routes";
import IconButton from "@mui/material/IconButton";
import { useDispatch, useSelector } from "react-redux";
import { provinceGetAllRequests } from "../../../province/services/province-get-all-requests";
import { format } from "date-fns-jalali";

export const ProvinceFinancialActiveRequests = () => {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { provinceAllRequests } = useSelector((state) => state.provinceSlice);
  const [dataTable, setDataTable] = useState([]);

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

  useEffect(() => {
    const filteredData = provinceAllRequests?.filter(
      (item, i) =>
        item.state === "accept" &&
        item?.poultryRequest?.finalState !== "archive"
    );
    const d = filteredData?.map((item, i) => {
      return [
        i + 1,
        item?.poultryRequest?.orderCode,
        format(new Date(item?.poultryRequest?.createDate), "yyyy/MM/dd"),
        format(new Date(item?.poultryRequest?.sendDate), "yyyy/MM/dd"),
        item?.poultryRequest?.process?.poultry?.poultryName,
        item?.poultryRequest?.process?.poultry?.poultryMobile,
        item?.poultryRequest?.process?.poultry?.poultryCity,
        item?.poultryRequest?.process?.poultry?.poultryProvince,
        item?.poultryRequest?.process?.poultry?.age,
        item?.poultryRequest?.process?.poultry?.poultryQuantity,
        <IconButton
          key={i}
          aria-label="delete"
          color="primary"
          onClick={() => {
            navigate(
              ROUTE_PROVINCE_FINANCIAL_FILE +
                item?.poultryRequest?.process?.poultry?.poultryRequestId
            );
          }}
        >
          <PlagiarismIcon />
        </IconButton>,
      ];
    });
    setDataTable(d);
  }, [provinceAllRequests]);

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