import ngModule from '../module';
import Summary from 'salix/components/summary';
import './style.scss';

class Controller extends Summary {
    set route(value) {
        this._route = value;
        if (value && value.id)
            this.getSummary();
    }

    sumPackages() {
        this.packagesTotal = 0;
        this.summary.tickets.forEach(ticket => {
            this.packagesTotal += ticket.packages;
        });
    }

    get isDelivery() {
        return this.aclService.hasAny(['delivery']);
    }

    get route() {
        return this._route;
    }

    getSummary() {
        this.$http.get(`Routes/${this.route.id}/summary`).then(response => {
            this.summary = response.data;
            if (response.data && response.data.tickets)
                this.sumPackages();
        });
    }
}

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