import {Component, Inject} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
import {Dialog} from '../../../../classes/base_components/dialog/dialog';
import {EditComponent} from '../../../../classes/base_components/edit/edit.component';
import {setErrors} from '../../../../classes/utils';
import {Universities} from '../../../../classes/resorcess/universities.service';


@Component({
  selector: 'app-edit-roles',
  templateUrl: './editUniversities.component.html',
  styleUrls: ['./editUniversities.component.scss']
})

export class EditUniversitiesComponent extends EditComponent {
  public loading = false;
  nameCtrl = new FormControl('', [Validators.required]);
  form: FormGroup;
  uni: Universities;
  constructor(private fb: FormBuilder,
              protected  dialog: Dialog,
              public  dialogRef: MatDialogRef<EditUniversitiesComponent>,
              @Inject(MAT_DIALOG_DATA)  data,
  ) {
    super();
    this.uni = data;
    this.form = this.fb.group({
      nameCtrl: this.nameCtrl,
    });
  }

  onSubmit() {
    this.loading = true;
    if (!this.form.valid) {
      this.loading = false;
      return;
    }
    this.uni.save({},
      () => {
        this.loading = false;
        this.close();
      },
      (error) => {
        this.loading = false;
        const JsonV = error.errors;
        setErrors(JsonV, this.form.controls, this.dialog);
      });
  }

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





