import {Component, OnChanges, OnInit, TemplateRef, ViewChild} from '@angular/core';
import {User, UsersService} from './users.service';
import {ICollection} from 'ngx-jsonapi';
import {RolesService} from '../roles/roles.service';
import {ListComponent} from '../../../classes/base_components/list/list.component';
import {Dialog} from '../../../classes/dialog';
import {EditUsersComponent} from './edit-users/edit-users.component';
import {TypesService} from '../../basic/types/types.service';
import {Http} from '@angular/http';


@Component({
  selector: 'app-user',
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.scss']
})
export class UsersComponent extends ListComponent implements OnInit {
  @ViewChild('filter_id') public filter_id_ref: TemplateRef<any>;
  @ViewChild('filter_name') public filter_name_ref: TemplateRef<any>;
  @ViewChild('filter_mobile') public filter_mobile_ref: TemplateRef<any>;
  @ViewChild('filter_email') public filter_email_ref: TemplateRef<any>;
  @ViewChild('filter_type') public filter_type_ref: TemplateRef<any>;
  @ViewChild('filter_role') public filter_role_ref: TemplateRef<any>;
  types;
  roles;
  users: ICollection<User>;
  data: any;


  constructor(protected listService: UsersService,
              protected typeService: TypesService,
              protected roleService: RolesService,
              protected dialog: Dialog,
              protected http: Http,
  ) {
    super(listService, dialog, EditUsersComponent, http);
    const basics = JSON.parse(localStorage.getItem('basics'));
    this.types = basics['types'];
    this.roles = basics['roles'];
    typeService.register();
    roleService.register();
  }

  ngOnInit() {
    this.columns = [
      {
        prop: 'name',
        name: 'نام',
        filter: this.filter_name_ref,
      },
      {
        prop: 'mobile',
        name: 'موبایل',
        filter: this.filter_mobile_ref,
      },
      {
        prop: 'email',
        name: 'ایمیل',
        filter: this.filter_email_ref,
      },
      {
        prop: 'type',
        name: 'نوع',
        isRelationship: true,
        mustIncluded: true,
        filter: this.filter_type_ref,
      },
      {
        prop: 'role',
        name: 'نقش',
        isRelationship: true,
        mustIncluded: true,
        filter: this.filter_role_ref,

      },
      {
        prop: 'id',
        name: 'شناسه',
        filter: this.filter_id_ref,
      },
    ];

    super.ngOnInit();

  }


}
