import { Component } from "@angular/core"; import { AddEditMasterComponent } from "../add-edit-master/add-edit-master.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 { AddEditMasterBuildingComponent } from "./add-edit-master-building/add-edit-master-building.component"; import { LoginService } from "../../service/login.service"; import { ToastrService } from "ngx-toastr"; @Component({ selector: "app-master-building", templateUrl: "./master-building.component.html", styleUrls: ["./master-building.component.css"], }) export class MasterBuildingComponent { data: any; filteredRows: any[]; searchTerm: string = ""; rows: any = []; public breadcrumb: any; constructor( private tableApiservice: TableApiService, private modalService: NgbModal, private router: Router, private monitoringApiService: BuildingService, private authService: LoginService, private toastr: ToastrService ) {} ngOnInit() { this.authService.checkTokenAndRedirect(); this.breadcrumb = { mainlabel: "Master Building", links: [ { name: "Home", isLink: false, }, { name: "Master Building", isLink: false, }, ], }; this.fetchData(); } fetchData() { this.monitoringApiService.getMasterBuildingData().subscribe((res) => { this.data = res.results.data; // this.filteredRows = this.data; this.filteredRows = this.data.sort((a, b) => b.id - a.id); }); } 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) ); } openAddMasterModal() { const modalRef = this.modalService.open(AddEditMasterBuildingComponent, { size: "lg", backdrop: "static", keyboard: false, }); modalRef.componentInstance.mode = "add"; modalRef.result.then( (result) => { if (result) { if ( this.filteredRows.some( (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() ) ) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, }); } else { this.monitoringApiService .postMasterBuildingParam(result) .subscribe((res) => { this.fetchData(); }); } } // if (result) { // this.monitoringApiService // .postMasterBuildingParam(result) // .subscribe((res) => { // this.fetchData(); // }); // } }, (reason) => { console.log(`Dismissed: ${reason}`); } ); } editRow(row) { const modalRef = this.modalService.open(AddEditMasterBuildingComponent, { size: "lg", backdrop: "static", keyboard: false, }); modalRef.componentInstance.dataRow = row; modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { if (result) { if ( this.filteredRows.some( (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() ) ) { if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { this.monitoringApiService .putMasterBuildingParam(result, row.id) .subscribe((res) => { this.fetchData(); }); } else { if ( this.filteredRows.some( (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() ) ) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, }); } } } else { this.monitoringApiService .putMasterBuildingParam(result, row.id) .subscribe((res) => { this.fetchData(); }); } } // if (result) { // console.log(result); // console.log(this.filteredRows); // if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status_id === result.statusId)) { // this.toastr.error("Warning", "Nama Gedung yang anda masukan double.", { // timeOut: 5000, // closeButton: true, // }); // } else { // this.monitoringApiService // .putMasterBuildingParam(result, row.id) // .subscribe((res) => { // this.fetchData(); // }); // } // } // if (result) { // this.monitoringApiService // .putMasterBuildingParam(result, 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 .deleteMasterBuildingParam(row.id) .subscribe((res) => { this.fetchData(); }, (error) => { console.error(error); this.toastr.error("Error", "Data sedang digunakan!", { timeOut: 2000, closeButton: true, }); }); } } }