import { Component, OnInit, ViewChild } from '@angular/core'; import { CustomerAdministration } from "../../../../../models/admin/company/customer-administration/customer-administration"; import { CustomerAdministrationService } from '../../../../../services/admin/company/customer-administration/customer-administration.service'; import { CommonService } from '../../../../../services/common.service'; import { DataTableDirective } from 'angular-datatables'; import { Subject } from 'rxjs'; @Component({ selector: 'app-customer-administration-list', templateUrl: './customer-administration-list.component.html', styleUrls: ['./customer-administration-list.component.css'] }) export class CustomerAdministrationListComponent implements OnInit { modelCustomerAdministration = new CustomerAdministration(); CustomerAPIResponse: any; data: any; defaultValue: string = ''; _paramId: number = 0; companyid: number = 1; dtOptions: any = {} dtTrigger: Subject = new Subject(); @ViewChild(DataTableDirective) dtElement: DataTableDirective; showModal: boolean = false; deleteModal; historyModal; constructor( private service: CustomerAdministrationService, private common: CommonService, ) { } ngOnInit(): void { this.dtOptions = this.common.dataTableOptionsForSN(false, false, false, true, true, false, true); this.getCustomerList(); } ngOnDestroy(): void { // Do not forget to unsubscribe the event this.dtTrigger.unsubscribe(); } getCustomerList() { this.service.getCustomer(this.companyid,data => { this.CustomerAPIResponse = data; this.data = this.CustomerAPIResponse.objResponse.responseContent; for (let CustomerData of this.data) { CustomerData.IsDefaultCustomer = CustomerData.IsDefaultCustomer == true ? "Yes" : "No"; CustomerData.IsActive = CustomerData.IsActive == true ? "Yes" : "No"; } //dtTrigger to rerender again this.dtTrigger.next(); }, (error) => { console.error('Error: ' + error); return error(error); }); } SetDefault(CustomerId) { this.defaultValue = this.data.find(item => item.CustomerId == CustomerId).IsDefaultCustomer; if (this.defaultValue == 'Yes') { this.defaultValue = 'N'; } else { this.defaultValue = 'Y'; } this.service.SetDefaultById('Customer', CustomerId, this.defaultValue, 1, '10.10.10.1', data => { if (data.objResponse.statusCode == "200") { this.common.showSuccess("Customer changes applied successfully.", "Success") this.rerender(); } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } rerender(): void { this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => { // Destroy the table first dtInstance.destroy(); this.getCustomerList(); }); } showDeleteModal(id) { this.showModal = true; this.deleteModal = { tableName: 'Customer', id: id }; } showHistoryModal(historyFor) { this.showModal = true; this.historyModal = { historyFor: historyFor } this.deleteModal = ""; } getModalToggleValue(data) { this.showModal = data.toggle; if (data.isDeleteSuccess) { this.rerender(); } } }