import { Component } from "@angular/core";
import { TableMonitoringService } from "../monitoring.service";
import { Router } from "@angular/router";
import { BuildingService } from "../../service/monitoring-api.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[];
  topUseData: any;
  searchTerm: string = "";
  colorChart: string = "";

  constructor(
    private monitoringService: TableMonitoringService,
    private monitoringApiService: BuildingService,
    private router: Router
  ) {}

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

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

  listBuilding() {
    this.monitoringApiService.listBuilding().subscribe((res) => {
      this.data = res.data;
      this.filteredRows = res.data;
    });
  }

  // buildingData() {
  //   this.monitoringApiService.getBuildingData().subscribe((res) => {
  //     console.log(res);
  //   });
  // }

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

  viewRow(row) {
    this.router.navigate(["/monitoring/view-new-building", row.id]);
  }

  editRow(row) {
    this.router.navigate(["/monitoring/edit-new-building", row.id]);
  }

  viewRoom(row) {
    this.router.navigate(["/monitoring/monitoring-room", row.id]);
  }
}
