import { Location } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BaseForm } from 'src/app/core/base/base-form';
import { BranchService } from 'src/app/core/service/settings/branch.service';
import { TestingFieldService } from 'src/app/core/service/settings/testing-field.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { ChangeTestingRoomService } from 'src/app/core/service/testing-register/change-testing-room.service';
import { TestingCenterService } from 'src/app/core/service/TestingCenter/testing-center.service';

@Component({
  selector: 'app-dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DialogComponent extends BaseForm implements OnInit {
  testingCenterList: any = [];
  branchList: any = [];
  testingFieldList: any = [];
  filteredTestingFieldList: any = [];
  filteredTestingCenterList : any = [];
  constructor(
    public fb: FormBuilder,
    public activeRoute: ActivatedRoute,
    public location: Location,
    public router: Router,
    public dialogRef: MatDialogRef<DialogComponent>,
    private testingCenterSV: TestingCenterService,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private branchSV: BranchService,
    private testingFieldSV: TestingFieldService,
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService,
    private changeTestingRoomSV: ChangeTestingRoomService
  ) {
    super(router,activeRoute,fb,location)
   }

  ngOnInit(): void {
    this.form.patchValue(this.data.data)
    this.testingCenterSV.getAll().pipe(
      tap(x => this.testingCenterList = x)
    ).subscribe()
    this.branchSV.getAll().pipe(
      tap(x => this.branchList = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
    this.testingFieldSV.getAll().pipe(
      tap(x => this.testingFieldList = x)
    ).subscribe()
  }

  createForm(): FormGroup {
    return this.fb.group({
      testing_information_uid: null,
      branch_uid: null,
      testing_field_uid: null,
      testing_center_uid: null,
      testing_datetime: null,
      period_id: null,
      new_period_id: null,
      new_branch_uid: [null,Validators.required],
      new_testing_field_uid: [null,Validators.required],
      new_testing_center_uid: [null,Validators.required],
      new_testing_datetime: [null,Validators.required],
      room_remark: [null,Validators.required]
    })
  }

  filterTestingField(uid){
    this.form.get('new_testing_field_uid').setValue(null)
    this.filteredTestingFieldList = this.testingFieldList.filter(x => x.branch_uid == uid)
  }

  findTestingCenter(){
    const form = this.form.getRawValue()
    const find = this.testingCenterList.filter(x => (x.branch_uid == form.new_branch_uid) && (x.testing_field_uid == form.new_testing_field_uid))
    this.filteredTestingCenterList = find
  }

  confirm(){
    const form = this.form.getRawValue()
    this.changeTestingRoomSV.update2(form).pipe(
      tap(x => this.swSV.updateSuccess('ย้ายห้องสำเร็จ')),
      tap(x => this.dialogRef.close()),
      catchError(err => {
        this.swSV.errText(err.error)
        return throwError(err)
      })
    ).subscribe()
  }

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

}
