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

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

        if (!this.$state.q) {
            const today = Date.vnNew();
            today.setHours(0, 0, 0, 0);

            const nextWeek = Date.vnNew();
            nextWeek.setHours(23, 59, 59, 59);
            nextWeek.setDate(nextWeek.getDate() + 7);

            this.filterParams = {
                from: today,
                to: nextWeek,
                state: 'pending'
            };
        }
    }

    fetchParams($params) {
        if (!Object.entries($params).length)
            $params.scopeDays = 1;

        if (typeof $params.scopeDays === 'number') {
            const from = Date.vnNew();
            from.setHours(0, 0, 0, 0);

            const to = new Date(from.getTime());
            to.setDate(to.getDate() + $params.scopeDays);
            to.setHours(23, 59, 59, 999);

            Object.assign($params, {from, to});
        }

        return $params;
    }

    getState(isOk) {
        if (isOk === null)
            return 'Pending';
        else if (isOk)
            return 'Accepted';
        else
            return 'Denied';
    }

    confirmRequest(request) {
        if (request.itemFk && request.saleQuantity) {
            let params = {
                itemFk: request.itemFk,
                quantity: request.saleQuantity
            };

            let query = `TicketRequests/${request.id}/confirm`;
            this.$http.post(query, params).then(res => {
                request.itemDescription = res.data.concept;
                request.isOk = true;

                this.vnApp.showSuccess(this.$t('Data saved!'));
            });
        }
    }

    changeQuantity(request) {
        if (request.saleFk) {
            let params = {
                quantity: request.saleQuantity
            };

            let endpoint = `Sales/${request.saleFk}`;

            this.$http.patch(endpoint, params)
                .then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
                .then(() => this.confirmRequest(request));
        } else
            this.confirmRequest(request);
    }

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

    onDenyAccept(request) {
        let params = {
            observation: this.denyObservation
        };

        return this.$http.post(`TicketRequests/${request.id}/deny`, params)
            .then(res => {
                const newRequest = res.data;
                request.isOk = newRequest.isOk;
                request.attenderFk = newRequest.attenderFk;
                request.response = newRequest.response;

                this.vnApp.showSuccess(this.$t('Data saved!'));
            });
    }

    exprBuilder(param, value) {
        switch (param) {
        case 'ticketFk':
        case 'quantity':
        case 'price':
        case 'isOk':
            return {[`tr.${param}`]: value};
        case 'attenderName':
            return {[`ua.name`]: value};
        case 'shipped':
            return {'t.shipped': {
                between: this.dateRange(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];
    }
}

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