import { Component } from "@angular/core";
import { TableMonitoringService } from "../monitoring.service";

@Component({
  selector: "app-building",
  templateUrl: "./building.component.html",
  styleUrls: ["./building.component.css"],
})
export class BuildingComponent {
  data: any;
  public breadcrumb: any;
  feedbacksdonutChart: any;
  donutChart1: any;
  filteredRows: any[];
  dataTop: any[];
  searchTerm: string = "";
  colorChart: string = "";

  constructor(private monitoringService: TableMonitoringService) {}

  ngOnInit() {
    this.breadcrumb = {
      mainlabel: "Dashboard",
      links: [
        {
          name: "Dashboard",
          isLink: false,
        },
      ],
    };

    this.donutChart1 = {
      type: "Pie",
      options: {
        fullwidth: true,
        height: "250px",
        donut: true,
        donutWidth: 60,
        startAngle: 270,
        total: 200,
        showLabel: false,
      },
    };
    this.fetchData();
  }

  fetchData() {
    this.monitoringService.getMonitoringBuildingData().subscribe((res) => {
      this.data = res
      this.filteredRows = res
      this.dataTop = res.map(item => ({
        label: item.topUse.label,
        value: item.topUse.value
      }));
      
    });
  }

  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)
    );
  }

}
