import { Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { ChangeTestingDateService } from 'src/app/core/service/testing-register/change-testing-date.service';
import { CheckAvailableRoomService } from 'src/app/core/service/testing-register/check-available-room.service';
import { CheckAvailableSeatService } from 'src/app/core/service/testing-register/check-available-seat.service';
import {CheckAvailableRoomNoConditionService} from "../../../../../../core/service/testing-register/check-available-room-no-condition.service";

@Component({
  selector: 'app-change-dialog',
  templateUrl: './change-dialog.component.html',
  styleUrls: ['./change-dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChangeDialogComponent implements OnInit {
  room;
  seat;
  form;
  constructor(
    private changeTestingDateSV: ChangeTestingDateService,
    private fb: FormBuilder,
    private cdr: ChangeDetectorRef,
    private checkAvalSV: CheckAvailableSeatService,
    private checkAvaRoomNoConditionSV: CheckAvailableRoomNoConditionService,
    public dialog: MatDialog,
    public dialogRef: MatDialogRef<ChangeDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private swSV: SweetalertService
  ) {
    this.form = this.fb.group({
      manage_testing_uid: null,
      branch_uid: null,
      testing_field_uid: null,
      testing_center_uid: [null,Validators.required],
      testing_seat: [null,Validators.required]
    })
   }

  ngOnInit(): void {
    this.checkAvalSV.checkAval(this.data).pipe(
      tap(x => this.seat = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
    this.checkAvaRoomNoConditionSV.checkAvalroom(this.data).pipe(
      tap(x => this.room = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
    this.form.get('testing_field_uid').setValue(this.data.testing_field_uid)
    this.form.get('branch_uid').setValue(this.data.branch_uid)
    this.form.get('manage_testing_uid').setValue(this.data.manage_testing_uid)
  }

  confirm(){
    const form = this.form.getRawValue()
    this.changeTestingDateSV.approve(form).pipe(
      tap(x => this.swSV.success('อนุมัติสำเร็จ')),
      tap(x => this.dialogRef.close()),
      catchError(err => {
        this.swSV.errText('อนุมัติไม่สำเร็จ')
        return throwError(err)
      })
    ).subscribe()
  }

  cancel(){
    this.changeTestingDateSV.reject(this.data.manage_testing_uid).pipe(
      tap(x => this.swSV.success('ไม่อนุมัติสำเร็จ')),
      tap(x => this.dialogRef.close()),
      catchError(err => {
        this.swSV.errText('มีข้อผิดพลาดในการไม่อนุมัติ')
        return throwError(err)
      })
    ).subscribe()
  }

}
