109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
import ngModule from '../../module';
|
|
import Section from 'salix/components/section';
|
|
import './style.scss';
|
|
|
|
class Controller extends Section {
|
|
constructor($element, $) {
|
|
super($element, $);
|
|
this.clientSample = {
|
|
clientFk: this.$params.id,
|
|
companyId: this.vnConfig.companyFk
|
|
};
|
|
}
|
|
|
|
get client() {
|
|
return this._client;
|
|
}
|
|
|
|
set client(value) {
|
|
this._client = value;
|
|
|
|
if (value) {
|
|
this.clientSample.recipient = value.email;
|
|
this.getWorkerEmail();
|
|
}
|
|
}
|
|
|
|
get companyId() {
|
|
if (!this.clientSample.companyFk)
|
|
this.clientSample.companyFk = this.vnConfig.companyFk;
|
|
return this.clientSample.companyFk;
|
|
}
|
|
|
|
set companyId(value) {
|
|
this.clientSample.companyFk = value;
|
|
}
|
|
|
|
onSubmit() {
|
|
this.$.watcher.check();
|
|
this.$.watcher.realSubmit().then(() =>
|
|
this.sendSample()
|
|
);
|
|
}
|
|
|
|
showPreview() {
|
|
this.send(true, res => {
|
|
this.$.showPreview.show();
|
|
const dialog = document.body.querySelector('div.vn-dialog');
|
|
const body = dialog.querySelector('tpl-body');
|
|
const scroll = dialog.querySelector('div:first-child');
|
|
|
|
body.innerHTML = res.data;
|
|
scroll.scrollTop = 0;
|
|
});
|
|
}
|
|
|
|
sendSample() {
|
|
this.send(false, () => {
|
|
this.vnApp.showSuccess(this.$t('Notification sent!'));
|
|
this.$state.go('client.card.sample.index');
|
|
});
|
|
}
|
|
|
|
send(isPreview, cb) {
|
|
const sampleType = this.$.sampleType.selection;
|
|
const params = {
|
|
recipientId: this.$params.id,
|
|
recipient: this.clientSample.recipient,
|
|
replyTo: this.clientSample.replyTo
|
|
};
|
|
|
|
if (!params.recipient)
|
|
return this.vnApp.showError(this.$t('Email cannot be blank'));
|
|
|
|
if (!sampleType)
|
|
return this.vnApp.showError(this.$t('Choose a sample'));
|
|
|
|
if (sampleType.hasCompany && !this.clientSample.companyFk)
|
|
return this.vnApp.showError(this.$t('Choose a company'));
|
|
|
|
if (sampleType.hasCompany)
|
|
params.companyId = this.clientSample.companyFk;
|
|
|
|
let query = `email/${sampleType.code}`;
|
|
if (isPreview)
|
|
query = `email/${sampleType.code}/preview`;
|
|
|
|
this.$http.get(query, {params}).then(cb);
|
|
}
|
|
|
|
getWorkerEmail() {
|
|
const userId = window.localStorage.currentUserWorkerId;
|
|
const params = {filter: {where: {userFk: userId}}};
|
|
this.$http.get('EmailUsers', params).then(res => {
|
|
const [worker] = res && res.data;
|
|
this.clientSample.replyTo = worker.email;
|
|
});
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$element', '$scope'];
|
|
|
|
ngModule.vnComponent('vnClientSampleCreate', {
|
|
template: require('./index.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
client: '<'
|
|
}
|
|
});
|