import { Component, OnDestroy } from '@angular/core';
import { BixiACService } from '@bixi/ac';
import { Subscription } from 'rxjs';

@Component({
  selector: 'idea-ac',
  templateUrl: './ac.component.html'
})
export class IdeaAcComponent implements OnDestroy {
  isRole = false;
  roles: string[] = [];
  permissions: string[] = [];
  sub = new Subscription();

  selected: string[] = [];

  items = [{
    text: '应用查看',
    value: 'app.view'
  }, {
    text: '应用删除',
    value: 'app.delete'
  }, {
    text: '应用修改',
    value: 'app.update'
  }, {
    text: '应用增加',
    value: 'app.create'
  }];

  constructor(
    private acServcie: BixiACService
  ) {
    this.sub.add(acServcie.roles$
      .subscribe((roles) => {
        this.roles = roles;
      }));
    this.sub.add(acServcie.permissions$
      .subscribe((permissions) => {
        this.permissions = permissions;
      }));
  }

  // tslint:disable-next-line: no-any
  onChange(e: any) {
    console.warn(e);
    if (this.isRole) {
      this.acServcie.setRoles(this.selected);
    } else {
      this.acServcie.setPermissions(this.selected);
    }
  }


  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}
