import { Button } from "@mui/material";
import moment from "moment";
import { Timer } from "../../../components/timer/Timer";
import GavelIcon from "@mui/icons-material/Gavel";
import { AuctionOfferPriceForm } from "../components/auction-offer-price-form/AuctionOfferPriceForm";
import { DRAWER } from "../../../lib/redux/slices/appSlice";
import { getRoleFromUrl } from "../../../utils/getRoleFromUrl";

export function getAuctionItemForTable(item, i, dispatch) {
  const auctionFinishDate = moment(
    new Date(item?.process?.auctionsList[0]?.date)
  );
  const currentDate = moment();
  const diff = auctionFinishDate.diff(currentDate);
  const auctionRemainedSeconds = moment.duration(diff).asSeconds();
  const highestBidedPrice = item.process?.killHouseAuctionsList
    ? item?.process?.killHouseAuctionsList[
        item?.process?.killHouseAuctionsList.length - 1
      ].fee
    : "پیشنهادی وجود ندارد!";

  return [
    item.id,
    item.orderCode,
    // item?.process?.provinceRequestAuctionList ? "استان" : "مرغدار",
    item?.process?.auctionsList[0]?.floorPrice,
    item?.process?.auctionsList[0]?.ceilingPrice,
    highestBidedPrice,
    item.quantity,
    item.sendDate,
    item.chickenBreed + " - " + item?.process?.poultry?.age,
    item.IndexWeight,
    item?.process?.city.cityOperatorProvince,
    item?.process?.city.cityOperatorCity,
    <Timer key={"auctiontimer" + i} seconds={auctionRemainedSeconds} />,
    getRoleFromUrl() === "KillHouse" && (
      <Button
        key={"auctionbidsumbmit" + i}
        variant="outlined"
        startIcon={<GavelIcon />}
        onClick={() => {
          dispatch(
            DRAWER({
              right: true,
              size: 500,
              title: "ثبت قیمت در مزایده",
              content: (
                <AuctionOfferPriceForm
                  data={item}
                  floorPrice={item?.process?.auctionsList[0]?.floorPrice}
                  ceilingPrice={item?.process?.auctionsList[0]?.ceilingPrice}
                  highestBidedPrice={highestBidedPrice}
                  auctionRemainedSeconds={auctionRemainedSeconds}
                />
              ),
            })
          );
        }}
      >
        ثبت قیمت
      </Button>
    ),
  ];
}
