import ngModule from '../module'; import Section from 'salix/components/section'; import './style.scss'; class Controller extends Section { $onInit() { this.setParams(); } $postLink() { this.deliveryMethodFk = 'delivery'; } setParams() { const hasParams = this.$params.deliveryMethodFk || this.$params.geoFk || this.$params.agencyModeFk; if (hasParams) { if (this.$params.deliveryMethodFk) this.deliveryMethodFk = this.$params.deliveryMethodFk; if (this.$params.geoFk) this.geoFk = this.$params.geoFk; if (this.$params.agencyModeFk) this.agencyModeFk = this.$params.agencyModeFk; this.fetchData(); } } fetchData() { const params = { deliveryMethodFk: this.deliveryMethodFk, geoFk: this.geoFk, agencyModeFk: this.agencyModeFk }; this.$.data = null; this.$http.get(`Zones/getEvents`, {params}) .then(res => { let data = res.data; this.$.data = data; if (!data.events.length) this.vnApp.showMessage(this.$t('No service for the specified zone')); this.$state.go(this.$state.current.name, params); }); } get deliveryMethodFk() { return this._deliveryMethodFk; } set deliveryMethodFk(value) { this._deliveryMethodFk = value; let filter; if (value === 'pickUp') filter = {where: {code: 'PICKUP'}}; else filter = {where: {code: {inq: ['DELIVERY', 'AGENCY']}}}; this.$http.get(`DeliveryMethods`, {filter}).then(res => { const deliveryMethods = res.data.map(deliveryMethod => deliveryMethod.id); this.agencyFilter = {deliveryMethodFk: {inq: deliveryMethods}}; }); } onSelection($event, $events, $days) { if (!$events.length) return; const day = $days[0]; const zonesIds = []; for (let event of $events) zonesIds.push(event.zoneFk); this.$.zoneEvents.show($event.target); const zoneModel = this.$.zoneModel; zoneModel.applyFilter({ include: [ { relation: 'agencyMode', scope: {fields: ['name']} }, { relation: 'events', scope: { where: {dated: day} } }, ], where: { id: {inq: zonesIds} } }).then(() => { const data = zoneModel.data; for (let row of data) { const [event] = row.events; if (event && event.hour) row.hour = event.hour; if (event && event.price) row.price = event.price; } }); } preview(zone) { this.selectedZone = zone; this.$.summary.show(); } } ngModule.vnComponent('vnZoneDeliveryDays', { template: require('./index.html'), controller: Controller });