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

export default class Controller extends Section {
    constructor($element, $) {
        super($element, $);
        this.$checkAll = false;

        this.smartTableOptions = {
            activeButtons: {
                search: true,
            },
            columns: [{
                field: 'totalProblems',
                searchable: false,
            },
            {
                field: 'shipped',
                searchable: false
            },
            {
                field: 'futureShipped',
                searchable: false
            }, {
                field: 'totalWithVat',
                searchable: false
            },
            {
                field: 'state',
                searchable: false
            },
            {
                field: 'futureState',
                searchable: false
            },
            {
                field: 'ipt',
                autocomplete: {
                    url: 'ItemPackingTypes',
                    where: `{isActive: true}`,
                    showField: 'description',
                    valueField: 'code'
                }
            },
            {
                field: 'futureIpt',
                autocomplete: {
                    url: 'ItemPackingTypes',
                    where: `{isActive: true}`,
                    showField: 'description',
                    valueField: 'code'
                }
            },
            ]
        };
    }

    $postLink() {
        this.setDefaultFilter();
    }

    setDefaultFilter() {
        const today = Date.vnNew();

        this.$http.get(`UserConfigs/getUserConfig`)
            .then(res => {
                this.filterParams = {
                    originScopeDays: today,
                    futureScopeDays: today,
                    warehouseFk: res.data.warehouseFk
                };
                this.$.model.applyFilter(null, this.filterParams);
            });
    }

    compareDate(date) {
        let today = Date.vnNew();
        today.setHours(0, 0, 0, 0);
        let timeTicket = new Date(date);
        timeTicket.setHours(0, 0, 0, 0);

        let comparation = today - timeTicket;

        if (comparation == 0)
            return 'warning';
        if (comparation < 0)
            return 'success';
    }

    get checked() {
        const tickets = this.$.model.data || [];
        const checkedLines = [];
        for (let ticket of tickets) {
            if (ticket.checked)
                checkedLines.push(ticket);
        }

        return checkedLines;
    }

    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];
    }

    get confirmationMessage() {
        if (!this.$.model) return 0;

        return this.$t(`Move confirmation`, {
            checked: this.checked.length
        });
    }

    moveTicketsFuture() {
        let ticketsToMove = [];
        this.checked.forEach(ticket => {
            ticketsToMove.push({
                originId: ticket.id,
                destinationId: ticket.futureId,
                originShipped: ticket.shipped,
                destinationShipped: ticket.futureShipped,
                workerFk: ticket.workerFk
            });
        });
        let params = {tickets: ticketsToMove};
        return this.$http.post('Tickets/merge', params)
            .then(() => {
                this.$.model.refresh();
                this.vnApp.showSuccess(this.$t('Success'));
            });
    }
    totalPriceColor(totalWithVat) {
        return this.isLessThan50(totalWithVat) ? 'warning' : '';
    }

    totalPriceTitle(totalWithVat) {
        return this.isLessThan50(totalWithVat) ? 'Less than 50€' : '';
    }

    isLessThan50(totalWithVat) {
        return (parseInt(totalWithVat) > 0 && parseInt(totalWithVat) < 50);
    }

    exprBuilder(param, value) {
        switch (param) {
        case 'id':
            return {'id': value};
        case 'futureId':
            return {'futureId': value};
        case 'liters':
            return {'liters': value};
        case 'lines':
            return {'lines': value};
        case 'ipt':
            return {'ipt': {like: `%${value}%`}};
        case 'futureIpt':
            return {'futureIpt': {like: `%${value}%`}};
        case 'totalWithVat':
            return {'totalWithVat': value};
        }
    }
}

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

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