import {Component, OnInit, ChangeDetectionStrategy, Input, OnChanges, ChangeDetectorRef, Output, EventEmitter} from '@angular/core';
import {SelectionModel} from '@angular/cdk/collections';
import {PeriodicElement} from '../../../../../manage-test-center/presenter/manage-test-center-table/manage-test-center-table.component';
import {BaseList} from '../../../../../../core/base/base-list';
import {MatDialog} from '@angular/material/dialog';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {TestingCenterService} from '../../../../../../core/service/TestingCenter/testing-center.service';
import {concatMap, filter, tap} from 'rxjs/operators';
import {StudentPaymentNotPaidService} from '../../../../../../core/service/student-payment/student-payment-not-paid.service';

@Component({
  selector: 'app-register-cancel-list',
  templateUrl: './register-cancel-list.component.html',
  styleUrls: ['./register-cancel-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RegisterCancelListComponent extends BaseList implements OnInit, OnChanges {
  @Input() studentRegisterList: any = [];
  @Output() OngetData = new EventEmitter();


  selection = new SelectionModel(true, []);

  /** Whether the number of selected elements matches the total number of rows. */
  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.studentRegisterList?.data?.length;
    return numSelected === numRows;
  }

  /** Selects all rows if they are not all selected; otherwise clear selection. */
  masterToggle() {
    this.isAllSelected() ?
      this.selection.clear() :
      this.studentRegisterList.data.forEach(row => this.selection.select(row));
  }

  /** The label for the checkbox on the passed row */
  checkboxLabel(row?: PeriodicElement): string {
    if (!row) {
      return `${this.isAllSelected() ? 'select' : 'deselect'} all`;
    }
    return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${
      row.position + 1
    }`;
  }
  column = ['1', '2', '3', '4', '5', '6', '7'];
  constructor(
    private studentPaymentNotPaidSV: StudentPaymentNotPaidService,
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService,
  ) {
    super();
  }

  ngOnInit(): void {
  }

  ngOnChanges(){
    this.studentRegisterList = this.updateMatTable(this.studentRegisterList);
    this.selection.clear()
  }

  confirmCancel(){
    let rowData:any = this.selection.selected;
    rowData = rowData.map((x) => {
      return x.user_testing_register_uid;
    });
    if (rowData.length !== 0){
      this.swSV.confirmText(`ต้องการยกเลิก ${this.selection.selected.length} รายการหรือไม่`).pipe(
        filter(x => x.isConfirmed),
        concatMap(x => {
          return this.studentPaymentNotPaidSV.putNotPaids(rowData).pipe(
            tap(x => this.swSV.success('ยกเลิกเรียบร้อย')),
            tap(x => this.selection.clear()),
            tap(x => this.OngetData.emit()),
            tap(x => this.cdr.detectChanges())
          );
        })
      ).subscribe();
    }else {
      this.swSV.error('กรุณาเลือกผู้สอบ')
    }
  }

}
