import ngModule from '../../module'; import SectionDms from 'salix/components/section-dms'; class Controller extends SectionDms { constructor($element, $) { super($element, $); this.dms = { files: [], hasFile: false, hasFileAttached: false }; } get ticket() { return this._ticket; } set ticket(value) { this._ticket = value; if (value) { const params = {filter: { where: {code: 'ticket'} }}; this.getDmsTypes(params).then(res => this.handleDefaultParams(res)); this.getAllowedContentTypes().then(data => this.allowedContentTypes = data); } } get contentTypesInfo() { return this.$t('ContentTypesInfo', { allowedContentTypes: this.allowedContentTypes }); } handleDefaultParams(data) { const dmsTypeId = res?.data?.id; const warehouseId = this.vnConfig.warehouseFk; const defaultParams = { reference: this.ticket.id, warehouseId: warehouseId, companyId: this.ticket.companyFk, dmsTypeId: dmsTypeId, description: this.$t('FileDescription', { ticketId: this.ticket.id, clientId: this.ticket.client.id, clientName: this.ticket.client.name }).toUpperCase() }; this.dms = Object.assign(this.dms, defaultParams); } onSubmit() { const query = `tickets/${this.ticket.id}/uploadFile`; const options = { method: 'POST', url: query, params: this.dms, headers: { 'Content-Type': undefined }, transformRequest: files => { const formData = new FormData(); for (let i = 0; i < files.length; i++) formData.append(files[i].name, files[i]); return formData; }, data: this.dms.files }; this.$http(options).then(res => { if (res) { this.vnApp.showSuccess(this.$t('Data saved!')); this.$.watcher.updateOriginalData(); this.$state.go('ticket.card.dms.index'); } }); } onFileChange(files) { let hasFileAttached = false; if (files.length > 0) hasFileAttached = true; this.$.$applyAsync(() => { this.dms.hasFileAttached = hasFileAttached; }); } } ngModule.vnComponent('vnTicketDmsCreate', { template: require('./index.html'), controller: Controller, bindings: { ticket: '<' } });