import ngModule from '../module';
import Section from 'salix/components/section';

class Controller extends Section {
    get client() {
        return this._client;
    }

    set client(value) {
        this._client = value;
        if (value)
            this.setLink(value);
    }

    setLink(value) {
        this.$.$applyAsync(()=> {
            this.$.model.link = {userFk: value.id};
            this.$.model.refresh();
        });
    }

    onSubmit() {
        this.$.watcher.check();
        return this.$.model.save().then(() => {
            this.$.watcher.updateOriginalData();
            this.$.watcher.notifySaved();
            this.card.reload();
        });
    }

    add() {
        this.$.model.insert();
    }
}


ngModule.component('vnClientPhones', {
    template: require('./index.html'),
    controller: Controller,
    require: {card: '^vnClientCard'},
    bindings: {
        client: '<'
    }
});