import ngModule from '../module';
import Descriptor from 'salix/components/descriptor';

class Controller extends Descriptor {
    constructor($element, $, $rootScope) {
        super($element, $);
        this.$rootScope = $rootScope;
    }

    get item() {
        return this.entity;
    }

    set item(value) {
        this.entity = value;
    }

    get entity() {
        return super.entity;
    }

    set entity(value) {
        super.entity = value;
        if (this.warehouseFk) this.updateStock();
    }

    get warehouseFk() {
        return this._warehouseFk;
    }

    set warehouseFk(value) {
        this._warehouseFk = value;
        if (value) {
            this.updateStock();
            this.getWarehouseName(value);
        }
    }

    loadData() {
        return this.getData(`Items/${this.id}/getCard`)
            .then(res => this.entity = res.data);
    }

    updateStock() {
        this.available = null;
        this.visible = null;
        if (!this.item) return;

        const params = {
            warehouseFk: this.warehouseFk,
            dated: this.dated
        };

        return this.$http.get(`Items/${this.id}/getVisibleAvailable`, {params})
            .then(res => {
                this.available = res.data.available;
                this.visible = res.data.visible;
            });
    }

    saveRegularize() {
        const params = {
            itemFk: this.id,
            quantity: parseInt(this.quantity),
            warehouseFk: this.warehouseFk
        };

        return this.$http.post(`Items/regularize`, params)
            .then(() => {
                this.vnApp.showSuccess(this.$t('Data saved!'));
                this.updateStock();
            });
    }

    clearRegularizeDialog() {
        this.warehouseFk = null;
        this.quantity = null;
    }

    onCloneAccept() {
        this.$http.post(`Items/${this.item.id}/clone`)
            .then(res => this.$state.go('item.card.tags', {id: res.data.id}));
    }

    onUploadResponse() {
        const timestamp = Date.vnNew().getTime();
        const src = this.$rootScope.imagePath('catalog', '200x200', this.item.id);
        const zoomSrc = this.$rootScope.imagePath('catalog', '1600x900', this.item.id);
        const newSrc = `${src}&t=${timestamp}`;
        const newZoomSrc = `${zoomSrc}&t=${timestamp}`;

        this.$.photo.setAttribute('src', newSrc);
        this.$.photo.setAttribute('zoom-image', newZoomSrc);

        if (this.item.isPhotoRequested)
            this.$http.patch(`Items/${this.item.id}`, {isPhotoRequested: false});
    }

    getWarehouseName(warehouseFk) {
        this.showIcon = false;

        const filter = {
            where: {id: warehouseFk}
        };
        this.$http.get('Warehouses/findOne', {filter})
            .then(res => {
                this.warehouseText = this.$t('WarehouseFk', {
                    warehouseName: res.data.name
                });

                this.showIcon = true;
            });
    }
}

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

ngModule.vnComponent('vnItemDescriptor', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        item: '<',
        dated: '<',
        cardReload: '&',
        warehouseFk: '<'
    }
});