import ngModule from '../module';
import Component from 'core/lib/component';
import './style.scss';

class Controller extends Component {
    constructor($element, $scope, $http, $timeout, $q) {
        super($element, $scope);
        this.$timeout = $timeout;
        this.$http = $http;
        this.$q = $q;
        this.worker = null;
    }

    set workerFk(id) {
        if (id == this._workerFk) return;

        this._workerFk = id;
        this.worker = null;
        this.loadData();
    }

    get workerFk() {
        return this._workerFk;
    }

    set quicklinks(value = {}) {
        this._quicklinks = Object.assign(value, this._quicklinks);
    }

    get quicklinks() {
        return this._quicklinks;
    }

    show() {
        this.$.popover.parent = this.parent;
        this.$.popover.show();
    }

    loadData() {
        let query = `api/Workers/${this._workerFk}`;
        let filter = {
            include: [
                {
                    relation: 'user',
                    scope: {fields: ['name', 'email']}
                }, {
                    relation: 'client',
                    scope: {fields: ['fi']}
                }, {
                    relation: 'sip',
                    scope: {fields: ['extension']}
                }
            ]
        };

        this.$http.get(query, {params: {filter}}).then(res => {
            this.worker = res.data;
            this.$.$applyAsync(() => {
                this.$.popover.relocate();
            });
        });
    }
}
Controller.$inject = ['$element', '$scope', '$http', '$timeout', '$q'];

ngModule.component('vnWorkerDescriptorPopover', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        workerFk: '<',
        quicklinks: '<'
    }
});