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 { DRAWER } from "../../../../lib/redux/slices/appSlice";
import EditIcon from "@mui/icons-material/Edit";
import { Grid } from "../../../../components/grid/Grid";
import { provinceFinancialGetFinalFactorsService } from "../../services/province-financial-get-final-factors-service";
import { Factor } from "../factor/Factor";

export const FinalFactorsRequests = () => {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { provinceFinancialGetFinalFactors } = useSelector(
    (state) => state.provinceFinancialSlice
  );
  const [dataTable, setDataTable] = useState([]);

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

  useEffect(() => {
    const d = provinceFinancialGetFinalFactors?.map((item, i) => {
      return [
        i + 1,
        item?.poultry?.orderCode,
        item?.poultry?.poultry?.unitName,
        item?.poultry?.poultry?.userprofile?.mobile,
        item?.poultry?.quantity + " قطعه",
        <IconButton
          key={i}
          color="primary"
          onClick={() => {
            dispatch(
              DRAWER({
                title: "فاکتور نهایی",
                // right: !(window.innerWidth <= 600),
                top: !(window.innerWidth <= 600),
                bottom: window.innerWidth <= 600,
                content: (
                  <Grid contianer>
                    <Factor
                      process={item.poultry.process}
                      // id={item.poultry.process.poultry.poultryRequestId}
                    />
                  </Grid>
                ),
              })
            );
          }}
        >
          <EditIcon />
        </IconButton>,
        <IconButton
          key={i}
          aria-label="delete"
          color="primary"
          onClick={() => {
            navigate(
              ROUTE_PROVINCE_FINANCIAL_FILE +
                item.poultry.process.poultry.poultryRequestId
            );
          }}
        >
          <PlagiarismIcon />
        </IconButton>,
      ];
    });
    setDataTable(d);
  }, [provinceFinancialGetFinalFactors]);

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