import { Component, OnInit } from '@angular/core'; import { Subject } from 'rxjs'; import { BsModalRef, BsModalService } from 'ngx-bootstrap'; import { JournalService } from '../../../services/journal.service'; import { ToastrService } from 'ngx-toastr'; import { Router } from '@angular/router'; import { AlertModalComponent } from '../../common/alert-modal/alert-modal.component'; import { NGXLogger } from 'ngx-logger'; @Component({ selector: 'app-journol', templateUrl: './journol.component.html', styleUrls: ['./journol.component.css'] }) export class JournolComponent implements OnInit { tableData: any[]; dtOptions: DataTables.Settings = {}; dtTrigger: Subject = new Subject(); singleRowSelected: boolean selectedJournalId: number bsModalRef: BsModalRef; alertConfirm: boolean; constructor(private journalService: JournalService,private logger:NGXLogger ,private router: Router,private modalService: BsModalService, private toastr: ToastrService,) { } ngOnInit(): void { this.loadData(); } /* @purpose: load the data into the table */ loadData() { this.logger.debug('Loads the data from service'); this.journalService.getAllJournals().subscribe((result: { status: string, message: string, data }) => { this.tableData = result.data; this.dtTrigger.next(); } ) this.dtOptions = { columnDefs: [{ type: 'date', 'targets': [1] },{ width: '20%', "targets": 4 },{ width: '15%', "targets": 3 }], order: [[1, 'desc']], } this.singleRowSelected = false; } /* @usage: called when single checked box is checked unchecked @purpose: delete btn will be enabled or disabled based on selected row */ singleRowChecked(journal) { this.singleRowSelected = journal.isSelected; this.selectedJournalId = journal.journal_id; for (let i = 0; i < this.tableData.length; i++) { if (this.tableData[i].journal_id === journal.journal_id) this.tableData[i].isSelected = true; else this.tableData[i].isSelected = false; } } /* @usage: called whennew btn is clicked @purpose: route to new vendor */ newJournal() { this.router.navigate(['/admin/accounts/journals/new-Journal']); } /* @usage: called when delete is required @purpose: delete the selected records */ onDelete() { this.bsModalRef = this.modalService.show(AlertModalComponent, { backdrop: 'static', keyboard: false, animated: true, ignoreBackdropClick: true, initialState: { title: 'Alert', data: 'The selected journal entry will be deleted and cannot be retrieved later. Are you sure about deleting', buttonText: 'Cancel', deleteBtn: 'Delete', } }); let confirm: boolean; this.bsModalRef.content.event.subscribe(res => { this.alertConfirm = res; if (this.alertConfirm) { this.logger.debug('Delete operation started'); this.journalService.deleteTheJournal(this.selectedJournalId).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { var table = $('#journalGrid').DataTable(); table.destroy(); this.loadData(); this.toastr.success('Selected journal is deleted'); } }, (error) => { var table = $('#journalGrid').DataTable(); table.destroy(); this.loadData(); this.toastr.error('Selected journal cannot be deleted.'); this.logger.debug('Selected journal cannot be deleted.'); this.logger.error(error); }, ); } }); } }