import { formatDate } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef } from '@angular/core';
import { 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 {
  paymentselect: [] = [];
  obj = {
    date: null,
    start_date: null,
    end_date: null,
    is_testing_information_report: null,
    testing_information_uid: null,
    type: null,
    is_cash: false,
    is_payin: false,
    is_qrcode: false,
    is_credit_card: false,
  }
  testingInformationList: any = [];
  etest101:boolean = false
  etest102:boolean = false
  etest103:boolean = false
  constructor(
    public dialogRef: MatDialogRef<DialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private testingInformationSV: TestingInformationService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.testingInformationSV.getAll().pipe(
      tap(x => this.testingInformationList = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()

    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
    }
  }

  confirm(){
    if (this.paymentselect.length != 0) {
        this.obj.is_cash = false;
        this.obj.is_payin = false;
        this.obj.is_qrcode = false;
        this.obj.is_credit_card = false;
        this.paymentselect.forEach(option => {
            switch (option) {
                case 'CASH':
                    this.obj.is_cash = true;
                    break;
                case 'PAYIN':
                    this.obj.is_payin = true;
                    break;
                case 'QRCODE':
                    this.obj.is_qrcode = true;
                    break;
                case 'CREDIT-CARD':
                    this.obj.is_credit_card = true;
                    break;
            }
        });
    }
    console.log(this.obj);
    if(this.etest101){
      this.obj.date = this.data.date
      this.dialogRef.close({obj:this.obj})
    }
    if(this.etest102){
      this.obj.date = this.data.date
      this.dialogRef.close({obj:this.obj})
    }
    if(this.etest103){
      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
  }

}
