import {Component, OnInit, ChangeDetectionStrategy, Inject} from '@angular/core';
import {BaseForm} from '../../../../../../core/base/base-form';
import {TestingInformationService} from '../../../../../../core/service/testing-information/testing-information.service';
import {AppTokenService} from '../../../../../../core/service/security/app-token.service';
import {FormBuilder} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {Location} from '@angular/common';
import {ExamNotificationService} from '../../../../../../core/service/testing-information/exam-notification.service';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {catchError, tap} from 'rxjs/operators';
import {throwError} from 'rxjs';

@Component({
  selector: 'app-confirm-approve-exam-dialog',
  templateUrl: './confirm-approve-exam-dialog.component.html',
  styleUrls: ['./confirm-approve-exam-dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ConfirmApproveExamDialogComponent  extends BaseForm implements OnInit{
  userData;
  userName;
  constructor(
    private TestinforSV: TestingInformationService,
    private appTokenSV: AppTokenService,
    public fb: FormBuilder,
    public router: Router,
    public activeRoute: ActivatedRoute,
    private swSV: SweetalertService,
    public location: Location,
    private examResultSV: ExamNotificationService,
    private dialogRef: MatDialogRef<ConfirmApproveExamDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data,
  ) {
    super(
      router,
      activeRoute,
      fb,
      location
    );
    this.form = this.fb.group({
      Username: null,
    });
  }

  ngOnInit(): void {
    this.userData = this.appTokenSV.user;
    this.userName = this.userData.login
  }

  save(){
    console.log(this.data)
    const form = this.form.getRawValue();
    this.TestinforSV.checkandopentest(this.data).pipe(
      tap(x => this.swSV.success('รอบสอบเปิดแล้ว')),
      tap(x => this.dialogRef.close()),
      // tap(x =>  this.subjectList$ = this.TestinforSV.getAll()),
      // tap(x => this.cdr.detectChanges()),
      catchError(err => {
        this.swSV.errOpenExamDetailText(err.error);
        return throwError(err);
      })
    ).subscribe();
  }

  cancel(){
    this.dialogRef.close();
  }

}
