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

class Controller extends Section {
    constructor($element, $, vnReport, vnEmail) {
        super($element, $);
        this.vnReport = vnReport;
        this.vnEmail = vnEmail;

        this.filter = {
            where: {
                isPackaging: false
            }
        };

        this.setDefaultFilter();
    }

    setDefaultFilter() {
        const minDate = Date.vnNew();
        minDate.setHours(0, 0, 0, 0);
        minDate.setMonth(minDate.getMonth() - 2);

        const maxDate = Date.vnNew();
        maxDate.setHours(23, 59, 59, 59);

        this.filterParams = {
            from: minDate,
            to: maxDate
        };
    }

    get reportParams() {
        const userParams = this.$.model.userParams;
        return Object.assign({
            recipient: this.client.email,
            recipientId: this.client.id
        }, userParams);
    }

    showTicketDescriptor(event, sale) {
        if (!sale.isTicket) return;

        this.$.ticketDescriptor.show(event.target, sale.origin);
    }

    showReport() {
        const path = `Clients/${this.client.id}/campaign-metrics-pdf`;
        this.vnReport.show(path, this.reportParams);
    }

    sendEmail() {
        const path = `Clients/${this.client.id}/campaign-metrics-email`;
        this.vnEmail.send(path, this.reportParams);
    }

    changeGrouped(value) {
        const model = this.$.model;

        model.addFilter({}, {grouped: value});
    }
}

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

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