import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, Inject} from '@angular/core';
import {Observable, throwError} from 'rxjs';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {EnrollmentCounterPaymentsService} from '../../../../../../core/service/testing-register/enrollment-counter-payments.service';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {createQueryStringFromObject} from '../../../../../../shared/util/func';
import {catchError, concatMap, filter, tap} from 'rxjs/operators';
import {TestingRegisterEnrollmentService} from '../../../../../../core/service/enrollment-api/testing-register-enrollment.service';
import {TestingRecieptService} from '../../../../../../core/service/testing-register/testing-reciept.service';
import {ReceiptByPaymentCodeService} from '../../../../../../core/service/report/receipt-by-payment-code/receipt-by-payment-code.service';

@Component({
  selector: 'app-dialog-check-payment-counter',
  templateUrl: './dialog-check-payment-counter.component.html',
  styleUrls: ['./dialog-check-payment-counter.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DialogCheckPaymentCounterComponent implements OnInit {
  paymentData$ = new Observable();

  query = {
    student_code: null,
    counter_no: null,
    counter_pin: null,
  };

  constructor(
    private swSV: SweetalertService,
    private testingrecieptsSV: TestingRecieptService,
    private reportPaymentcode: ReceiptByPaymentCodeService,
    private enrollentCounterPaymentSV: EnrollmentCounterPaymentsService,
    private testingregisterEnrollSV: TestingRegisterEnrollmentService,
    private cdr: ChangeDetectorRef,
    private dialogRef: MatDialogRef<DialogCheckPaymentCounterComponent>,
    @Inject(MAT_DIALOG_DATA) public data
  ) { }

  ngOnInit(): void {
    this.query.counter_no = this.data.data.counter_no;
    this.query.counter_pin = this.data.data.counter_pin;
  }

  search(){
    const query = createQueryStringFromObject(this.query);
    console.log(query);
    if(query) return this.paymentData$ = this.testingrecieptsSV.search(`?${query}`);
  }

  print(paymentCode){
    // console.log(paymentCode);
    if (paymentCode !== null){
      this.reportPaymentcode.getPdfReportReceiptForAll(paymentCode).subscribe(res => {
        const fileURL = URL.createObjectURL(res);
        window.open(fileURL, '_blank');
      });
    }else {
      this.swSV.error('กรุณากดรับเงินใหม่');
    }
  }

}
