import { Component } from "@angular/core";
import { Router } from "@angular/router";
import { TableApiService } from "src/app/_services/table-api.service";
import { BuildingService } from "../service/monitoring-api.service";
import Swal from "sweetalert2";

@Component({
  selector: "app-list-monitoring",
  templateUrl: "./list-monitoring.component.html",
  styleUrls: ["./list-monitoring.component.css"],
})
export class ListMonitoringComponent {
  public breadcrumb: any;
  data: any;
  filteredRows: any[];
  searchTerm: string = "";
  rows: any = [];

  constructor(
    private tableApiservice: TableApiService,
    private router: Router,
    private monitoringApiService: BuildingService
  ) {}

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

  fetchData() {
    this.monitoringApiService.getBuildingRoomList().subscribe((res) => {
      this.data = res;
      this.filteredRows = this.data.results.data;
    });
  }

  filterRows() {
    if (!this.searchTerm) {
      this.filteredRows = [...this.data.rows];
    } else {
      this.filteredRows = this.data.rows.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(["/list-monitoring/view", row.id]);
  }

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

  deleteRow(row) {
    const swalWithBootstrapButtons = Swal.mixin({
      customClass: {
        confirmButton: "btn-confirm",
        cancelButton: "btn-cancel",
        popup: "custom-swal-background",
      },
    });
    swalWithBootstrapButtons.fire({
      title: "Are you sure?",
      text: "Do you want to delete?",
      icon: "question",
      showCancelButton: true,
      confirmButtonText: "Yes",
      cancelButtonText: "No",
    }).then((result) => {
      if (result.isConfirmed) {
        Swal.fire({
          title: "Loading...",
          text: "Please wait",
          allowOutsideClick: false,
          customClass: {
            popup: "custom-swal-background",
          },
          didOpen: () => {
            Swal.showLoading();
          },
        });
        setTimeout(() => {
          this.monitoringApiService
            .deleteRoomBuilding(row.id)
            .subscribe((data) => {
              Swal.fire({
                title: "Confirmed!",
                text: "You clicked Yes.",
                icon: "success",
                customClass: {
                  popup: "custom-swal-background",
                },
              });
              this.fetchData();
            });
        }, 2000);
      } else if (result.isDismissed) {
        Swal.fire({
          title: "Cancelled",
          text: "You clicked No.",
          icon: "error",
          customClass: {
            popup: "custom-swal-background",
          },
        });
      }
    });
  }

  onTouchStart(event: Event) {
    event.preventDefault(); // Add this if necessary
  }
}
