import { Component, ViewChild, Injector, Output, EventEmitter, OnInit } from '@angular/core'; import { ModalDirective, TabsetComponent } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { RouteHandOffDefinitionsServiceProxy, CreateOrEditRouteHandOffDefinitionDto, GetManagerListDto, RouteStopsServiceProxy, GetRouteStopForViewDto, LocationServiceProxy, LocationListDto, LocationNameDto, DriversServiceProxy, OrderIncidentsServiceProxy, GetAllRouteStopsInput } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { RouteHandOffDefinitionRouteTemplateLookupTableModalComponent } from './routeHandOffDefinition-routeTemplate-lookup-table-modal.component'; import { RouteHandOffDefinitionRouteTypeLookupTableModalComponent } from './routeHandOffDefinition-routeType-lookup-table-modal.component'; import { AppSessionService } from '@shared/common/session/app-session.service'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; declare let $: any; @Component({ selector: 'createOrEditRouteHandOffDefinitionModal', templateUrl: './create-or-edit-routeHandOffDefinition-modal.component.html' }) export class CreateOrEditRouteHandOffDefinitionModalComponent extends AppComponentBase implements OnInit { @ViewChild('routeHandOffDefinitionTab', { static: false }) staticTabs: TabsetComponent; @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild('routeStopList', {static: true}) routeStopList: CommonLookupModalComponent; @ViewChild('routeHandOffDefinitionRouteTemplateLookupTableModal', { static: true }) routeHandOffDefinitionRouteTemplateLookupTableModal: RouteHandOffDefinitionRouteTemplateLookupTableModalComponent; @ViewChild('routeHandOffDefinitionRouteTypeLookupTableModal', { static: true }) routeHandOffDefinitionRouteTypeLookupTableModal: RouteHandOffDefinitionRouteTypeLookupTableModalComponent; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; routeHandOffDefinition: CreateOrEditRouteHandOffDefinitionDto = new CreateOrEditRouteHandOffDefinitionDto(); maxcount = 1000; routeTemplateName = ''; routeTypeRouteTypeCode = ''; getManagersListDto: GetManagerListDto[]; getRouteStops: GetRouteStopForViewDto[] = []; locations: LocationNameDto[]; currentDate: Date; routeTypes: any; drivers: Array; managers: Array; activeStatus: boolean; eta: any; cutOff: any; toSave: true; startTime: any; endTime: any; showSearchOnRouteStop : boolean; showSearchOnRouteStart : boolean; showSearchOnRouteCode : boolean routeStart : string; routeStop : string; searchRouteStop : boolean; routeStopId : number; routeStartId : number; constructor( injector: Injector, private _routeHandOffDefinitionsServiceProxy: RouteHandOffDefinitionsServiceProxy, private _routeStopsServiceProxy: RouteStopsServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _driverServiceProxy: DriversServiceProxy, private _orderIncidentAppService: OrderIncidentsServiceProxy, private _appSessionService: AppSessionService ) { super(injector); } ngOnInit(): void { this.routeStopModalInit(); } routeStopModalInit(){ this.routeStopList.configure({ title: this.l('Select Stop'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?:number) => { let input = new GetAllRouteStopsInput(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._routeStopsServiceProxy.showPagedRouteStop(input); } }); } onShow(){ let toggleStartActive = (bool) => { return this.routeStartActiveToggle(bool) } $('#routeStartOption').on('select2:select', function (e) { if($('#routeStartOption').val() == '0'){ toggleStartActive(false) } else{ toggleStartActive(true) } }); } getRouteStop(routeStopFilter : any){ this.searchRouteStop = true; this.routeStopList.show(); this.routeStopList.filterText = routeStopFilter; } getRouteStart(routeStartFilter : any){ this.searchRouteStop = false; this.routeStopList.show(); this.routeStopList.filterText = routeStartFilter; } clearRouteStop(){ this.routeStop = ''; this.routeStopId = null; this.showSearchOnRouteStop = true; } clearRouteStart(){ this.routeStart = ''; this.routeStartId = null; this.showSearchOnRouteStart = true; } selectRouteStop(item){ var resultId = item.value; //this.getClinicWithRoute(orderId); if(this.searchRouteStop){ this._routeStopsServiceProxy.getRouteStop(resultId) .subscribe(result => { this.routeStopId = resultId; this.routeStop = result.routeStop.company + ' - ' + result.contactAddress; this.searchRouteStop = false; this.showSearchOnRouteStop = false; }); } else{ this._routeStopsServiceProxy.getRouteStop(resultId) .subscribe(result => { this.showSearchOnRouteStart = false; this.routeStartId = resultId; this.routeStart = result.routeStop.company + ' - ' + result.contactAddress; }); } } show(routeHandOffDefinitionId?: number): void { this.showSearchOnRouteStop = true; this.showSearchOnRouteStart = true; this.showSearchOnRouteCode = true; this.routeStartId =null; this.routeStopId =null; if (!routeHandOffDefinitionId) { this.routeHandOffDefinition = new CreateOrEditRouteHandOffDefinitionDto(); this.getAllDropDowns(); this.routeHandOffDefinition.id = routeHandOffDefinitionId; this.routeHandOffDefinition.assignedDate = moment().startOf('day'); this.routeTemplateName = ''; this.routeTypeRouteTypeCode = ''; this.eta = ''; this.routeStart = ''; this.routeStop = ''; this.active = true; this.modal.show(); } else { this._routeHandOffDefinitionsServiceProxy.getRouteHandOffDefinitionForEdit(routeHandOffDefinitionId).subscribe(result => { this.getAllDropDowns(); this.routeHandOffDefinition = result.routeHandOffDefinition; this.routeHandOffDefinition.routeCode = result.routeTemplateName; this.routeTypeRouteTypeCode = result.routeTypeRouteTypeCode; this.eta = result.routeHandOffDefinition.eta.toDate(); this.cutOff = result.routeHandOffDefinition.cutOff.toDate(); this.activeStatus = !this.routeHandOffDefinition.inactive; this.startTime = moment(moment(this.currentDate).format('ddd MMM DD YYYY') + ' ' + result.routeHandOffDefinition.startTime + ':00').toDate(); this.endTime = moment(moment(this.currentDate).format('ddd MMM DD YYYY') + ' ' + result.routeHandOffDefinition.endTime + ':00').toDate(); this.showSearchOnRouteCode = false; if(result.routeHandOffDefinition.routeStartContactId != null){ this._routeStopsServiceProxy.getRouteStop(result.routeHandOffDefinition.routeStartContactId) .subscribe(res => { this.showSearchOnRouteStart = false; this.routeStartId = result.routeHandOffDefinition.routeStartContactId; this.routeStart = res.routeStop.company + ' - ' + res.contactAddress }); } this._routeStopsServiceProxy.getRouteStop(result.routeHandOffDefinition.defaultContactId) .subscribe(res => { this.showSearchOnRouteStop = false; this.routeStopId = result.routeHandOffDefinition.defaultContactId; this.routeStop = res.routeStop.company + ' - ' + res.contactAddress }); this.active = true; this.modal.show(); }); } } save(): void { let drivers = []; this.saving = true; this.routeHandOffDefinition.locationId = Number((document.getElementById('txtLocation')).value); // this.routeHandOffDefinition.defaultContactId = Number((document.getElementById('routeStop')).value); // this.routeHandOffDefinition.routeStartContactId = Number((document.getElementById('routeStart')).value); this.routeHandOffDefinition.routeStartContactId = this.routeStartId == null ? null :this.routeStartId ; this.routeHandOffDefinition.defaultContactId = this.routeStopId; this.routeHandOffDefinition.managerId = Number((document.getElementById('selectedManager')).value) == 0 ? null : Number((document.getElementById('selectedManager')).value); this.routeHandOffDefinition.assignedById = this._appSessionService.userId; if (document.getElementById('driverMonday') || document.getElementById('driverTuesday') || document.getElementById('driverWednesday') || document.getElementById('driverThursday') || document.getElementById('driverFriday')) { for (let index = 0; index < document.getElementsByClassName('route-driver').length; index++) { if (Number((document.getElementsByClassName('route-driver')[index]).value) !== 0) { drivers.push(Number((document.getElementsByClassName('route-driver')[index]).value)); } } if (drivers.length > 0) { this.routeHandOffDefinition.defaultUserId = drivers[0]; } else { this.routeHandOffDefinition.defaultUserId = 0; } this.routeHandOffDefinition.sundayDriverId = 0; this.routeHandOffDefinition.saturdayDriverId = 0; } if (document.getElementById('driverMonday')) { this.routeHandOffDefinition.mondayDriverId = Number((document.getElementById('driverMonday')).value); } if (document.getElementById('driverTuesday')) { this.routeHandOffDefinition.tuesdayDriverId = Number((document.getElementById('driverTuesday')).value); } if (document.getElementById('driverWednesday')) { this.routeHandOffDefinition.wednesdayDriverId = Number((document.getElementById('driverWednesday')).value); } if (document.getElementById('driverThursday')) { this.routeHandOffDefinition.thursdayDriverId = Number((document.getElementById('driverThursday')).value); } if (document.getElementById('driverFriday')) { this.routeHandOffDefinition.fridayDriverId = Number((document.getElementById('driverFriday')).value); } if (document.getElementById('driverSaturday')) { this.routeHandOffDefinition.defaultUserId = Number((document.getElementById('driverSaturday')).value); this.routeHandOffDefinition.saturdayDriverId = Number((document.getElementById('driverSaturday')).value); this.routeHandOffDefinition.mondayDriverId = 0; this.routeHandOffDefinition.tuesdayDriverId = 0; this.routeHandOffDefinition.wednesdayDriverId = 0; this.routeHandOffDefinition.thursdayDriverId = 0; this.routeHandOffDefinition.fridayDriverId = 0; this.routeHandOffDefinition.sundayDriverId = 0; } if (document.getElementById('driverSunday')) { this.routeHandOffDefinition.defaultUserId = Number((document.getElementById('driverSunday')).value); this.routeHandOffDefinition.sundayDriverId = Number((document.getElementById('driverSunday')).value); this.routeHandOffDefinition.mondayDriverId = 0; this.routeHandOffDefinition.tuesdayDriverId = 0; this.routeHandOffDefinition.wednesdayDriverId = 0; this.routeHandOffDefinition.thursdayDriverId = 0; this.routeHandOffDefinition.fridayDriverId = 0; this.routeHandOffDefinition.saturdayDriverId = 0; } if (document.getElementById('managerMonday') || document.getElementById('managerTuesday') || document.getElementById('managerWednesday') || document.getElementById('managerThursday') || document.getElementById('managerFriday')) { this.routeHandOffDefinition.saturdayManagerId = 0; this.routeHandOffDefinition.sundayManagerId = 0; } if (document.getElementById('managerMonday')) { this.routeHandOffDefinition.mondayManagerId = Number((document.getElementById('managerMonday')).value); } if (document.getElementById('managerTuesday')) { this.routeHandOffDefinition.tuesdayManagerId = Number((document.getElementById('managerTuesday')).value); } if (document.getElementById('managerWednesday')) { this.routeHandOffDefinition.wednesdayManagerId = Number((document.getElementById('managerWednesday')).value); } if (document.getElementById('managerThursday')) { this.routeHandOffDefinition.thursdayManagerId = Number((document.getElementById('managerThursday')).value); } if (document.getElementById('managerFriday')) { this.routeHandOffDefinition.fridayManagerId = Number((document.getElementById('managerFriday')).value); } if (document.getElementById('managerSaturday')) { this.routeHandOffDefinition.saturdayManagerId = Number((document.getElementById('managerSaturday')).value); this.routeHandOffDefinition.mondayManagerId = 0; this.routeHandOffDefinition.tuesdayManagerId = 0; this.routeHandOffDefinition.wednesdayManagerId = 0; this.routeHandOffDefinition.thursdayManagerId = 0; this.routeHandOffDefinition.fridayManagerId = 0; this.routeHandOffDefinition.sundayManagerId = 0; } if (document.getElementById('managerSunday')) { this.routeHandOffDefinition.sundayManagerId = Number((document.getElementById('managerSunday')).value); this.routeHandOffDefinition.mondayManagerId = 0; this.routeHandOffDefinition.tuesdayManagerId = 0; this.routeHandOffDefinition.wednesdayManagerId = 0; this.routeHandOffDefinition.thursdayManagerId = 0; this.routeHandOffDefinition.fridayManagerId = 0; this.routeHandOffDefinition.saturdayManagerId = 0; } this.routeHandOffDefinition.inactive = !this.activeStatus; this.routeHandOffDefinition.routeTemplateTypeId = 1; this.routeHandOffDefinition.routeTypeId = Number((document.getElementById('routeType')).value); this.routeHandOffDefinition.eta = this.eta; this.routeHandOffDefinition.cutOff = this.cutOff; this.routeHandOffDefinition.endTime = new Date(this.endTime).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric" }) as any; this.routeHandOffDefinition.startTime = new Date(this.startTime).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric" }) as any; if (this.routeHandOffDefinition.routeTypeId === 0) { this.saving = false; (document.getElementById('routeTypeDiv')).style.color = '#FF3300'; } if (this.routeHandOffDefinition.defaultContactId === 0) { this.saving = false; (document.getElementById('routeStopDiv')).style.color = '#FF3300'; } if (this.routeHandOffDefinition.locationId === 0) { this.saving = false; (document.getElementById('locationDiv')).style.color = '#FF3300'; } if (this.saving == true) { this._routeHandOffDefinitionsServiceProxy.createOrEdit(this.routeHandOffDefinition) .pipe( finalize(() => { this.saving = false; }) ) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } } openSelectRouteTemplateModal() { this.routeHandOffDefinitionRouteTemplateLookupTableModal.filterText = this.routeHandOffDefinition.routeCode; this.routeHandOffDefinitionRouteTemplateLookupTableModal.id = this.routeHandOffDefinition.routeTemplateId; this.routeHandOffDefinitionRouteTemplateLookupTableModal.displayName = this.routeHandOffDefinition.routeCode; this.routeHandOffDefinitionRouteTemplateLookupTableModal.displayName = this.routeTemplateName; this.routeHandOffDefinitionRouteTemplateLookupTableModal.show(); } openSelectRouteTypeModal() { this.routeHandOffDefinitionRouteTypeLookupTableModal.id = this.routeHandOffDefinition.routeTypeId; this.routeHandOffDefinitionRouteTypeLookupTableModal.displayName = this.routeTypeRouteTypeCode; this.routeHandOffDefinitionRouteTypeLookupTableModal.show(); } setRouteTemplateIdNull() { this.showSearchOnRouteCode = true; this.routeHandOffDefinition.routeTemplateId = null; this.routeHandOffDefinition.routeCode = ''; this.routeTemplateName = ''; } setRouteTypeIdNull() { this.routeHandOffDefinition.routeTypeId = null; this.routeTypeRouteTypeCode = ''; } getNewRouteTemplateId() { this.showSearchOnRouteCode = false; this.routeHandOffDefinition.routeTemplateId = this.routeHandOffDefinitionRouteTemplateLookupTableModal.id; this.routeHandOffDefinition.routeCode = this.routeHandOffDefinitionRouteTemplateLookupTableModal.displayName; this.routeTemplateName = this.routeHandOffDefinitionRouteTemplateLookupTableModal.displayName; } getNewRouteTypeId() { this.routeHandOffDefinition.routeTypeId = this.routeHandOffDefinitionRouteTypeLookupTableModal.id; this.routeTypeRouteTypeCode = this.routeHandOffDefinitionRouteTypeLookupTableModal.displayName; } close(): void { this.active = false; this.modal.hide(); } getAllDropDowns() { jQuery(document).ready(function() { $('.kt-select2').select2(); // $('.select2-container').css('width', '100%'); $('.route-driver').on('select2:select', function (e) { for (let index = 0; index < $('.route-driver').length; index++) { if ($('.route-driver').eq(index).val() == '') { $('.route-driver').eq(index).val(this.value); } } $('.route-driver').trigger('change'); }); }); this.getFilterRouteManagers(); this.getManagers(); // this.getStops(); this.getLocations(); this.getDrivers(); this.getRouteTypes(); } getManagers() { this._routeHandOffDefinitionsServiceProxy.getManagersList().subscribe(result => { this.getManagersListDto = result; }); } getFilterRouteManagers() { this._orderIncidentAppService.getManagers().subscribe(result => { this.managers = result; }); } getStops() { this._routeStopsServiceProxy.getRouteStopForDropDown().subscribe(result => { this.getRouteStops = result.items; }); } routeStartActiveToggle(status){ this.routeHandOffDefinition.routeStartInactive = status; } getLocations() { this._locationServiceProxy.getLocationNames(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined).subscribe(result => { this.locations = result; }); } getDrivers() { this._driverServiceProxy.getDriverList().subscribe(result => { this.drivers = result; }); } getRouteTypes() { this._routeHandOffDefinitionsServiceProxy.getAllRouteTypeForLookupTable(undefined, undefined, undefined, this.maxcount).subscribe(result => { this.routeTypes = result.items; }); } }