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

class Controller extends Descriptor {
    get invoiceIn() {
        return this.entity;
    }

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

    get entryFilter() {
        if (this.invoiceIn)
            return JSON.stringify({invoiceInFk: this.invoiceIn.id});

        return null;
    }

    get invoiceInFilter() {
        if (this.invoiceIn)
            return JSON.stringify({supplierFk: this.invoiceIn.supplierFk});

        return null;
    }

    deleteInvoiceIn() {
        return this.$http.delete(`InvoiceIns/${this.id}`)
            .then(() => this.$state.go('invoiceIn.index'))
            .then(() => this.vnApp.showSuccess(this.$t('InvoiceIn deleted')));
    }

    cloneInvoiceIn() {
        return this.$http.post(`InvoiceIns/${this.id}/clone`)
            .then(res => this.$state.go('invoiceIn.card.summary', {id: res.data.id}))
            .then(() => this.vnApp.showSuccess(this.$t('InvoiceIn cloned')));
    }

    loadData() {
        const filter = {
            include: [
                {
                    relation: 'company',
                    scope: {
                        fields: ['id', 'code']
                    }
                }
            ]
        };

        return this.getData(`InvoiceIns/${this.id}`, {filter})
            .then(res => this.entity = res.data);
    }
}

ngModule.vnComponent('vnInvoiceInDescriptor', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        invoiceIn: '<'
    }
});