import React, { useState, useEffect } from "react";
import "../../assets/styles/room-list.css";
import { RoomBox } from "../../components/rooms/roomBox";
import { formatRoomStats } from "../../api/room";



export const RoomList = ({ statsParam, onRoomClick }) => {
  const [rooms, setRooms] = useState([]);

  useEffect(() => {
    if (statsParam.rooms) {
      console.log(statsParam.rooms);
      let formatedStats = formatRoomStats(statsParam);
      setRooms(formatedStats.rooms);
    }
  }, [statsParam]);

  const handleRoomClick = (roomId) => {
    onRoomClick(roomId);
  };

  return (
    <div>
      <h2>Liste des chambres</h2>
      <div className="list-container">
        {rooms &&
          rooms.map((room) => (
            <RoomBox room={room} key={room._id} onRoomClick={handleRoomClick} />
          ))}
      </div>
    </div>
  );
};
