import { Component, EventEmitter, Injector, Output, ViewChild, Input } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { DriverIncidentTypesServiceProxy, GetDriverIncidentTypeForViewDto, OrderIncidentsServiceProxy, IncidentStatusServiceProxy, GetIncidentForUpdate, DriverIncidentNotes, DriverIncidentAttachment, CreateOrEditDriverIncidentsInput, DriverIncidentsServiceProxy, GetDriverList, EmployeeIncidentsServiceProxy, EmployeeIncidentTypesServiceProxy, CreateOrEditEmployeeIncidentsInput, GetManagerResult, GetEmployeeIncidentTypeForViewDto, GetEmployeeList } from '@shared/service-proxies/service-proxies'; import { ModalDirective, BsDatepickerConfig } from 'ngx-bootstrap'; import * as _ from 'lodash'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment'; import { Content } from '@angular/compiler/src/render3/r3_ast'; import { IncidentNotesModalComponent } from '@app/sprintship/controller/create-incidents/incident-notes-modal.component'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { Router } from '@angular/router'; import { AppSessionService } from '@shared/common/session/app-session.service'; @Component({ templateUrl: './create-employee-incident.component.html', styleUrls: ['./employee-incident.component.less'], styles: [`.user-edit-dialog-profile-image { margin-bottom: 20px; }`], animations: [appModuleAnimation()] }) export class CreateEmployeeIncidentComponent extends AppComponentBase { @Output() viewDriver = new EventEmitter(); @Input('driverId') driverId: string; @ViewChild('incidentNotesModal', { static: false }) incidentNotesModal: IncidentNotesModalComponent; bsConfig: Partial; isCreate: boolean = false; active = false; saving = false; managers: any; statuses: any; today: any; defaultDate = new Date(); driverIncident: { id: number, userId: number, incidentId: number, content: any, incidentDate: any, incidentTime: any, latitude: any, longitude: any, geocodeId: number } = {}; filter: any; input: any; users: GetEmployeeList[] = []; userInput: any; employeeIncidentType: GetEmployeeIncidentTypeForViewDto[]; notes: any[] = []; uploadUrl: string; uploadedFiles: any[] = []; data: any[] = []; showAlert: boolean = false; noteCreator: string; files: File[] = []; mId: any; sId: any; email:string; notesCreate: Array = []; attachMents: Array = []; forUpdate: GetIncidentForUpdate = new GetIncidentForUpdate(); timeId: any; driver: string; typesId: number; content: string = " "; constructor( injector: Injector, private _emplloyeeIncidentsServiceProxy: EmployeeIncidentsServiceProxy, private _employeeIncidentTypeServiceProxy: EmployeeIncidentTypesServiceProxy, private _incidentStatus : IncidentStatusServiceProxy, private _router: Router, private _appSessionService: AppSessionService, ) { super(injector); } ngOnInit() { $('.kt-select2').select2(); this.getEmployeeIncidentType(1000); this.getManagers(); this.getIncidentStatus(); this.today = new Date(); this.showAllUsers(1000); this.noteCreator = this._appSessionService.user.name + ' ' + this._appSessionService.user.surname; } create(): void { for(let item of this.notes) { let a = new DriverIncidentNotes({ incidentId: 0, notes: item, }); this.notesCreate.push(a); } for (let item of this.data) { let a = new DriverIncidentAttachment({ fileName: item.fileName, base64EncodedData: item.base64EncodedData, incidentId: 0, }); this.attachMents.push(a); } let that = this; let createEmployeeIncident = new CreateOrEditEmployeeIncidentsInput({ id : 0, incidentTypeId: that.typesId, geocodeId: 0, incidentContent: 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, employeeIncidentTypeId : Number($('#driverIncidentType1').val()) == 0 ? null : Number($('#driverIncidentType1').val()), userId : Number($('#userInputId').val()) == 0 ? null : Number($('#userInputId').val()), }); this.spinnerService.show(); this._emplloyeeIncidentsServiceProxy.createEmployeeIncident(createEmployeeIncident).subscribe(result=>{ this.spinnerService.hide(); this.typesId = 0; this.content = " "; this.files = []; this.data = []; this.notes = []; this.attachMents = []; this.notesCreate = []; this.notify.info(this.l('SavedSuccessfully')); this._router.navigateByUrl('/app/sprintship/employee-incidents'); }); } alertSaveSuccess(): void { this.notify.info(this.l('SavedSuccessfully')); this.close(); } showAllUsers(maxcount): void { this.active = true; this._emplloyeeIncidentsServiceProxy.getEmployeeList().subscribe((result) => { this.users = result; }); } clearField(): void { this.driverIncident.content = null; this.driverIncident.incidentDate = null; this.driverIncident.longitude = null; this.driverIncident.latitude = null; this.userInput = null; this.driverIncident.incidentTime = null; } close(): void { this.active = false; } getEmployeeIncidentType(maxcount): void { this._employeeIncidentTypeServiceProxy.getAll(undefined, undefined, undefined, undefined, undefined, -1, undefined, undefined, maxcount) .subscribe(result => { this.employeeIncidentType = result.items; }); } getManagers(): void { this._emplloyeeIncidentsServiceProxy.getManagers().subscribe(result => { this.managers = result; }) } getIncidentStatus(): void { this._incidentStatus.getAllIncidentStatus(undefined, undefined).subscribe(result => { this.statuses = result; }) } incidentNotes(): void { this.incidentNotesModal.show(); } attach(){ $('#FileDrop').click(); } 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); }; } closeAlert() { this.showAlert = false; } }