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

export default class Controller extends Section {
    $onInit() {
        this.getTaxes();
    }

    getTaxes() {
        let filter = {
            fields: ['id', 'countryFk', 'taxClassFk'],
            include: [{
                relation: 'country',
                scope: {fields: ['country']}
            }]
        };

        let url = `Items/${this.$params.id}/taxes`;
        this.$http.get(url, {params: {filter}}).then(json => {
            this.taxes = json.data;
        });
    }

    submit() {
        let data = [];
        for (let tax of this.taxes)
            data.push({id: tax.id, taxClassFk: tax.taxClassFk});

        this.$.watcher.check();
        let url = `Items/updateTaxes`;
        this.$http.post(url, data).then(() => {
            this.$.watcher.notifySaved();
            this.$.watcher.updateOriginalData();
        });
    }
}

ngModule.vnComponent('vnItemTax', {
    template: require('./index.html'),
    controller: Controller
});