import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, Inject} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {CheckAvailableSeatService} from '../../../../../../core/service/testing-register/check-available-seat.service';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {catchError, tap} from 'rxjs/operators';
import {throwError} from 'rxjs';
import {BranchService} from '../../../../../../core/service/settings/branch.service';
import {TestingFieldService} from '../../../../../../core/service/settings/testing-field.service';
import {TestingCenterService} from '../../../../../../core/service/TestingCenter/testing-center.service';
import {CheckAvailableRoomService} from '../../../../../../core/service/testing-register/check-available-room.service';

@Component({
  selector: 'app-dialog-change-room',
  templateUrl: './dialog-change-room.component.html',
  styleUrls: ['./dialog-change-room.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DialogChangeRoomComponent implements OnInit {
  subjectList = [];
  subjectList2 = [];
  subjectList3 = [];
  roomData: any;
  form;
  setupdata = {
    testing_center_uid: null,
    testing_center_name_th: null,
    branch_uid: null,
    branch_name_th: null,
    testing_field_uid: null,
    testing_field_name_th: null,
    testing_room_uid: null,
    testing_room_name_th: null,
    testing_auth_name: null,
  };
  constructor(
    private fb: FormBuilder,
    private testcenterSV: TestingCenterService,
    private testFiledSV: TestingFieldService,
    private branchSV: BranchService,
    private testingFieldSV: TestingFieldService,
    private dialogRef: MatDialogRef<DialogChangeRoomComponent>,
    private checkavailableseatSV: CheckAvailableSeatService,
    private checkavailableroomSV: CheckAvailableRoomService,
    private swSV: SweetalertService,
    private cdf: ChangeDetectorRef,
    @Inject(MAT_DIALOG_DATA) public data
  ) {
    this.form = this.fb.group({
      testing_subject_selected_uid: null,
      branch_uid: null,
      branch_name_th: null,
      testing_field_uid: null,
      testing_field_name_th: null,
      testing_center_uid: null,
      testing_room_name_th: null,
    });
  }

  ngOnInit(): void {
    this.form.get('testing_subject_selected_uid').setValue(this.data.testing_subject_selected_uid);
    this.branchSV.getAll().pipe(
      tap(x => console.log(x)),
      tap(x => this.subjectList = x),
    ).subscribe();
    this.testingFieldSV.getAll().pipe(
      tap(x => console.log(x)),
      tap(x => this.subjectList2 = x),
    ).subscribe();
    this.testcenterSV.getAll().pipe(
      tap(x => console.log(x)),
      tap(x => this.subjectList3 = x),
    ).subscribe();
  }

  setFormtestcentername(testcenterData, choose){
    console.log(testcenterData);
    console.log(choose);
    if (choose === 'branch_name_th'){
      this.setupdata.branch_name_th  = testcenterData.branch_name_th;
      this.setupdata.branch_uid = testcenterData.branch_uid;
      this.form.get('branch_uid').setValue(testcenterData.branch_uid);
      this.testingFieldSV.getAll().pipe(
        tap(x => console.log(x)),
        tap(x => this.subjectList2 = x),
      ).subscribe(
        result => {
          this.subjectList2 = this.subjectList2.filter((x) => {
            return x.branch_uid === testcenterData.branch_uid;
          });
        }
      );
    }
    if (choose === 'testing_field_name_th'){
      this.setupdata.testing_field_name_th  = testcenterData.testing_field_name_th;
      this.setupdata.testing_field_uid  = testcenterData.testing_field_uid;
      this.form.get('testing_field_uid').setValue(testcenterData.testing_field_uid);
      this.testcenterSV.getAll().pipe(
        tap(x => console.log(x)),
        tap(x => this.subjectList3 = x),
      ).subscribe(
        result => {
          this.subjectList3 = this.subjectList3.filter((x) => {
            return x.testing_field_uid === testcenterData.testing_field_uid;
          });
        }
      );
    }
    if (choose === 'testing_room_name_th'){
      this.setupdata.testing_room_name_th  = testcenterData.testing_room_name_th;
      this.setupdata.testing_room_uid  = testcenterData.testing_center_uid;
      this.form.get('testing_center_uid').setValue(testcenterData.testing_center_uid);
      this.setupdata.testing_center_uid  = testcenterData.testing_center_uid;
    }
  }

  save(){
    const form = this.form.getRawValue();
    console.log(this.form.getRawValue());
    // this.checkavailableroomSV.update2(form).pipe(
    //   tap(x => this.swSV.updateSuccess()),
    //   tap(x => this.dialogRef.close()),
    //   catchError(err => {
    //     this.swSV.err();
    //     this.dialogRef.close('err');
    //     return throwError(err);
    //   })
    // ).subscribe();
  }

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

}
