import { SelectionModel } from '@angular/cdk/collections';
import { Component, OnInit, ChangeDetectionStrategy, Input } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { filter, switchMap, tap } from 'rxjs/operators';
import { DailyRegisterPaymentTotalService } from 'src/app/core/service/report/daily-register/daily-register-payment-total.service';
import { DailyRegisterPaymentService } from 'src/app/core/service/report/daily-register/daily-register-payment.service';
import { PeriodRegisterPaymentOnlineService } from 'src/app/core/service/report/period-register/period-register-payment-online.service';
import { PeriodRegisterPaymentService } from 'src/app/core/service/report/period-register/period-register-payment.service';
import { PeriodicElement } from 'src/app/feature/manage-test-center/presenter/manage-test-center-table/manage-test-center-table.component';
import { DialogComponent } from '../../dialog/dialog.component';

@Component({
  selector: 'daily-summary-list',
  templateUrl: './daily-summary-list.component.html',
  styleUrls: ['./daily-summary-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DailySummaryListComponent implements OnInit {
  test = ['1','2','3']
  column = ['0','1','2','3','4','5','6','7']
  @Input() dailySummary : any =[];
  selection = new SelectionModel<PeriodicElement>(true, []);
  constructor(
    private dailyRegisterPaymentSV: DailyRegisterPaymentService,
    private dialog: MatDialog,
    private dailyRegisterPaymentTotalSV: DailyRegisterPaymentTotalService,
    private periodRegisterPaymentSV: PeriodRegisterPaymentService
  ) { }

  ngOnInit(): void {
  }

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

  masterToggle() {
    this.isAllSelected() ?
        this.selection.clear() :
        this.dailySummary?.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}`;
  }


  reportEtest101(){
    const obj = {
      data: this.dailySummary,
      type: '101'
    }
    const dialogRef = this.dialog.open(DialogComponent, {
      width: '500px',
      data: obj,
    });

    dialogRef.afterClosed().pipe(
      filter(x => x.obj),
      switchMap(x => {
        if(x.obj.is_testing_information_report){
          return window.open(this.dailyRegisterPaymentSV.etest101Information(x.obj))
        }else{
          return window.open(this.dailyRegisterPaymentSV.etest101(x.obj))
        }
      })
    ).subscribe()
  }

  reportEtest102(){
    const obj = {
      data: this.dailySummary,
      type: '102',
      date: this.dailySummary[0].payment_date,
      counter_no: this.dailySummary[0].counter_no
    }
    const dialogRef = this.dialog.open(DialogComponent, {
      width: '500px',
      data: obj,
    });
    dialogRef.afterClosed().pipe(
      filter(x => x.obj),
      switchMap(x => {
        if(x.obj.is_testing_information_report){
          return window.open(this.dailyRegisterPaymentTotalSV.etest102Information(x.obj))
        }else{
          return window.open(this.dailyRegisterPaymentTotalSV.etest102(x.obj))
        }
      })
    ).subscribe()

  }

  reportEtest103(){
    const obj = {
      data: this.dailySummary,
      type: '103'
    }
    const dialogRef = this.dialog.open(DialogComponent, {
      width: '500px',
      data: obj,
    });

    dialogRef.afterClosed().pipe(
      filter(x => x.obj),
      switchMap(x => {
        if(x.obj.is_testing_information_report){
          return window.open(this.periodRegisterPaymentSV.etest103Information(x.obj))
        }else{
          return window.open(this.periodRegisterPaymentSV.etest103(x.obj))
        }
      })
    ).subscribe()
  }


  totalAmount(){
    let sum = 0
    this.selection.selected.map((x:any) => {
      sum += Number(x.total_amount)
    })
    return sum
  }

  totalStudent(){
    let sum = 0
    this.selection.selected.map((x:any) => {
      sum += Number(x.total_student)
    })
    return sum
  }

  totalSubject(){
    let sum = 0
    this.selection.selected.map((x:any) => {
      sum += Number(x.number_of_subjects)
    })
    return sum
  }
}

