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

class Controller extends Section {
    constructor($element, $) {
        super($element, $);

        this.smartTableOptions = {
            activeButtons: {
                search: true
            },
            columns: [
                {
                    field: 'agencyModeFk',
                    autocomplete: {
                        url: 'AgencyModes',
                        showField: 'name',
                        valueField: 'id'
                    }
                },
                {
                    field: 'agencyFk',
                    autocomplete: {
                        url: 'Agencies',
                        showField: 'name',
                        valueField: 'id'
                    }
                },
                {
                    field: 'supplierFk',
                    autocomplete: {
                        url: 'Suppliers',
                        showField: 'name',
                        valueField: 'name',
                    }
                }
            ]
        };
    }

    exprBuilder(param, value) {
        switch (param) {
        case 'agencyModeFk':
            return {'a.agencyModeFk': value};
        case 'supplierFk':
            return {'a.supplierName': value};
        case 'routeFk':
            return {'a.routeFk': value};
        case 'created':
        case 'agencyFk':
        case 'packages':
        case 'm3':
        case 'kmTotal':
        case 'price':
        case 'invoiceInFk':
            return {[`a.${param}`]: value};
        }
    }

    get checked() {
        const agencyTerms = this.$.model.data || [];
        const checkedAgencyTerms = [];
        for (let agencyTerm of agencyTerms) {
            if (agencyTerm.checked)
                checkedAgencyTerms.push(agencyTerm);
        }

        return checkedAgencyTerms;
    }

    get totalChecked() {
        return this.checked.length;
    }

    get totalPrice() {
        let totalPrice = 0;

        if (this.checked.length > 0) {
            for (let agencyTerm of this.checked)
                totalPrice += agencyTerm.price;

            return totalPrice;
        }

        return totalPrice;
    }

    preview(route) {
        this.routeSelected = route;
        this.$.summary.show();
    }

    createInvoiceIn() {
        const rowsToCreateInvoiceIn = [];
        const supplierFk = this.checked[0].supplierFk;

        for (let agencyTerm of this.checked) {
            let hasSameSupplier = supplierFk == agencyTerm.supplierFk;
            if (hasSameSupplier) {
                rowsToCreateInvoiceIn.push({
                    routeFk: agencyTerm.routeFk,
                    supplierFk: agencyTerm.supplierFk,
                    created: agencyTerm.created,
                    totalPrice: this.totalPrice});
            } else {
                this.vnApp.showError(this.$t('Two autonomous cannot be counted at the same time'));
                return false;
            }
        }
        const params = JSON.stringify({
            supplierName: this.checked[0].supplierName,
            rows: rowsToCreateInvoiceIn
        });
        this.$state.go('route.agencyTerm.createInvoiceIn', {q: params});
    }
}

ngModule.vnComponent('vnAgencyTermIndex', {
    template: require('./index.html'),
    controller: Controller
});