import {Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BranchService} from '../../../../../../core/service/settings/branch.service';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {catchError, tap} from 'rxjs/operators';
import {throwError} from 'rxjs';
import {RegionService} from '../../../../../../core/service/region/region.service';
import {ProviceService} from '../../../../../../core/service/province/provice.service';

@Component({
  selector: 'app-testcenter-dialog',
  templateUrl: './testcenter-dialog.component.html',
  styleUrls: ['./testcenter-dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TestcenterDialogComponent implements OnInit {
  form;
  statusMock = [{id: 1, name: 'ใช้งาน'}, {id: 2, name: 'ยกเลิก'}];

  regionMock = [];

  provinceMock = [];
  isEdit: boolean = false
  selectAlready = false;
  provinceMock2 = [{id: 3, name: 'จังหวัด 3 '}, {id: 4, name: 'จังหวัด 4'}];
  constructor(
    private fb: FormBuilder,
    private branchservice: BranchService,
    private regionSV: RegionService,
    private provinceSV: ProviceService,
    private swSV: SweetalertService,
    private dialogRef: MatDialogRef<TestcenterDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data,
    private cdr: ChangeDetectorRef
  ) {
    this.form = this.fb.group({
      status_id: 1,
      created_by: null,
      created_datetime: null,
      updated_by: null,
      updated_datetime: null,
      deleted_by: null,
      deleted_datetime: null,
      branch_uid: null,
      branch_code: [null],
      branch_name_th: [null,Validators.required],
      branch_name_en: null,
      province_uid: [null,Validators.required],
      region_uid: [null,Validators.required],
    });
  }

  ngOnInit(): void {
    this.regionSV.getAll().subscribe(
      result => this.regionMock = result,
    );
    this.provinceSV.getAll().subscribe(
      result => this.provinceMock = result,
    );
    if (this.data){
      this.isEdit = true
      this.branchservice.get(this.data).pipe(
        tap(x => this.form.patchValue(x)),
        tap(x => this.cdr.detectChanges())
      ).subscribe();
    }
  }

  save(){
    const form = this.form.getRawValue();
    if (this.data){
      this.branchservice.update2(form).pipe(
        tap(x => this.swSV.updateSuccess()),
        tap(x => this.dialogRef.close()),
        catchError(err => {
          this.swSV.err();
          this.dialogRef.close();
          return throwError(err);
        })
      ).subscribe();
    } else {
      this.branchservice.add(form).pipe(
        tap(x => this.swSV.saveSuccess()),
        tap(x => this.dialogRef.close()),
        catchError(err => {
          this.swSV.err();
          this.dialogRef.close();
          return throwError(err);
        })
      ).subscribe();
    }
  }

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

  setProvince(selectObject){
    if (this.selectAlready === true){
      this.provinceSV.getAll().subscribe(
        result => {
          this.provinceMock = result;
          this.provinceMock = this.provinceMock.filter((x) => {
            return x.region_uid === selectObject;
          });
        }
      );
    }else {
      this.provinceMock = this.provinceMock.filter((x) => {
        return x.region_uid === selectObject;
      });
      this.selectAlready = true;
    }
  }
}
