import { Component } from "@angular/core"; import { AddEditMasterRoomComponent } from "./add-edit-master-room/add-edit-master-room.component"; import { TableApiService } from "src/app/_services/table-api.service"; import { NgbModal } from "@ng-bootstrap/ng-bootstrap"; import { Router } from "@angular/router"; import { BuildingService } from "../../service/monitoring-api.service"; import { LoginService } from "../../service/login.service"; @Component({ selector: "app-master-room", templateUrl: "./master-room.component.html", styleUrls: ["./master-room.component.css"], }) export class MasterRoomComponent { data: any; filteredRows: any[]; searchTerm: string = ""; rows: any = []; public breadcrumb: any; constructor( private modalService: NgbModal, private monitoringApiService: BuildingService, private authService: LoginService ) {} ngOnInit() { this.authService.checkTokenAndRedirect(); this.breadcrumb = { mainlabel: "Master Room", links: [ { name: "Home", isLink: false, }, { name: "Master Room", isLink: false, }, ], }; this.fetchData(); } fetchData() { this.monitoringApiService.getBuildingRoomList().subscribe((res) => { this.data = res; this.filteredRows = this.data.results.data.sort((a, b) => b.id - a.id); // this.data.results.data.sort((a, b) => b.id - a.id); console.log(this.filteredRows); }); } filterRows() { if (!this.searchTerm) { this.filteredRows = [...this.data.results.data]; } else { const searchTermLC = this.searchTerm.toLowerCase(); this.filteredRows = this.data.results.data.filter((row) => this.rowContainsSearchTerm(row, searchTermLC) ); } } rowContainsSearchTerm(row: any, searchTermLC: string): boolean { return ( row.roomEntity.name.toLowerCase().includes(searchTermLC) || row.roomEntity.description.toLowerCase().includes(searchTermLC) || row.buildingEntity.name.toLowerCase().includes(searchTermLC) || row.statusEntity.name.toLowerCase().includes(searchTermLC) ); } openAddMasterModal() { const modalRef = this.modalService.open(AddEditMasterRoomComponent, { size: "lg", backdrop: "static", // Add this line keyboard: false, // Add this line }); modalRef.componentInstance.mode = "add"; modalRef.result.then( (result) => { if (result) { const filteredData = { name: result.name, description: result.description, }; this.monitoringApiService .postMasterRoomParam(filteredData) .subscribe((res) => { const transformedData = { buildingId: result.buildingId, roomId: [res.data.id], statusId: result.statusId, }; this.monitoringApiService .postBatchBuilding(transformedData) .subscribe((res) => { this.fetchData(); }); }); } }, (reason) => { console.log(`Dismissed: ${reason}`); } ); } editRow(row) { const modalRef = this.modalService.open(AddEditMasterRoomComponent, { size: "lg", backdrop: "static", // Add this line keyboard: false, // Add this line }); modalRef.componentInstance.dataRow = row; modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { if (result) { const filteredData = { id: result.roomId, name: result.name, description: result.description, }; this.monitoringApiService .putMasterRoomParam(filteredData, row.roomId) .subscribe((res) => { console.log(res); const transformedData = { buildingId: result.buildingId, roomId: res.data.id, statusId: result.statusId, }; this.monitoringApiService .puttBuildingRoom(transformedData, row.id) .subscribe((res) => { this.fetchData(); }); }); } }, (reason) => { console.log(`Dismissed: ${reason}`); } ); } deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { this.monitoringApiService .deleteHeaderDetailParam(row.roomEntity.id) .subscribe((res) => { this.monitoringApiService .deleteRoomBuilding(row.id) .subscribe((res) => { this.fetchData(); }); }); } } }