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


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

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

  onSubmit() {
    this.loading = true;
    if (!this.form.valid) {
      this.loading = false;
      return;
    }
    this.city.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);
  }
}





