import { Component, Injector, ViewEncapsulation, ViewChild, OnInit, AfterViewInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { NotifyService } from '@abp/notify/notify.service'; import { AppComponentBase } from '@shared/common/app-component-base'; import { TokenAuthServiceProxy, WillCallServiceServiceProxy, IncidentStatusServiceProxy, IncidentStatusListDto, CreateOrderIncidentInput, OrderIncidentNotes, OrderIncidentAttachment, IncidentServiceProxy, GetIncidentForUpdate } from '@shared/service-proxies/service-proxies'; import { OrderServiceProxy, OrderListDto, OrderRouteListDto, RouteVehicleListDto, UserListDto, OrderIncidentsServiceProxy, CreateOrderIncidentDto, RouteTemplateListDto, ContactListDto, IncidentTypesServiceProxy, ListResultDtoOfIncidentTypeDto } from '@shared/service-proxies/service-proxies'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { IncidentNotesModalComponent } from './incident-notes-modal.component'; import { Table } from 'primeng/components/table/table'; import { Paginator } from 'primeng/components/paginator/paginator'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { FileDownloadService } from '@shared/utils/file-download.service'; import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component'; import * as _ from 'lodash'; import * as moment from 'moment'; import { Router } from '@angular/router'; import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker'; import { HttpClient } from '@angular/common/http'; import { FormControl, FormGroup } from '@angular/forms'; import {Location} from '@angular/common'; import { NgxDropzoneModule } from 'ngx-dropzone'; import { AppSessionService } from '@shared/common/session/app-session.service'; import { catchError } from 'rxjs/operators'; import { throwError } from 'rxjs'; @Component({ templateUrl: './create-incident.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./incident.less'], animations: [appModuleAnimation()] }) export class CreateIncidentComponent extends AppComponentBase implements AfterViewInit { @ViewChild('dataTable', { static: true }) dataTable: Table; @ViewChild('paginator', { static: true }) paginator: Paginator; @ViewChild('incidentNotesModal', {static: false}) incidentNotesModal: IncidentNotesModalComponent; bsConfig: Partial; incidentId: number = 0; id: any; defaultDate = new Date(); orderDetails: OrderRouteListDto = new OrderRouteListDto(); vehicle: RouteVehicleListDto = new RouteVehicleListDto(); users: UserListDto = new UserListDto(); routeTemplate: RouteTemplateListDto = new RouteTemplateListDto(); contacts: ContactListDto= new ContactListDto(); incidentTypes : ListResultDtoOfIncidentTypeDto = new ListResultDtoOfIncidentTypeDto(); createOrderIncident: CreateOrderIncidentDto = new CreateOrderIncidentDto(); incidentDate: any; incidentType: any; typesId: number; content: string = " "; notes: any[] = []; managers: any; uploadUrl: string; uploadedFiles: any[] = []; data: any[] = []; showAlert: boolean = false; noteCreator: string; files: File[] = []; today: any; statuses: any; mId: any; sId: any; email:string; getManager:string; notesCreate: Array = []; attachMents: Array = []; forUpdate: GetIncidentForUpdate = new GetIncidentForUpdate(); timeId: any; driver: string; constructor( injector: Injector, private _notifyService: NotifyService, private _tokenAuth: TokenAuthServiceProxy, private _activatedRoute: ActivatedRoute, private _httpClient: HttpClient, private _router: Router, private route: ActivatedRoute, private _orderAppService: OrderServiceProxy, private _incidentTypesService: IncidentTypesServiceProxy, private _orderIncidentsService : OrderIncidentsServiceProxy, private _incidentStatus : IncidentStatusServiceProxy, private _willcallService: WillCallServiceServiceProxy, private _appSessionService: AppSessionService, private _incidentAppService: IncidentServiceProxy, private _location: Location ) { super(injector); } ngOnInit(): void { $('.kt-select2').select2(); this.today = new Date(); this.route.paramMap.subscribe(params => { this.id = params.get("id"); this._orderAppService.getRouteCodeDriver( this.id ).pipe().subscribe(result => { this.driver = result; }); this._orderIncidentsService.getManagerByOrder( this.id ).pipe().subscribe(result => { this.getManager = result; }); }) this.timeId = localStorage.getItem('timeZoneId'); moment.tz.setDefault(this.timeId); } ngAfterViewInit(){ this.noteCreator = this._appSessionService.user.name + ' ' + this._appSessionService.user.surname; this._incidentTypesService.getAllIncidentType().subscribe(result=>{ this.incidentTypes = result; }); this._orderIncidentsService.getManagers() .subscribe(result=>{ this.managers = result; }) this._incidentStatus.getAllIncidentStatus(undefined,undefined) .subscribe(result=>{ this.statuses = result; }) } getIncident(event?: LazyLoadEvent) { if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } } reloadPage(): void { this.paginator.changePage(this.paginator.getPage()); } closeAlert() { this.showAlert = false; } save(): void { this.spinnerService.show(); for(let item of this.notes) { let a = new OrderIncidentNotes({ incidentId: 0, notes: item, }); this.notesCreate.push(a); } for (let item of this.data) { let a = new OrderIncidentAttachment({ fileName: item.fileName, base64EncodedData: item.base64EncodedData, incidentId: 0, }); this.attachMents.push(a); } let that = this; that.typesId = Number($('#selectedIncidentTypeId').val()); let createOrderIncidents = new CreateOrderIncidentInput({ orderId: this.id, incidentTypeId: that.typesId, geocodeId: 0, content: that.content, incidentDate: moment.tz(this.defaultDate, this.timeId) , managerId : Number($('#Manager').val()), email : this.email, incidentStatusId: Number($('#Status').val()), createAttachments: that.attachMents, createNotes: that.notesCreate }); this._orderIncidentsService.createOrderIncident(createOrderIncidents) .pipe( catchError((err) => { this.spinnerService.hide(); return throwError(err); }) ) .subscribe(result=>{ this.spinnerService.hide(); this.typesId = 0; this.content = " "; this.files = []; this.data = []; this.notes = []; this.attachMents = []; this.notesCreate = []; this.incidentId = result; this.notify.info(this.l('SavedSuccessfully')); //this._router.navigateByUrl('/app/sprintship/update-incidents/'+this.incidentId); if(this.id > 0 || this.id != null){ this._orderIncidentsService.getClinicId( this.id ).pipe( catchError((err) => { return throwError(err); }) ).subscribe(result => { this._router.navigateByUrl('/app/main/laboratory/clinic-history/'+ result); }); } else{this._router.navigateByUrl('/app/sprintship/update-incidents/'+this.incidentId); } }); } updateMyDate(newDate) { this.defaultDate = newDate; } incidentNotes(): void { this.incidentNotesModal.show(); } onSelect(event) { console.log(event.addedFiles); if(event.rejectedFiles.length != 0) { this.showAlert = true; } this.files.push(...event.addedFiles); let f = event.addedFiles; for (const file of f) { this.getData(file); } } onRemove(event) { this.files.splice(this.files.indexOf(event), 1); _.remove(this.data, function (el) { return el.fileName === event.name; }); } getData(file) { var reader:any; let me = this; reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function() { me.data.push( { fileName: file.name, base64EncodedData: reader.result.toString().split(',')[1] }); }; reader.onerror = function (error) { console.log('Error: ', error); }; } attach(){ $('#FileDrop').click(); } goBack(){ this._location.back(); } }