import ngModule from '../../module';
import Component from 'core/lib/component';
import './style.scss';

class Controller extends Component {
    $onInit() {
        this.data.registerChild(this);
    }

    get ticket() {
        return this._ticket;
    }

    set ticket(value) {
        this._ticket = value;

        if (!value) return;

        this.getTotalPrice();
        this.getTotalNewPrice();
        this.getTotalDifferenceOfPrice();
        this.loadDefaultTicketAction();
        this.ticketHaveNegatives();
    }

    loadDefaultTicketAction() {
        const isSalesAssistant = this.aclService.hasAny(['salesAssistant']);
        this.ticket.option = isSalesAssistant ? 'mana' : 'renewPrices';
    }

    onStepChange() {
        return true;
    }

    getTotalPrice() {
        let totalPrice = 0;
        this.ticket.sale.items.forEach(item => {
            let itemTotalPrice = item.quantity * item.price;
            totalPrice += itemTotalPrice;
        });
        this.totalPrice = totalPrice;
    }

    getTotalNewPrice() {
        let totalNewPrice = 0;
        this.ticket.sale.items.forEach(item => {
            if (item.component)
                totalNewPrice += item.quantity * item.component.newPrice;
        });
        this.totalNewPrice = totalNewPrice;
    }

    getTotalDifferenceOfPrice() {
        let totalPriceDifference = 0;
        this.ticket.sale.items.forEach(item => {
            if (item.component)
                totalPriceDifference += item.component.difference;
        });
        this.totalPriceDifference = totalPriceDifference;
    }

    ticketHaveNegatives() {
        let haveNegatives = false;
        let haveNotNegatives = false;
        this.ticket.withoutNegatives = false;
        const haveDifferences = this.ticket.sale.haveDifferences;

        this.ticket.sale.items.forEach(item => {
            if (item.quantity > item.movable)
                haveNegatives = true;
            else
                haveNotNegatives = true;
        });

        this.haveNegatives = (haveNegatives && haveNotNegatives && haveDifferences);
        if (this.haveNegatives)
            this.ticket.withoutNegatives = true;
    }

    onSubmit() {
        if (this.haveNegatives && !this.ticket.withoutNegatives)
            this.$.negativesConfirm.show();
        else this.applyChanges();
    }

    onConfirmAccept() {
        this.ticket.withWarningAccept = true;
        this.applyChanges();
    }

    applyChanges() {
        if (!this.ticket.option) {
            return this.vnApp.showError(
                this.$t('Choose an option')
            );
        }

        const query = `tickets/${this.ticket.id}/componentUpdate`;
        const params = {
            clientFk: this.ticket.clientFk,
            nickname: this.ticket.nickname,
            agencyModeFk: this.ticket.agencyModeFk,
            addressFk: this.ticket.addressFk,
            zoneFk: this.ticket.zoneFk,
            warehouseFk: this.ticket.warehouseFk,
            companyFk: this.ticket.companyFk,
            shipped: this.ticket.shipped,
            landed: this.ticket.landed,
            isDeleted: this.ticket.isDeleted,
            option: this.ticket.option,
            isWithoutNegatives: this.ticket.withoutNegatives,
            withWarningAccept: this.ticket.withWarningAccept,
            keepPrice: false
        };

        this.$http.post(query, params)
            .then(res => {
                this.ticketToMove = res.data.id;
                this.vnApp.showMessage(
                    this.$t(`The ticket has been unrouted`)
                );
            })
            .finally(() => {
                this.$state.go('ticket.card.summary', {id: this.ticketToMove});
            });
    }
}

ngModule.vnComponent('vnTicketBasicDataStepTwo', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        ticket: '<'
    },
    require: {
        card: '^vnTicketCard',
        data: '^vnTicketBasicData'
    }
});