import ngModule from '../module'; import Component from 'core/lib/component'; class Controller extends Component { constructor($element, $) { super($element, $); this.order = {}; this.clientFk = this.$params.clientFk; } $onInit() { if (this.$params && this.$params.clientFk) this.clientFk = this.$params.clientFk; } set order(value) { if (value) this._order = value; } get order() { return this._order; } set clientFk(value) { this.order.clientFk = value; if (value) { let filter = { include: { relation: 'defaultAddress', scope: { fields: 'id' } }, where: {id: value} }; filter = encodeURIComponent(JSON.stringify(filter)); let query = `Clients?filter=${filter}`; this.$http.get(query).then(res => { if (res.data) { let client = res.data[0]; let defaultAddress = client.defaultAddress; this.addressFk = defaultAddress.id; } }); } else this.addressFk = null; } get clientFk() { return this.order.clientFk; } set addressFk(value) { this.order.addressFk = value; this.getAvailableAgencies(); } get addressFk() { return this.order.addressFk; } set landed(value) { this.order.landed = value; this.getAvailableAgencies(); } get landed() { return this.order.landed; } get warehouseFk() { return this.order.warehouseFk; } getAvailableAgencies() { let order = this.order; order.agencyModeFk = null; let params = { addressFk: order.addressFk, landed: order.landed }; if (params.landed && params.addressFk) { this.$http.get(`Agencies/landsThatDay`, {params}) .then(res => this._availableAgencies = res.data); } } onSubmit() { this.createOrder(); } createOrder() { let params = { landed: this.order.landed, addressId: this.order.addressFk, agencyModeId: this.order.agencyModeFk }; this.$http.post(`Orders/new`, params).then(res => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.$state.go('order.card.catalog', {id: res.data}); }); } } ngModule.component('vnOrderCreateCard', { template: require('./card.html'), controller: Controller, bindings: { order: '