import {Component, OnInit, ChangeDetectionStrategy, Inject} from '@angular/core';
import {catchError, tap} from 'rxjs/operators';
import {throwError} from 'rxjs';
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 {BaseForm} from '../../../../../../core/base/base-form';
import {AppTokenService} from '../../../../../../core/service/security/app-token.service';
import {TestingInformationService} from '../../../../../../core/service/testing-information/testing-information.service';

@Component({
  selector: 'app-confirm-reject-exam-dialog',
  templateUrl: './confirm-reject-exam-dialog.component.html',
  styleUrls: ['./confirm-reject-exam-dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ConfirmRejectExamDialogComponent 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<ConfirmRejectExamDialogComponent>,
    @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(){
    const form = this.form.getRawValue();
    this.TestinforSV.checkandclosetest(this.data).pipe(
      tap(x => this.swSV.success('รอบสอบปิดแล้ว')),
      tap(x => this.dialogRef.close()),
      // tap(x => this.cdr.detectChanges()),
      catchError(err => {
        this.swSV.errCloseExamDetailText(err.error);
        return throwError(err);
      })
    ).subscribe();
  }

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

}
