import { Component , OnInit , NgModule , TemplateRef , ViewChild , ViewContainerRef } from '@angular/core' ; import { Router , ActivatedRoute , Params , ParamMap } from '@angular/router' ; import { Http , HttpModule , Response } from '@angular/http' ; import { FormsModule } from '@angular/forms' ; import { SendFax , DocumentProgram } from './sendfax' ; import { SendFaxService } from './sendfax.service' ; import { DocumentService } from '../message/document/document.service' ; import { Document } from '../message/document/document' ; import { FileUploader , FileUploaderOptions } from 'ng2-file-upload' ; import { FileSelectDirective , FileDropDirective } from 'ng2-file-upload/ng2-file-upload' ; import { AppService } from '../../app.service' ; import 'rxjs/add/operator/toPromise' ; import { Contact } from '../contact/contact' ; import { ContactService } from '../contact/contact.service' ; import { CompleterService , CompleterData , CompleterItem } from 'ng2-completer' ; import { NgbActiveModal , NgbModal , ModalDismissReasons , NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ; @ Component ( { selector : 'ngx-add-sendfax-component' , templateUrl : './sendfax-form-component.html' , styleUrls : [ './sendfax-form-component.scss' ] , } ) export class AddSendFaxComponent implements OnInit { constructor ( private http , private route , private document_service , private sendfax_service , private router , private app_service , private contact_service , private completerService , private modalService ) { } form1 : any = { } ; documentProgram : DocumentProgram = new DocumentProgram ; program_id : any = null ; sendfax : SendFax = new SendFax ; documentArray : Document [ ] = [ ] ; document : Document = new Document ; selectedDocument : any ; trans_id : number ; dataService : CompleterData ; protected searchStr : string ; contactArray : Contact [ ] = [ ] ; public file_sending = false ; public file_sent = false ; file : any = [ ] ; document_id : any ; URL = `${ this . app_service . apiUrlDocument }/${ this . document_id }/media` ; public uploader : FileUploader = new FileUploader ( { url : this . URL , disableMultipart : true } ) ; unsupportedErr : any = false ; private modalRef : NgbModalRef ; ngOnInit ( ) { this . getDocumentlist ( ) ; this . getContactlist ( ) ; this . selectedDocument = 0 ; this . document . quality = 'standard' ; this . uploader . onBeforeUploadItem = ( item ) => { item . method = 'POST' ; item . url = this . URL ; item . withCredentials = false ; } ; this . uploader . onAfterAddingFile = ( response ) => { console . log ( response ) ; this . file = response ; if ( response . file . type == 'application/pdf' || response . file . type == 'image/png' || response . file . type == 'image/jpg' || response . file . type == 'image/jpeg' || response . file . type == 'image/tiff' || response . file . type == 'image/tif' ) { } else { this . unsupportedErr = true ; this . uploader . removeFromQueue ( response ) ; setTimeout ( ( ) => { this . unsupportedErr = false ; } , 2000 ) ; } } ; const authHeader = this . app_service . upload_Header ; const uploadOptions = < FileUploaderOptions > { headers : authHeader } ; this . uploader . setOptions ( uploadOptions ) ; this . uploader . onSuccessItem = ( item , response , status , headers ) => { } ; this . uploader . onCompleteAll = ( ) => { console . log ( 'complete' ) ; this . file_sent = true ; this . file_sending = false ; this . modalRef . close ( ) ; this . getDocumentlist ( ) ; } ; console . log ( this . document . name ) ; } sendnewFax ( ) { if ( this . documentProgram . document_id == 0 ) { this . addDocument ( ) ; } else { this . addSendDocument ( ) ; } } addSendDocument ( ) { if ( this . sendfax . contact_id != undefined ) { this . sendfax . phone = undefined ; } this . sendfax_service . add_senddocument ( this . documentProgram ) . then ( response => { const program_id = response ; this . sendfax . program_id = program_id ; this . AddTransmission ( ) ; this . router . navigate ( [ '../../sendfax' ] , { relativeTo : this . route } ) ; } ) ; } AddTransmission ( ) { this . sendfax_service . add_SendFax ( this . sendfax ) . then ( response => { const transmission_id = response ; this . trans_id = transmission_id ; this . AddSend ( this . trans_id ) ; } ) ; } AddSend ( trans_id ) { this . sendfax_service . send_transmission ( this . trans_id ) . then ( response => { } ) ; } getDocumentlist ( ) { this . document_service . get_DocumentList ( ) . then ( data => { this . documentArray = data ; console . log ( this . documentArray ) ; this . documentProgram . document_id = this . documentArray [ this . documentArray . length - 1 ] . document_id ; console . log ( this . documentProgram . document_id ) ; } ) ; } onSelect ( value ) { if ( value != 0 ) { this . selectedDocument = value ; this . documentProgram . document_id = value ; } else { this . documentProgram . document_id = undefined ; } } addDocument ( ) { this . document_service . add_Document ( this . document ) . then ( response => { const document_id = response ; this . documentProgram . document_id = response ; this . URL = `${ this . app_service . apiUrlDocument }/${ document_id }/media` ; this . upload ( ) ; } ) ; } upload ( ) { this . file_sending = true ; this . uploader . uploadAll ( ) ; } private hasBaseDropZoneOver = false ; private hasAnotherDropZoneOver = false ; private fileOverBase ( e ) { this . hasBaseDropZoneOver = e ; } private fileOverAnother ( e ) { this . hasAnotherDropZoneOver = e ; } getContactlist ( ) { this . contact_service . get_ContactList ( ) . then ( data => { this . contactArray = data ; this . dataService = this . completerService . local ( this . contactArray , 'phone' , 'phone' ) ; } ) ; } onSelected ( item ) { console . log ( item ) ; if ( item != null ) { this . sendfax . contact_id = item . originalObject . contact_id ; } else { this . sendfax . contact_id = undefined ; } } file_upload ( ) { this . document_service . add_Document ( this . document ) . then ( response => { const document_id = response ; this . documentProgram . document_id = response ; this . URL = `${ this . app_service . apiUrlDocument }/${ document_id }/media` ; this . upload ( ) ; } ) ; } remove ( item ) { console . log ( this . uploader . queue . length - 1 ) ; } private handleError ( error ) { console . error ( 'An error occurred' , error ) ; return Promise . reject ( error . message || error ) ; } open ( content ) { console . log ( 'open the modal' ) ; this . modalRef = this . modalService . open ( content , { size : 'lg' } ) ; this . modalRef . result . then ( ( result ) => { } , ( reason ) => { } ) ; } private getDismissReason ( reason ) { if ( reason === ModalDismissReasons . ESC ) { return 'by pressing ESC' ; } else if ( reason === ModalDismissReasons . BACKDROP_CLICK ) { return 'by clicking on a backdrop' ; } else { return `with: ${ reason }` ; } } onSave ( ) { this . modalRef . close ( ) ; } }