import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { TableMonitoringService } from "../monitoring.service";

@Component({
  selector: "app-room",
  templateUrl: "./room.component.html",
  styleUrls: ["./room.component.css"],
})
export class RoomComponent implements OnInit {
  public breadcrumb: any;
  mode: string;
  filteredRows: any[];
  data: any[];
  searchTerm: string = "";

  constructor(
    private route: ActivatedRoute,
    private monitoringService: TableMonitoringService
  ) {}

  ngOnInit() {
    this.route.data.subscribe((data) => {
      this.mode = data.mode;
    });
    this.breadcrumb = {
      mainlabel: "Room",
      links: [
        {
          name: "Dashboard",
          isLink: false,
          link: "/dashboard/sales",
        },
        {
          name: "Room",
          isLink: false,
        },
      ],
    };
    this.fetchData();
    
  }

  fetchData() {
    this.monitoringService.getMonitoringRoomData().subscribe((Response) => {
      this.data = Response;
      this.filteredRows = Response;
    });
  }

  filterRows() {
    if (!this.searchTerm) {
      this.filteredRows = [...this.data];
    } else {
      this.filteredRows = this.data.filter((row) =>
        this.rowContainsSearchTerm(row)
      );
    }
  }

  rowContainsSearchTerm(row: any): boolean {
    const searchTermLC = this.searchTerm.toLowerCase();
    return Object.values(row).some(
      (value) =>
        value !== null && value.toString().toLowerCase().includes(searchTermLC)
    );
  }
}
