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

class Controller extends Descriptor {
    get route() {
        return this.entity;
    }

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

    showRouteReport() {
        this.vnReport.show(`Routes/${this.id}/driver-route-pdf`, {
            id: this.id
        });
    }

    sendRouteReport() {
        const workerUser = this.route.worker.user;
        this.vnEmail.send(`Routes/${this.id}/driver-route-email`, {
            recipient: workerUser.emailUser.email,
            id: this.id
        });
        const params = {isOk: true};
        return this.$http.patch(`Routes/${this.id}`, params);
    }

    updateVolume() {
        this.$http.post(`Routes/${this.id}/updateVolume`)
            .then(() => {
                if (this.cardReload) this.cardReload();
                this.vnApp.showSuccess(this.$t('Volume updated'));
            });
    }

    deleteCurrentRoute() {
        this.$http.delete(`Routes/${this.id}`)
            .then(() => {
                this.vnApp.showSuccess(this.$t('Route deleted'));
                this.$state.go('route.index');
            });
    }

    loadData() {
        const filter = {
            fields: [
                'id',
                'workerFk',
                'agencyModeFk',
                'dated',
                'm3',
                'warehouseFk',
                'description',
                'vehicleFk',
                'kmStart',
                'kmEnd',
                'started',
                'finished',
                'cost',
                'zoneFk'
            ],
            include: [
                {
                    relation: 'agencyMode',
                    scope: {
                        fields: ['id', 'name']
                    }
                }, {
                    relation: 'vehicle',
                    scope: {
                        fields: ['id', 'm3']
                    }
                }, {
                    relation: 'zone',
                    scope: {
                        fields: ['id', 'name']
                    }
                }, {
                    relation: 'worker',
                    scope: {
                        fields: ['id'],
                        include: {
                            relation: 'user',
                            scope: {
                                fields: ['id'],
                                include: {
                                    relation: 'emailUser',
                                    scope: {
                                        fields: ['email']
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        };

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

ngModule.vnComponent('vnRouteDescriptor', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        route: '<',
        cardReload: '&?'
    }
});