import { ThrowStmt } from '@angular/compiler';
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { tap } from 'rxjs/operators';
import { CardPrintingService } from 'src/app/core/service/card-print/card-printing.service';
import { TestingCardService } from 'src/app/core/service/report/exam-card-print/testing-card.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-info',
  templateUrl: './info.component.html',
  styleUrls: ['./info.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class InfoComponent implements OnInit {
  query = {
    code: null,
    test1: null
  }
  data;
  list:any = []
  constructor(
    private cardSV: CardPrintingService,
    private printSV: TestingCardService,
    private swSV: SweetalertService
  ) { }

  ngOnInit(): void {
    this.list = this.cardSV.studentList
  }

  onSearch(){
    this.data = this.list.find(x => x.student_code == this.query.code)
    if(!this.data) this.swSV.errText('ไม่พบรหัสนักศึกษา')
  }

  print(){
    if(!this.data){
      this.swSV.errText('กรุณากรอกรหัสนักศึกษา')
    } else{
      const fileURL = this.printSV.print(this.data.tests_uid)
      window.open(fileURL)
    }
  }

}
