import ngModule from '../module'; class Controller { constructor($stateParams, $scope, $http, $translate, vnApp) { this.params = $stateParams; this.$scope = $scope; this.$http = $http; this.$translate = $translate; this.vnApp = vnApp; this.ticketObservations = []; this.oldObservations = {}; this.removedObservations = []; } _setIconAdd() { if (this.ticketObservations.length) { this.ticketObservations.map(element => { element.showAddIcon = false; return true; }); this.ticketObservations[this.ticketObservations.length - 1].showAddIcon = true; } } _setDirtyForm() { if (this.$scope.form) { this.$scope.form.$setDirty(); } } _unsetDirtyForm() { if (this.$scope.form) { this.$scope.form.$setPristine(); } } addObservation() { this.ticketObservations.push({description: null, ticketFk: this.params.id, showAddIcon: true}); this._setIconAdd(); } removeObservation(index) { let item = this.ticketObservations[index]; if (item) { this.ticketObservations.splice(index, 1); this._setIconAdd(); if (item.id) { this.removedObservations.push(item.id); this._setDirtyForm(); } } } _equalObservations(oldObservation, newObservation) { return oldObservation.id === newObservation.id && oldObservation.observationTypeFk === newObservation.observationTypeFk && oldObservation.description === newObservation.description; } setOldObservations(response) { this._setIconAdd(); response.data.forEach(observation => { this.oldObservations[observation.id] = Object.assign({}, observation); }); } getObservations() { let filter = { where: {ticketFk: this.params.id}, include: ['observationType'] }; this.$http.get(`/ticket/api/TicketObservations?filter=${JSON.stringify(filter)}`).then(response => { this.ticketObservations = response.data; this.setOldObservations(response); }); } submit() { let typesDefined = []; let repeatedType = false; let canSubmit; let observationsObj = { delete: this.removedObservations, create: [], update: [] }; this.ticketObservations.forEach(observation => { let isNewObservation = !observation.id; delete observation.showAddIcon; if (typesDefined.indexOf(observation.observationTypeFk) !== -1) { repeatedType = true; return; } typesDefined.push(observation.observationTypeFk); if (isNewObservation && observation.description && observation.observationTypeFk) { observationsObj.create.push(observation); } if (!isNewObservation && !this._equalObservations(this.oldObservations[observation.id], observation)) { observationsObj.update.push(observation); } }); if (this.$scope.form.$invalid) { return this.vnApp.showMessage(this.$translate.instant('Some fields are invalid')); } if (repeatedType) { return this.vnApp.showMessage(this.$translate.instant('The observation type must be unique')); } canSubmit = observationsObj.update.length > 0 || observationsObj.create.length > 0 || observationsObj.delete.length > 0; if (canSubmit) { return this.$http.post(`/ticket/api/TicketObservations/crudTicketObservation`, observationsObj).then(() => { this.getObservations(); this._unsetDirtyForm(); }); } this.vnApp.showMessage(this.$translate.instant('No changes to save')); } $onInit() { this.getObservations(); } } Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; ngModule.component('vnTicketObservation', { template: require('./ticket-observation.html'), controller: Controller, bindings: { ticket: '<' } });