import ngModule from '../module'; import Section from 'salix/components/section'; export default class Controller extends Section { constructor($element, $) { super($element, $); this.$checkAll = false; this.smartTableOptions = { activeButtons: { search: true }, columns: [ { field: 'socialName', autocomplete: { url: 'Clients', showField: 'socialName', valueField: 'socialName' } }, { field: 'city', autocomplete: { url: 'Towns', valueField: 'name', showField: 'name' } } ] }; this.campaign = { id: null, from: null, to: null }; this.getUpcomingCampaing(); } get checked() { const clients = this.$.model.data || []; const checkedClients = []; for (const buy of clients) { if (buy.$checked) checkedClients.push(buy); } return checkedClients; } onChangeDate(value) { if (value) this.campaign.id = null; } getUpcomingCampaing() { this.$http.get('Campaigns/upcoming') .then(res => this.campaign.id = res.data.id); } get campaignSelection() { return this._campaignSelection; } set campaignSelection(value) { this._campaignSelection = value; if (value) { const from = new Date(value.dated); from.setDate(from.getDate() - value.scopeDays); this.campaign.to = value.dated; this.campaign.from = from; } } onSendClientConsumption() { const clientIds = this.checked.map(client => client.id); const params = Object.assign({ clientIds: clientIds }, this.campaign); this.$http.post('schedule/consumption', params) .then(() => this.$.filters.hide()) .then(() => this.vnApp.showSuccess(this.$t('Notifications sent!'))); } exprBuilder(param, value) { switch (param) { case 'search': return /^\d+$/.test(value) ? {id: value} : {or: [{name: {like: `%${value}%`}}, {socialName: {like: `%${value}%`}}]}; case 'phone': return { or: [ {phone: value}, {mobile: value} ] }; case 'name': case 'socialName': case 'city': case 'email': return {[param]: {like: `%${value}%`}}; case 'id': case 'fi': case 'postcode': case 'salesPersonFk': return {[param]: value}; } } } ngModule.vnComponent('vnClientNotification', { template: require('./index.html'), controller: Controller });