import ngModule from '../module'; import './style.scss'; class Controller { constructor($element, $scope, $http, $filter, $translate, $stateParams, vnApp) { this.$element = $element; this.$ = $scope; this.$http = $http; this.$filter = $filter; this.$translate = $translate; this.$stateParams = $stateParams; this.vnApp = vnApp; this.stMonthDate = new Date(); this.ndMonthDate = new Date(); this.ndMonthDate.setMonth(this.ndMonthDate.getMonth() + 1); this.selectedDay = {}; } $postLink() { this.stMonth = this.$.stMonth; this.ndMonth = this.$.ndMonth; } get data() { return this._data; } set data(value) { this._data = value; if (!value) return; const events = []; value.forEach(event => { events.push({ name: `P: ${this.$filter('currency')(event.price)}`, description: 'Price', dated: event.delivered, style: {backgroundColor: '#a3d131'}, data: {price: event.price} }); events.push({ name: `B: ${this.$filter('currency')(event.bonus)}`, description: 'Bonus', dated: event.delivered, data: {bonus: event.bonus} }); }); this.events = events; } onSelection(values) { if (values.length > 1) return false; this.options = [ {label: 'Only this day', value: 'Only this day'}, {label: 'From this day', value: 'From this day'}, {label: 'All days', value: 'All days'} ]; const selection = values[0]; const events = selection.events; const hasEvents = events.length > 0; if (!hasEvents) return this.vnApp.showMessage(this.$translate.instant(`There's no delivery for this day`)); this.selectedDay = { delivered: selection.dated, option: 'Only this day' }; events.forEach(event => { this.selectedDay = Object.assign(this.selectedDay, event.data); }); this.$.priceDialog.show(); } onResponse(response) { if (response == 'ACCEPT') { try { const data = { delivered: this.selectedDay.delivered, price: this.selectedDay.price, bonus: this.selectedDay.bonus, option: this.selectedDay.option }; this.$.watcher.check(); const path = `/api/Zones/${this.zone.id}/editPrices`; this.$http.post(path, data).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.$.model.refresh(); this.card.reload(); }); } catch (e) { this.vnApp.showError(this.$translate.instant(e.message)); return false; } } return this.onClose(); } onClose() { this.$.watcher.updateOriginalData(); } onMoveNext(calendars) { calendars.forEach(calendar => { calendar.moveNext(2); }); } onMovePrevious(calendars) { calendars.forEach(calendar => { calendar.movePrevious(2); }); } } Controller.$inject = ['$element', '$scope', '$http', '$filter', '$translate', '$stateParams', 'vnApp']; ngModule.component('vnZoneCalendar', { template: require('./index.html'), controller: Controller, bindings: { zone: '<' }, require: { card: '^vnZoneCard' } });