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

class Controller extends Section {
    showItemDescriptor(event, sale) {
        this.quicklinks = {
            btnThree: {
                icon: 'icon-transaction',
                state: `item.card.diary({
                    id: ${sale.item.id},
                    warehouseFk: ${this.ticket.warehouseFk},
                    lineFk: ${sale.id}
                })`,
                tooltip: 'Item diary'
            }
        };
        this.$.itemDescriptor.show(event.target, sale.item.id);
    }

    showSaleTracking(sale) {
        this.saleId = sale.saleFk;
        this.$.saleTracking.show();
    }

    showItemShelvingSale(sale) {
        this.saleId = sale.saleFk;
        this.$.itemShelvingSale.show();
    }

    clickSaleGroupDetail(index) {
        const sale = this.sales[index];
        if (!sale.saleGroupDetailFk) return;

        return this.$http.delete(`SaleGroupDetails/${sale.saleGroupDetailFk}`)
            .then(() => {
                sale.hasSaleGroupDetail = false;
                this.vnApp.showSuccess(this.$t('Data saved!'));
            });
    }

    clickPreviousSelected(index) {
        const sale = this.sales[index];
        if (!sale.isPreviousSelected) {
            this.saleTrackingNew(sale, 'PREVIOUS_PREPARATION', false);
            sale.isPreviousSelected = true;
        } else {
            this.saleTrackingDel(sale, 'PREVIOUS_PREPARATION');
            sale.isPreviousSelected = false;
            sale.isPrevious = false;
        }
    }

    clickPrevious(index) {
        const sale = this.sales[index];
        if (!sale.isPrevious) {
            this.saleTrackingNew(sale, 'PREVIOUS_PREPARATION', true);
            sale.isPrevious = true;
            sale.isPreviousSelected = true;
        } else {
            this.saleTrackingNew(sale, 'PREVIOUS_PREPARATION', false);
            sale.isPrevious = false;
        }
    }

    clickPrepared(index) {
        const sale = this.sales[index];
        if (!sale.isPrepared) {
            this.saleTrackingNew(sale, 'PREPARED', true);
            sale.isPrepared = true;
        } else {
            this.saleTrackingDel(sale, 'PREPARED');
            sale.isPrepared = false;
        }
    }

    clickControled(index) {
        const sale = this.sales[index];
        if (!sale.isControled) {
            this.saleTrackingNew(sale, 'CHECKED', true);
            sale.isControled = true;
        } else {
            this.saleTrackingDel(sale, 'CHECKED');
            sale.isControled = false;
        }
    }

    saleTrackingNew(sale, stateCode, isChecked) {
        const params = {
            saleFk: sale.saleFk,
            isChecked: isChecked,
            quantity: sale.quantity,
            stateCode: stateCode
        };
        this.$http.post(`SaleTrackings/new`, params).then(() => {
            this.vnApp.showSuccess(this.$t('Data saved!'));
        });
    }

    saleTrackingDel(sale, stateCode) {
        const params = {
            saleFk: sale.saleFk,
            stateCode: stateCode
        };
        this.$http.post(`SaleTrackings/delete`, params).then(() => {
            this.vnApp.showSuccess(this.$t('Data saved!'));
        });
    }

    updateQuantity(itemShelvingSale) {
        const params = {
            quantity: itemShelvingSale.quantity
        };
        this.$http.patch(`ItemShelvingSales/${itemShelvingSale.id}`, params)
            .then(() => {
                this.vnApp.showSuccess(this.$t('Data saved!'));
            });
    }

    async updateShelving(itemShelvingSale) {
        const params = {
            shelvingFk: itemShelvingSale.shelvingFk
        };
        const res = await this.$http.patch(`ItemShelvings/${itemShelvingSale.itemShelvingFk}`, params);

        const filter = {
            fields: ['parkingFk'],
            where: {
                code: res.data.shelvingFk
            }
        };
        this.$http.get(`Shelvings/findOne`, {filter})
            .then(res => {
                itemShelvingSale.parkingFk = res.data.parkingFk;
                this.vnApp.showSuccess(this.$t('Data saved!'));
            });
    }

    async updateParking(itemShelvingSale) {
        const filter = {
            fields: ['id'],
            where: {
                code: itemShelvingSale.shelvingFk
            }
        };
        const res = await this.$http.get(`Shelvings/findOne`, {filter});

        const params = {
            parkingFk: itemShelvingSale.parkingFk
        };
        this.$http.patch(`Shelvings/${res.data.id}`, params)
            .then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
    }
}

ngModule.vnComponent('vnTicketSaleTracking', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        ticket: '<',
        model: '<?'
    }
});