import ngModule from '../../module'; import './style.scss'; class Controller { constructor($scope, $state, $http, vnApp, $translate) { this.$scope = $scope; this.$state = $state; this.$stateParams = $state.params; this.$http = $http; this.vnApp = vnApp; this.$translate = $translate; this.clientSample = { clientFk: this.$stateParams.id }; } jsonToQuery(json) { let query = ''; for (let param in json) { if (query != '') query += '&'; query += `${param}=${json[param]}`; } return query; } showPreview() { let sampleType = this.$scope.sampleType.selection; let params = {clientFk: this.$stateParams.id}; 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.companyFk = this.clientSample.companyFk; let query = `/api/email/${sampleType.code}?${this.jsonToQuery(params)}`; this.$http.get(query).then(res => { if (res.data) { let dialog = this.$scope.showPreview.element; let body = dialog.querySelector('tpl-body'); let scroll = dialog.querySelector('div:first-child'); body.innerHTML = res.data; this.$scope.showPreview.show(); scroll.scrollTop = 0; } }); } onSubmit() { this.$scope.watcher.check(); this.$scope.watcher.realSubmit().then(() => this.sendSample() ); } sendSample() { let sampleType = this.$scope.sampleType.selection; let params = {clientFk: this.$stateParams.id}; if (sampleType.hasCompany) params.companyFk = this.clientSample.companyFk; let query = `/api/email/${sampleType.code}?${this.jsonToQuery(params)}`; this.$http.post(query).then(res => { if (res) { this.vnApp.showSuccess(this.$translate.instant('Notification sent!')); this.$state.go('client.card.sample.index'); } }); } } Controller.$inject = ['$scope', '$state', '$http', 'vnApp', '$translate']; ngModule.component('vnClientSampleCreate', { template: require('./index.html'), controller: Controller });