import {Component, OnInit, TemplateRef, ViewChild} from '@angular/core';
import {ListComponent} from '../../../classes/base_components/list/list.component';
import {Dialog} from '../../../classes/base_components/dialog/dialog';
import {Http} from '@angular/http';
import {UnitsService} from '../../../classes/resorcess/units.service';
import {EditUnitiesComponent} from './editUnities/editUnities.component';
import {Base} from 'ngx-jsonapi/services/base';
import {Universities, UniversitiesService} from '../../../classes/resorcess/universities.service';
import {Cities, CitiesService} from '../../../classes/resorcess/cities.service';

@Component({
  selector: 'app-unites',
  templateUrl: './unites.component.html',
  styleUrls: ['./unites.component.scss']
})
export class UnitesComponent extends ListComponent implements OnInit {
  @ViewChild('filter_name') public filter_name_ref: TemplateRef<any>;
  @ViewChild('filter_id') public filter_id_ref: TemplateRef<any>;
  @ViewChild('filter_cities') public filter_cities_ref: TemplateRef<any>;
  @ViewChild('filter_universities') public filter_universities_ref: TemplateRef<any>;
  universities;
  cities;


  constructor(public unitesComponent: UnitsService,
              public dialog: Dialog,
              public http: Http,
              public citiesService: CitiesService,
              public universitiesService: UniversitiesService,
  ) {
    super(unitesComponent, dialog, EditUnitiesComponent, http);
    this.unitesComponent.register();
    this.citiesService.register() ;
    this.universitiesService.register();
    const basics = JSON.parse(localStorage.getItem('basics'));
    this.universities = Base.newCollection<Universities>();
    (new UniversitiesService()).convert(JSON.parse(basics['universities']), this.universities);
    this.cities = Base.newCollection<Cities>();
    (new CitiesService()).convert(JSON.parse(basics['cities']), this.cities);
  }

  ngOnInit() {
    this.columns = [
      {
        prop: 'name',
        name: 'نام',
        filter: this.filter_name_ref,
      },
      {
        prop: 'cities',
        name: 'شهر',
        filter: this.filter_cities_ref,
        isRelationship: true,
        mustIncluded: true,
      },
      {
        prop: 'universities',
        name: 'دانشگاه',
        filter: this.filter_universities_ref,
        isRelationship: true,
        mustIncluded: true,
      },
      {
        prop: 'id',
        name: 'شناسه',
        filter: this.filter_id_ref,
      },
    ];
    super.ngOnInit();
  }

}
