import { SelectionModel } from '@angular/cdk/collections';
import { Component, OnInit, ChangeDetectionStrategy, Input, OnChanges, Output, EventEmitter } from '@angular/core';
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BaseList } from 'src/app/core/base/base-list';
import { StudentPaymentService } from 'src/app/core/service/student-payment/student-payment.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { PeriodicElement } from 'src/app/feature/manage-test-center/presenter/manage-test-center-table/manage-test-center-table.component';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ListComponent extends BaseList implements OnInit,OnChanges {
  @Input() confirmList: any = [];
  @Output() delete = new EventEmitter()
  @Output() confirm = new EventEmitter()
  @Output() search =  new EventEmitter()
  column = ['1','2','3','4','5','6','7']
  selection = new SelectionModel<PeriodicElement>(true, []);
  query = {
    date: null
  }
  constructor(
    private studentPaymentSV: StudentPaymentService,
    private swSV: SweetalertService
  ) {
    super()
   }

  ngOnInit(): void {

  }

  ngOnChanges(){
    this.confirmList = this.updateMatTable(this.confirmList? this.confirmList: [])
  }

  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.confirmList?.data.length;
    return numSelected === numRows;
  }

  masterToggle() {
    this.isAllSelected() ?
        this.selection.clear() :
        this.confirmList?.data.forEach((row:any) => this.selection.select(row));
  }

  checkboxLabel(row?: PeriodicElement): string {
    if (!row) {
      return `${this.isAllSelected() ? 'select' : 'deselect'} all`;
    }
    return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${row.position + 1}`;
  }

  save(){
    this.confirm.emit(this.selection.selected)
  }

  clear(){
    this.selection.clear()
  }

  onSearch(event){
    this.search.emit(event)
  }

  deletePayment(){
    this.delete.emit(this.selection.selected)
  }

}
