import ngModule from '../../module'; import './style.scss'; class Controller { constructor($scope, $state, $http, vnApp, $translate, $httpParamSerializer, $window) { this.$scope = $scope; this.$state = $state; this.$stateParams = $state.params; this.$http = $http; this.vnApp = vnApp; this.$translate = $translate; this.$window = $window; this.$httpParamSerializer = $httpParamSerializer; this.clientSample = { clientFk: this.$stateParams.id }; } get client() { return this._client; } set client(value) { this._client = value; if (value) this.clientSample.recipient = value.email; } showPreview() { let sampleType = this.$scope.sampleType.selection; if (!sampleType) return this.vnApp.showError(this.$translate.instant('Choose a sample')); if (sampleType.hasCompany && !this.clientSample.companyFk) return this.vnApp.showError(this.$translate.instant('Choose a company')); const params = { clientId: this.$stateParams.id, recipient: this.clientSample.recipient, isPreview: true }; if (sampleType.hasCompany) params.companyId = this.clientSample.companyFk; const serializedParams = this.$httpParamSerializer(params); const query = `email/${sampleType.code}?${serializedParams}`; this.$http.get(query).then(res => { this.$scope.showPreview.show(); let dialog = document.body.querySelector('div.vn-dialog'); let body = dialog.querySelector('tpl-body'); let scroll = dialog.querySelector('div:first-child'); body.innerHTML = res.data; scroll.scrollTop = 0; }); } onSubmit() { this.$scope.watcher.check(); this.$scope.watcher.realSubmit().then(() => this.sendSample() ); } sendSample() { let sampleType = this.$scope.sampleType.selection; let params = { clientId: this.$stateParams.id, recipient: this.clientSample.recipient }; if (!sampleType) return this.vnApp.showError(this.$translate.instant('Choose a sample')); if (sampleType.hasCompany && !this.clientSample.companyFk) return this.vnApp.showError(this.$translate.instant('Choose a company')); if (sampleType.hasCompany) params.companyId = this.clientSample.companyFk; const serializedParams = this.$httpParamSerializer(params); const query = `email/${sampleType.code}?${serializedParams}`; this.$http.get(query).then(res => { this.vnApp.showSuccess(this.$translate.instant('Notification sent!')); this.$state.go('client.card.sample.index'); }); } } Controller.$inject = ['$scope', '$state', '$http', 'vnApp', '$translate', '$httpParamSerializer', '$window']; ngModule.component('vnClientSampleCreate', { template: require('./index.html'), controller: Controller, bindings: { client: '<' } });