import { Component, Inject, OnChanges, Input, Output, EventEmitter } from "@angular/core";
import { ComponentService } from "./Component.service";
import { Unit, UnitType } from "./Unit";
import { Lock } from "./Lock";
@Component({
selector: "SelectUnit",
providers: [ComponentService],
template: `
`
})
export class SelectUnit implements OnChanges {
@Input() chooseCaption: string = '';
@Input() types: UnitType[] = [UnitType.Pizzeria];
@Input() departmentId: number;
@Input() selectedUnitId: number = 0;
@Input() exclude: number[] = [];
@Output() selectedUnitIdChange: EventEmitter = new EventEmitter();
@Input() disabled: boolean = false;
private units: Unit[] = [];
private lock: Lock = new Lock();
constructor( @Inject(ComponentService) private componentService: ComponentService) { }
ngOnChanges(changes) {
if (changes.departmentId) {
this.lock.lock();
this.componentService.getUnits(this.departmentId, this.types)
.subscribe(units => {
this.units = units;
this.lock.unlock();
});
}
}
onChange(id: number) {
this.selectedUnitId = id;
this.selectedUnitIdChange.emit(this.selectedUnitId);
}
displayUnits() {
return this.units.filter(x => !this.exclude.find(idToExclude => idToExclude === x.Id));
}
}