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 useProvinceNewRequests from "../../hooks/useProvinceNewRequests";
import { ROUTE_PROVINCE_FILE } from "../../../../routes/routes";
import IconButton from "@mui/material/IconButton";
import { useDispatch } from "react-redux";
import { DRAWER } from "../../../../lib/redux/slices/appSlice";
import ProvinceCheckRequest from "../../../file/components/province-check-request/ProvinceCheckRequest";
import CreateIcon from "@mui/icons-material/Create";
import { format } from "date-fns-jalali";

export const ProvinceNewRequests = () => {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const provinceNewRequestsData = useProvinceNewRequests();
  const [dataTable, setDataTable] = useState([]);

  useEffect(() => {
    const d = provinceNewRequestsData?.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}
          color="primary"
          onClick={() =>
            dispatch(
              DRAWER({
                right: !(window.innerWidth <= 600),
                bottom: window.innerWidth <= 600,
                content: (
                  <ProvinceCheckRequest
                    file={item?.poultryRequest?.process}
                    id={item.poultryRequest?.process?.poultry?.poultryRequestId}
                  />
                ),
                title: "انجام عملیات استان",
              })
            )
          }
        >
          <CreateIcon />
        </IconButton>,
        <IconButton
          key={i}
          aria-label="delete"
          color="primary"
          onClick={() => {
            navigate(
              ROUTE_PROVINCE_FILE +
                item?.poultryRequest?.process?.poultry?.poultryRequestId
            );
          }}
        >
          <PlagiarismIcon />
        </IconButton>,
      ];
    });
    setDataTable(d);
  }, [provinceNewRequestsData]);

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