import ngModule from '../module'; export default class Controller { constructor($stateParams, $scope, $http, $translate, vnApp) { this.params = $stateParams; this.$scope = $scope; this.$http = $http; this.$translate = $translate; this.vnApp = vnApp; this.warehouses = []; this.niches = []; this.removedNiches = []; this.oldNiches = {}; } $onInit() { this.getNiches(); this.getWarehouses(); } _setIconAdd() { if (this.niches.length) { this.niches.map(element => { element.showAddIcon = false; return true; }); this.niches[this.niches.length - 1].showAddIcon = true; } else { this.addNiche(); } } _setDirtyForm() { if (this.$scope.form) { this.$scope.form.$setDirty(); } } _unsetDirtyForm() { if (this.$scope.form) { this.$scope.form.$setPristine(); } } addNiche() { this.niches.push({code: null, itemFk: this.params.id, showAddIcon: true}); this._setIconAdd(); } removeNiche(index) { let item = this.niches[index]; if (item) { this.niches.splice(index, 1); this._setIconAdd(); if (item.id) { this.removedNiches.push(item.id); this._setDirtyForm(); } } } _equalNiches(oldNiche, newNiche) { return oldNiche.id === newNiche.id && oldNiche.code === newNiche.code && oldNiche.warehouseFk === newNiche.warehouseFk; } setOldNiches(response) { this._setIconAdd(); response.data.forEach(niche => { this.oldNiches[niche.id] = Object.assign({}, niche); }); } getNiches() { let filter = { where: {itemFk: this.params.id}, include: {relation: 'warehouse'} }; this.$http.get(`/item/api/ItemNiches?filter=${JSON.stringify(filter)}`).then(response => { this.niches = response.data; this.setOldNiches(response); }); } getWarehouse(id, warehouses) { return warehouses.find(warehouse => warehouse.id === id); } getWarehouses() { this.$http.get(`/item/api/Warehouses`).then(response => { this.warehouses = response.data; }); } submit() { let warehousesDefined = []; let repeatedWarehouse = false; let canSubmit; let nichesObj = { delete: this.removedNiches, create: [], update: [] }; this.niches.forEach(niche => { let isNewNiche = !niche.id; if (warehousesDefined.indexOf(niche.warehouseFk) !== -1) { repeatedWarehouse = true; return; } warehousesDefined.push(niche.warehouseFk); if (isNewNiche) { nichesObj.create.push(niche); } if (!isNewNiche && !this._equalNiches(this.oldNiches[niche.id], niche)) { nichesObj.update.push(niche); } }); if (this.$scope.form.$invalid) { return this.vnApp.showError(this.$translate.instant('Some fields are invalid')); } if (repeatedWarehouse) { return this.vnApp.showError(this.$translate.instant('The niche must be unique')); } canSubmit = nichesObj.update.length > 0 || nichesObj.create.length > 0 || nichesObj.delete.length > 0; if (canSubmit) { return this.$http.post(`/item/api/ItemNiches/crudItemNiches`, nichesObj).then(() => { this.getNiches(); this._unsetDirtyForm(); this.$scope.watcher.notifySaved(); }); } this.vnApp.showError(this.$translate.instant('No changes to save')); } } Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; ngModule.component('vnItemNiche', { template: require('./index.html'), controller: Controller });