import { formatDate } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { tap } from 'rxjs/operators';
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';

@Component({
  selector: 'app-dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DialogComponent implements OnInit {
  counterList: any = []
  counterNo;
  obj = {
    counter_no: null,
    period: null,
    date: null,
    start_date: null,
    end_date: null,
    is_testing_information_report: null,
    testing_information_uid: null
  }
  etest101: boolean = false
  etest102: boolean = false
  etest103: boolean = false
  period;
  periodList;
  testingInformationList;
  constructor(
    public dialog: MatDialog,
    public dialogRef: MatDialogRef<DialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private testingInformationSV: TestingInformationService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    if(this.data){
      if(this.data.type == '101') this.etest101 = true
      if(this.data.type == '102') this.etest102 = true
      if(this.data.type == '103') this.etest103 = true
    }
    this.testingInformationSV.getAll().pipe(
      tap(x => this.testingInformationList = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
    this.counterList = [...this.data.data].filter((value,index,array) => array.findIndex(x => value.counter_no == x.counter_no) == index)
    this.periodList = [...this.data.data].filter((value,index,array) => array.findIndex(x => value.period == x.period) == index)
  }

  confirm(){
    this.obj.date = formatDate(this.data.data[0].payment_date, 'yyyy-MM-dd', 'en-US');
    this.dialogRef.close({obj:this.obj})
  }

  confirm102(){
    this.obj.date = formatDate(this.data.date, 'yyyy-MM-dd', 'en-US');
    // this.obj.counter_no = this.data.counter_no
    this.dialogRef.close({obj:this.obj})
  }

  confirm103(){
    this.obj.start_date = formatDate(this.obj.start_date, 'yyyy-MM-dd', 'en-US')
    this.obj.end_date = formatDate(this.obj.end_date, 'yyyy-MM-dd', 'en-US')
    this.dialogRef.close({obj:this.obj})
  }

  display(uid){
    if (uid && this.testingInformationList) return this.testingInformationList.find(x => x.testing_information_uid === uid)?.testing_information_name
  }

}
