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, GetAllForLookupTableInput } 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'; import { FeatureCheckerService } from 'abp-ng2-module/dist/src/features/feature-checker.service'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; @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; @ViewChild('incidentTypesList', { static: true}) incidentTypesList: CommonLookupModalComponent; @ViewChild('managerList', { static: true}) managerList: CommonLookupModalComponent; @ViewChild('statusList', { static: true}) statusList: CommonLookupModalComponent; @ViewChild('sourceList', { static: true}) sourceList: CommonLookupModalComponent; bsConfig: Partial; incidentTypeDropDown = 0; 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; incidentSourceId: number; content: string = ""; complaint: string = ""; resolution: string = ""; investigation: 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; source:any; notesCreate: Array = []; accountDetails: string; phone: string = ""; attachMents: Array = []; lab: boolean; txtIncidentTypeFilter: any; selectedIncidentType: any; incidentTypeId: any; txtManagerFilter: any; selectedManager: any; managerId: any; txtIncidentStatusFilter: any; selectedIncidentStatus: any; statusId: any; txtSourceFilter: any; selectedSource: any; sourceId: any; 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, private _featureCheckerService: FeatureCheckerService ) { super(injector); } ngOnInit(): void { this.lab = abp.features.isEnabled('App.RouteManagementFeauture.ClinicManagement'); $('.kt-select2').select2(); this.today = new Date(); this.route.paramMap.subscribe(params => { this.id = params.get("id"); if(this.id == null){ this._router.navigateByUrl('/app/sprintship/order'); } this._orderAppService.getRouteCodeDriver( this.id ).pipe().subscribe(result => { this.driver = result; }); this._orderAppService.getAccountDetails( this.id ).pipe().subscribe(result => { if (this.lab) { if (result != null) { this.accountDetails = result.split("|")[0] + ' - ' + result.split("|")[1]; this.phone = result.split("|")[2] == '' ? null : result.split("|")[2]; } } else { this.accountDetails = ''; this.phone = '0'; } }); this._orderIncidentsService.getManagerByOrder( this.id ).pipe().subscribe(result => { this.getManager = result; }); }) this.timeId = localStorage.getItem('timeZoneId'); moment.tz.setDefault(this.timeId); this.getIncidentTypesInit(); this.getManagersInit(); this.getIncidentStatusInit(); this.getSourceInit(); } 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; }) this._orderIncidentsService.getIncidentSource().pipe().subscribe(result => { this.source = result; }); } getIncident(event?: LazyLoadEvent) { if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } } reloadPage(): void { this.paginator.changePage(this.paginator.getPage()); } private validateNum(event: KeyboardEvent): void { const pattern = /[0-9\+\-\(\)\ ]/; let inputChar = String.fromCharCode(event.charCode); if (event.keyCode != 8 && !pattern.test(inputChar)) { event.preventDefault(); } } 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; let createOrderIncidents = new CreateOrderIncidentInput({ complaint:that.complaint, resolution:that.resolution, investigation:that.investigation, incidentSourceId: this.sourceId, orderId: this.id, incidentTypeId: this.incidentTypeId, geocodeId: 0, content: that.content, incidentDate: moment.tz(this.defaultDate, this.timeId) , managerId : this.managerId, email : this.email, phone : !this.lab ? null : this.phone, incidentStatusId: this.statusId, 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.complaint = ""; this.resolution = ""; this.investigation = ""; this.incidentSourceId = 0; this.incidentTypeId = null; this.sourceId = null; this.statusId = null; this.managerId = null; 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 => { if (result != 0) { this._router.navigateByUrl('/app/main/laboratory/clinic-history/'+ result); } else { this._router.navigateByUrl('/app/sprintship/view-incidents/'+this.incidentId); } }); } else{this._router.navigateByUrl('/app/sprintship/update-incidents/'+this.incidentId); } }); } updateMyDate(newDate) { this.defaultDate = newDate; } incidentNotes(): void { this.incidentNotesModal.show(); } onSelect(event) { 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; // }); // } onRemove(event) { this.message.confirm( '', '', (isConfirmed) => { if (isConfirmed) { 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(); } getIncidentTypesInit() { this.incidentTypesList.configure({ title: this.l('Select Incident Type'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new GetAllForLookupTableInput(); input.filter = filter; input.skipCount = skipCount; input.maxResultCount = maxResultCount; return this._incidentTypesService.incidentTypePagedList(input); } }); } getIncidentTypesList(filter : any) { this.incidentTypesList.show(); this.incidentTypesList.filterText = filter; } clearIncidentTypesList(){ this.txtIncidentTypeFilter = ''; this.incidentTypeId = null; } selectIncidentType(item) { this.incidentTypeId = item.value; this.txtIncidentTypeFilter = item.name; } getManagersInit() { this.managerList.configure({ title: this.l('Select Assignee'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new GetAllForLookupTableInput(); input.filter = filter; input.skipCount = skipCount; input.maxResultCount = maxResultCount; return this._orderIncidentsService.managersPagedList(input); } }); } getManagerList(filter : any) { this.managerList.show(); this.managerList.filterText = filter; } clearManagerList(){ this.txtManagerFilter = ''; this.managerId = null; } selectManager(item) { this.managerId = item.value; this.txtManagerFilter = item.name; } getIncidentStatusInit() { this.statusList.configure({ title: this.l('Select Status'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new GetAllForLookupTableInput(); input.filter = filter; input.skipCount = skipCount; input.maxResultCount = maxResultCount; return this._incidentStatus.incidentStatusPagedList(input); } }); } getIncidentStatusList(filter : any) { this.statusList.show(); this.statusList.filterText = filter; } clearIncidentStatusList(){ this.txtIncidentStatusFilter = ''; this.statusId = null; } selectIncidentStatus(item) { this.statusId = item.value; this.txtIncidentStatusFilter = item.name; } getSourceInit() { this.sourceList.configure({ title: this.l('Select Fault Type'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new GetAllForLookupTableInput(); input.filter = filter; input.skipCount = skipCount; input.maxResultCount = maxResultCount; return this._orderIncidentsService.incidentSourcePagedList(input); } }); } getSourceList(filter : any) { this.sourceList.show(); this.sourceList.filterText = filter; } clearSourceList(){ this.txtSourceFilter = ''; this.sourceId = null; } selectSource(item) { this.sourceId = item.value; this.txtSourceFilter = item.name; } }