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

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

        this.smartTableOptions = {
            activeButtons: {
                search: true,
                shownColumns: true,
            },
            columns: [
                {
                    field: 'socialName',
                    autocomplete: {
                        url: 'Clients',
                        showField: 'socialName',
                        valueField: 'socialName',
                    }
                },
                {
                    field: 'created',
                    datepicker: true
                },
                {
                    field: 'countryFk',
                    autocomplete: {
                        url: 'Countries',
                        showField: 'country',
                    }
                },
                {
                    field: 'provinceFk',
                    autocomplete: {
                        url: 'Provinces'
                    }
                },
                {
                    field: 'salesPersonFk',
                    autocomplete: {
                        url: 'Workers/activeWithInheritedRole',
                        where: `{role: 'salesPerson'}`,
                        searchFunction: '{firstName: $search}',
                        showField: 'nickname',
                        valueField: 'id',
                    }
                },
                {
                    field: 'businessTypeFk',
                    autocomplete: {
                        url: 'BusinessTypes',
                        valueField: 'code',
                        showField: 'description',
                    }
                },
                {
                    field: 'payMethodFk',
                    autocomplete: {
                        url: 'PayMethods',
                    }
                },
                {
                    field: 'sageTaxTypeFk',
                    autocomplete: {
                        url: 'SageTaxTypes',
                        showField: 'vat',
                    }
                },
                {
                    field: 'sageTransactionTypeFk',
                    autocomplete: {
                        url: 'SageTransactionTypes',
                        showField: 'transaction',
                    }
                },
                {
                    field: 'isActive',
                    checkbox: true
                },
                {
                    field: 'isVies',
                    checkbox: true
                },
                {
                    field: 'isTaxDataChecked',
                    checkbox: true
                },
                {
                    field: 'isEqualizated',
                    checkbox: true
                },
                {
                    field: 'isFreezed',
                    checkbox: true
                },
                {
                    field: 'hasToInvoice',
                    checkbox: true
                },
                {
                    field: 'hasToInvoiceByAddress',
                    checkbox: true
                },
                {
                    field: 'isToBeMailed',
                    checkbox: true
                },
                {
                    field: 'hasSepaVnl',
                    checkbox: true
                },
                {
                    field: 'hasLcr',
                    checkbox: true
                },
                {
                    field: 'hasCoreVnl',
                    checkbox: true
                }
            ]
        };
    }

    exprBuilder(param, value) {
        switch (param) {
        case 'created':
            return {'c.created': {
                between: this.dateRange(value)}
            };
        case 'id':
        case 'name':
        case 'socialName':
        case 'fi':
        case 'credit':
        case 'creditInsurance':
        case 'phone':
        case 'mobile':
        case 'street':
        case 'city':
        case 'postcode':
        case 'email':
        case 'isActive':
        case 'isVies':
        case 'isTaxDataChecked':
        case 'isEqualizated':
        case 'isFreezed':
        case 'hasToInvoice':
        case 'hasToInvoiceByAddress':
        case 'isToBeMailed':
        case 'hasSepaVnl':
        case 'hasLcr':
        case 'hasCoreVnl':
        case 'countryFk':
        case 'provinceFk':
        case 'salesPersonFk':
        case 'businessTypeFk':
        case 'payMethodFk':
        case 'sageTaxTypeFk':
        case 'sageTransactionTypeFk':
            return {[`c.${param}`]: value};
        }
    }

    dateRange(value) {
        const minHour = new Date(value);
        minHour.setHours(0, 0, 0, 0);
        const maxHour = new Date(value);
        maxHour.setHours(23, 59, 59, 59);

        return [minHour, maxHour];
    }

    preview(client) {
        this.clientSelected = client;
        this.$.preview.show();
    }
}

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