import {Component, OnInit, ChangeDetectionStrategy, Input, OnChanges, ChangeDetectorRef, Output, EventEmitter} from '@angular/core';
import {BaseList} from '../../../../../../core/base/base-list';
import {TestingRegisterService} from '../../../../../../core/service/testing-register/testing-register.service';
import {tap} from 'rxjs/operators';
import {any} from 'codelyzer/util/function';
import {UserCountersService} from '../../../../../../core/service/permission/user-counters.service';

@Component({
  selector: 'app-register-summary-summary',
  templateUrl: './register-summary-summary.component.html',
  styleUrls: ['./register-summary-summary.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RegisterSummarySummaryComponent extends BaseList  implements OnInit , OnChanges {
  @Input() userData: any = [];
  @Input() CandidateTypeName: any = [];
  @Input() registerList: any = [];
  @Output() onLogoutcouter = new EventEmitter();
  counterDetail;
  listsummary;
  totalfee:number = 0 ;
  studentcount = 0;
  subjectcount = 0;
  constructor(
    private tesingregisterSV: TestingRegisterService,
    private userCounterSV: UserCountersService,
    private cdr: ChangeDetectorRef
  ) {
    super();
  }

  ngOnInit() {
  }

  ngOnChanges(){
    console.log(this.userData);
    if (this.userData.counter_no != null && this.userData.counter_uid != null){
      this.userCounterSV.getCounterSummary(this.userData.counter_no, this.userData.counter_uid , this.userData.counter_periods[0].period).pipe(
        tap(x => this.counterDetail = x),
        tap(x => this.cdr.detectChanges()),
      ).subscribe();
    }

    // if (this.registerList != null){
    //   this.studentcount = +this.registerList.length;
    //   for (let i = 0; i < this.registerList.length; i++) {
    //     const fee: number = +this.registerList[i].testing_fee;
    //     this.tesingregisterSV.get(this.registerList[i].testing_register_uid).pipe(
    //       tap(x => {
    //         const countSub: any = x;
    //         this.subjectcount += countSub.testing_subject_selecteds.length;
    //       }),
    //       tap(x => {
    //         this.cdr.detectChanges();
    //       }),
    //     ).subscribe(
    //     );
    //     this.totalfee += fee;
    //   }
    //   this.cdr.detectChanges();
    // }
  }

  updateSummary(){
    this.userCounterSV.getCounterSummary(this.userData.counter_no, this.userData.counter_uid, this.userData.counter_periods[0].period).pipe(
      tap(x => this.counterDetail = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  logoutcounter(){
    this.onLogoutcouter.emit();
  }


}
