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

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

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

    get entity() {
        return super.entity;
    }

    set entity(value) {
        super.entity = value;
    }

    loadData() {
        const filter = {
            include: [
                {
                    relation: 'warehouse',
                    scope: {
                        fields: ['name']
                    }
                }, {
                    relation: 'agencyMode',
                    scope: {
                        fields: ['name']
                    }
                }, {
                    relation: 'client',
                    scope: {
                        fields: [
                            'salesPersonFk',
                            'name',
                            'isActive',
                            'isFreezed',
                            'isTaxDataChecked'
                        ],
                        include: {
                            relation: 'salesPersonUser',
                            scope: {
                                fields: ['id', 'name']
                            }
                        }
                    }
                }, {
                    relation: 'ticketState',
                    scope: {
                        fields: ['stateFk'],
                        include: {
                            relation: 'state',
                            fields: ['id', 'name'],
                        }
                    }
                }
            ]
        };

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

    cardReload() {
        // Prevents error when not defined
    }

    get filter() {
        return this.ticket ?
            JSON.stringify({clientFk: this.ticket.clientFk}) : null;
    }
}

ngModule.vnComponent('vnTicketDescriptor', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        ticket: '<',
        cardReload: '&'
    }
});