import ngModule from '../module';
import Descriptor from 'salix/components/descriptor';

class Controller extends Descriptor {
    constructor($element, $, $rootScope) {
        super($element, $);
        this.$rootScope = $rootScope;
    }

    get worker() {
        return this.entity;
    }

    set worker(value) {
        this.entity = value;
    }

    loadData() {
        const filter = {
            include: [
                {
                    relation: 'user',
                    scope: {
                        fields: ['name'],
                        include: {
                            relation: 'emailUser',
                            scope: {
                                fields: ['email']
                            }
                        }
                    }
                }, {
                    relation: 'client',
                    scope: {
                        fields: ['fi']
                    }
                }, {
                    relation: 'sip',
                    scope: {
                        fields: ['extension']
                    }
                }, {
                    relation: 'department',
                    scope: {
                        include: {
                            relation: 'department'
                        }
                    }
                }
            ]
        };

        return this.getData(`Workers/${this.id}`, {filter})
            .then(res => this.entity = res.data);
    }

    onUploadResponse() {
        const timestamp = new Date().getTime();
        const src = this.$rootScope.imagePath('user', '520x520', this.worker.id);
        const zoomSrc = this.$rootScope.imagePath('user', '1600x1600', this.worker.id);
        const newSrc = `${src}&t=${timestamp}`;
        const newZoomSrc = `${zoomSrc}&t=${timestamp}`;

        this.$.photo.setAttribute('src', newSrc);
        this.$.photo.setAttribute('zoom-image', newZoomSrc);
    }
}

Controller.$inject = ['$element', '$scope', '$rootScope'];

ngModule.vnComponent('vnWorkerDescriptor', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        worker: '<'
    }
});