import { Component, ViewChild, Injector, Output, EventEmitter, Input } from '@angular/core'; import { ModalDirective, TabsetComponent } from 'ngx-bootstrap'; import { finalize, isEmpty } from 'rxjs/operators'; import { RouteDefinitionsServiceProxy, CreateOrEditRouteDefinitionDto, RouteTypesServiceProxy, RouteDefinitionRouteTypeLookupTableDto, ContactServiceProxy, OrganizationUnitDto, OrderNotificationsServiceProxy, OrganizationUnitServiceProxy, SupervisorDto, ContactListDto, LocationServiceProxy, LocationListDto, RouteTemplateTypeServiceProxy, RouteTemplateTypeListDto, UserServiceProxy, RoleServiceProxy, DriversServiceProxy, UserListRoleDto, UserListDto, RouteStopsServiceProxy, RouteStopDto, GetRouteStopForViewDto, GetManagerListDto, WillCallServiceServiceProxy, GetDriverListDto, OrderIncidentsServiceProxy, LocationNameDto, GetUnassignedDrivers, GetDriverList } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { RouteDefinitionRouteTypeLookupTableModalComponent } from './routeDefinition-routeType-lookup-table-modal.component'; import { AppSessionService } from '@shared/common/session/app-session.service'; declare var KTWizard: any; declare var $: any; declare var KTApp: any; @Component({ selector: 'createOrEditRouteDefinitionModal', templateUrl: './create-or-edit-routeDefinition-modal.component.html' }) export class CreateOrEditRouteDefinitionModalComponent extends AppComponentBase { @ViewChild('routeDefTab', { static: false }) staticTabs: TabsetComponent; @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild('routeDefinitionRouteTypeLookupTableModal', { static: true }) routeDefinitionRouteTypeLookupTableModal: RouteDefinitionRouteTypeLookupTableModalComponent; @Output() modalSave: EventEmitter = new EventEmitter(); maxcount = 1000; active = false; saving = false; activeTabIndex: number = 0; routeDefinition: CreateOrEditRouteDefinitionDto = new CreateOrEditRouteDefinitionDto(); locations: LocationNameDto[] = []; templateTypes: RouteTemplateTypeListDto[] = []; users: GetDriverList[] = []; contacts: ContactListDto[] = []; routeTypes: any; routeStops: GetRouteStopForViewDto[] = []; routeTypeRouteTypeCode = ''; routeTemplateName = ''; currentDateTime: Date; eta: any; cutOff: any; activeStatus: boolean; drivers: Array; contact_drivers: Array; selectedDrivers: Array; managerId: number; getManagerListDto: GetManagerListDto[]; appSession: AppSessionService; managers: any; allOrganizationUnits: OrganizationUnitDto[]; supervisors: SupervisorDto[] = []; activeTab: boolean = false; addSupervisors: any; allUsers: UserListDto[]; delete: any; isEmail: boolean; supervisorList: Array = []; constructor( injector: Injector, private _routeDefinitionsServiceProxy: RouteDefinitionsServiceProxy, private _contactServiceProxy: ContactServiceProxy, private _driverService: DriversServiceProxy, private _routeStopsService: RouteStopsServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _routeTemplateTypeServiceProxy: RouteTemplateTypeServiceProxy, private _routeTypesServiceProxy: RouteTypesServiceProxy, private _willCallService: WillCallServiceServiceProxy, private _orderIncidentAppService: OrderIncidentsServiceProxy, private _orderNotificationsServiceProxy: OrderNotificationsServiceProxy, private _organizationUnitService: OrganizationUnitServiceProxy ) { super(injector); } show(routeDefinitionId?: number): void { let day = []; this.supervisors = []; this.allOrganizationUnits = []; this.allUsers = []; this.supervisorList = []; $('.organization-unit').select2(); $('.user').select2(); this._orderNotificationsServiceProxy.getAllUserForTableDropdown().subscribe(result => { this.allUsers = result; }); this._orderNotificationsServiceProxy.getAllOrgUnitsForTableDropdown().subscribe(result => { this.allOrganizationUnits = result; }); if (!routeDefinitionId) { this.routeDefinition = new CreateOrEditRouteDefinitionDto(); this.getAllDropDown(); this.routeDefinition.id = routeDefinitionId; this.routeTypeRouteTypeCode = ''; this.routeTemplateName = ''; this.eta = ''; // // this.activeTabIndex = 0; this.active = true; this.modal.show(); } else { this._routeDefinitionsServiceProxy.getRouteDefinitionForEdit(routeDefinitionId).subscribe(result => { this.getAllDropDown(); this.routeDefinition = result.routeDefinition; this.routeTypeRouteTypeCode = result.routeTypeRouteTypeCode; //this.routeDefinition.routeCode = result.routeTypeRouteTypeCode; this.routeDefinition.routeCode = result.routeTemplateName; //this.routeDefinition.eta = this.routeDefinition.eta; this.eta = result.routeDefinition.eta.toDate(); this.cutOff = result.routeDefinition.cutOff.toDate(); //moment(moment(this.currentDateTime + ' ' + result.routeDefinition.cutOff).toDate(); //this.routeDefinition.eta = this.eta; this.routeDefinition.cutOff = this.cutOff; if (this.routeDefinition.inactive == true) { this.activeStatus = false; } else { this.activeStatus = true; } if(result.routeDefinition.supervisors != undefined) { this.supervisors = result.routeDefinition.supervisors; let s = this; setTimeout(() => { $('.organization-unit').on('select2:select', function (e) { s.updateUnits($(e.currentTarget).attr('id'), $(e.currentTarget).val()); }); $('.user').on('select2:select', function (e) { s.updateUsers($(e.currentTarget).attr('id'), $(e.currentTarget).val()); }); }, 100); } this.activeTabIndex = 0; this.active = true; this.modal.show(); }); } } save(): void { let drivers = []; this.saving = true; //this.currentDateTime = moment(moment().toISOString()).toDate(); this.routeDefinition.locationId = Number((document.getElementById('txtLocation')).value); this.routeDefinition.defaultContactId = Number((document.getElementById('routeStop')).value); this.routeDefinition.routeStartContactId = Number((document.getElementById('routeStart')).value); this.routeDefinition.managerId = Number((document.getElementById('selectedManager')).value) == 0 ? null : Number((document.getElementById('selectedManager')).value); this.routeDefinition.assignedById = this.appSession.userId; if (document.getElementById('driverMonday') || document.getElementById('driverTuesday') || document.getElementById('driverWednesday') || document.getElementById('driverThursday') || document.getElementById('driverFriday')) { // let default = 0; 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.routeDefinition.defaultUserId = drivers[0]; } else { this.routeDefinition.defaultUserId = 0; } this.routeDefinition.sundayDriverId = 0; this.routeDefinition.saturdayDriverId = 0; } if (document.getElementById('driverMonday')) { this.routeDefinition.mondayDriverId = Number((document.getElementById('driverMonday')).value); } if (document.getElementById('driverTuesday')) { this.routeDefinition.tuesdayDriverId = Number((document.getElementById('driverTuesday')).value); } if (document.getElementById('driverWednesday')) { this.routeDefinition.wednesdayDriverId = Number((document.getElementById('driverWednesday')).value); } if (document.getElementById('driverThursday')) { this.routeDefinition.thursdayDriverId = Number((document.getElementById('driverThursday')).value); } if (document.getElementById('driverFriday')) { this.routeDefinition.fridayDriverId = Number((document.getElementById('driverFriday')).value); } if (document.getElementById('driverSaturday')) { this.routeDefinition.defaultUserId = Number((document.getElementById('driverSaturday')).value); this.routeDefinition.saturdayDriverId = Number((document.getElementById('driverSaturday')).value); /*this.routeDefinition.mondayDriverId = 0; this.routeDefinition.tuesdayDriverId = 0; this.routeDefinition.wednesdayDriverId = 0; this.routeDefinition.thursdayDriverId = 0; this.routeDefinition.fridayDriverId = 0; this.routeDefinition.sundayDriverId = 0;*/ } if (document.getElementById('driverSunday')) { this.routeDefinition.defaultUserId = Number((document.getElementById('driverSunday')).value); this.routeDefinition.sundayDriverId = Number((document.getElementById('driverSunday')).value); /* this.routeDefinition.mondayDriverId = 0; this.routeDefinition.tuesdayDriverId = 0; this.routeDefinition.wednesdayDriverId = 0; this.routeDefinition.thursdayDriverId = 0; this.routeDefinition.fridayDriverId = 0; this.routeDefinition.saturdayDriverId = 0;*/ } //managers if (document.getElementById('managerMonday') || document.getElementById('managerTuesday') || document.getElementById('managerWednesday') || document.getElementById('managerThursday') || document.getElementById('managerFriday')) { this.routeDefinition.saturdayManagerId = 0; this.routeDefinition.sundayManagerId = 0; } if (document.getElementById('managerMonday')) { this.routeDefinition.mondayManagerId = Number((document.getElementById('managerMonday')).value); } if (document.getElementById('managerTuesday')) { this.routeDefinition.tuesdayManagerId = Number((document.getElementById('managerTuesday')).value); } if (document.getElementById('managerWednesday')) { this.routeDefinition.wednesdayManagerId = Number((document.getElementById('managerWednesday')).value); } if (document.getElementById('managerThursday')) { this.routeDefinition.thursdayManagerId = Number((document.getElementById('managerThursday')).value); } if (document.getElementById('managerFriday')) { this.routeDefinition.fridayManagerId = Number((document.getElementById('managerFriday')).value); } if (document.getElementById('managerSaturday')) { this.routeDefinition.saturdayManagerId = Number((document.getElementById('managerSaturday')).value); /* this.routeDefinition.mondayManagerId = 0; this.routeDefinition.tuesdayManagerId = 0; this.routeDefinition.wednesdayManagerId = 0; this.routeDefinition.thursdayManagerId = 0; this.routeDefinition.fridayManagerId = 0; this.routeDefinition.sundayManagerId = 0;*/ } if (document.getElementById('managerSunday')) { this.routeDefinition.sundayManagerId = Number((document.getElementById('managerSunday')).value); /*this.routeDefinition.mondayManagerId = 0; this.routeDefinition.tuesdayManagerId = 0; this.routeDefinition.wednesdayManagerId = 0; this.routeDefinition.thursdayManagerId = 0; this.routeDefinition.fridayManagerId = 0; this.routeDefinition.saturdayManagerId = 0;*/ } this.routeDefinition.routeTemplateTypeId = 1; this.routeDefinition.routeTypeId = Number((document.getElementById('routeType')).value); //this.eta = moment(moment(this.routeDefinition.eta).toISOString()).toDate(); //this.routeDefinition.eta = moment.utc(moment(this.currentDateTime).format('YYYY-MM-DD') + ' ' + moment(this.eta).format('HH:mm:ss')); this.routeDefinition.eta = this.eta; //alert(moment(moment(this.currentDateTime).format('YYYY-MM-DD') + ' ' + moment(this.eta).format('HH:mm:ss'))); // let cutOffS = moment(moment(this.routeDefinition.cutOff).format('HH:mm:ss')); this.routeDefinition.cutOff = this.routeDefinition.cutOff; //console.log(this.supervisors); if(this.supervisors.length != 0) { //this.routeDefinition.supervisors = this.supervisors; for (var i = 0; i < this.supervisors.length; i++) { let a = new SupervisorDto({ isEmail: this.supervisors[i].isEmail, tenantId: null, isActive: this.supervisors[i].isActive, emailAddress: this.supervisors[i].emailAddress, code: this.supervisors[i].code, organizationUnitId: this.supervisors[i].organizationUnitId, userId: this.supervisors[i].userId, routeTemplateId: this.supervisors[i].routeTemplateId, id: this.supervisors[i].id, }); this.supervisorList.push(a); } this.routeDefinition.supervisors = this.supervisorList; } //console.log(this.routeDefinition); if (this.activeStatus == true) { this.routeDefinition.inactive = false; } else { this.routeDefinition.inactive = true; } if(this.routeDefinition.routeTypeId === 0) { this.saving = false; (document.getElementById('routeTypeDiv')).style.color="#fd397a"; } else if(this.routeDefinition.routeStartContactId === 0 && this.routeDefinition.routeStartInactive == false) { this.saving = false; (document.getElementById('routeStartDiv')).style.color="#FF3300"; } else if(this.routeDefinition.defaultContactId === 0) { this.saving = false; (document.getElementById('routeStopDiv')).style.color="#FF3300"; } else if(this.routeDefinition.locationId === 0) { this.saving = false; (document.getElementById('locationDiv')).style.color="#FF3300"; } else { this._routeDefinitionsServiceProxy.createOrEdit(this.routeDefinition) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } } openSelectRouteTypeModal() { this.routeDefinitionRouteTypeLookupTableModal.id = this.routeDefinition.routeTypeId; this.routeDefinitionRouteTypeLookupTableModal.displayName = this.routeTypeRouteTypeCode; this.routeDefinitionRouteTypeLookupTableModal.show(); } setRouteTypeIdNull() { this.routeDefinition.routeTypeId = null; this.routeTypeRouteTypeCode = ''; } setRouteTemplateIdNull() { this.routeDefinition.routeTemplateId = null; this.routeTemplateName = ''; } getNewRouteTypeId() { this.routeDefinition.routeTypeId = this.routeDefinitionRouteTypeLookupTableModal.id; this.routeTypeRouteTypeCode = this.routeDefinitionRouteTypeLookupTableModal.displayName; } getAllDropDown() { let s = this; jQuery(document).ready(function () { $('.kt-select2').select2(); $('.select2-container').css('width', '100%'); $('#routeType').on('select2:select', function (e) { s.setRouteType(this.value); }); $('.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.filterRouteManager(); let maxcount = 1000; // let that = this; this._contactServiceProxy.getContactAddress().subscribe(result => { this.contacts = result.items; }); this._routeStopsService.getRouteStopForDropDown().subscribe(result => { this.routeStops = result.items; }); this._driverService.getDriverList().subscribe((result) => { this.users = result; }); this.getManagers(); this._locationServiceProxy.getLocationNames(undefined, undefined, undefined, undefined, undefined, undefined,undefined, undefined, undefined) .subscribe(result => { this.locations = result; }); this._routeDefinitionsServiceProxy.getAllRouteTypeForLookupTable( undefined, undefined, undefined, maxcount ).subscribe(result => { this.routeTypes = result.items; }); } ngAfterViewInit(): void { $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); } updateEmail(id, data): void { this.supervisors[id].isEmail = data; console.log(this.supervisors); } deleteRecipient(id: number): void { this.delete = this.supervisors[id]; this._routeDefinitionsServiceProxy.deleteSupervisor(this.delete.id) .subscribe(() => { this.notify.success(this.l('SuccessfullyDeleted')); }); this.supervisors.splice(id, 1); } updateUnits(id, data): void { this.supervisors[id].organizationUnitId = data; console.log(this.supervisors); setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 100); } updateUsers(id, data): void { this.supervisors[id].userId = data; console.log(this.supervisors); setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 100); } updateRecipientType(id, data): void { let s = this; this.supervisors[id].code = data; console.log(this.supervisors); setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); $('.organization-unit').on('select2:select', function (e) { s.updateUnits($(e.currentTarget).attr('id'), $(e.currentTarget).val()); }); $('.user').on('select2:select', function (e) { s.updateUsers($(e.currentTarget).attr('id'), $(e.currentTarget).val()); }); }, 100); } addRecipient(): void { let s = this; this.addSupervisors = []; this.addSupervisors.isEmail = false; this.addSupervisors.isActive = false; this.addSupervisors.organizationUnitId = null; this.addSupervisors.emailAddress = ""; this.addSupervisors.code = ""; this.addSupervisors.userId = null; this.addSupervisors.routeTemplateId = null; this.supervisors.push(this.addSupervisors); //console.log(this.supervisors); //this.supervisors = this.addSupervisors; setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 10); } routeStartActiveToggle(status){ if(status == true){ this.routeDefinition.routeStartInactive = true; } if(status == false){ this.routeDefinition.routeStartInactive = false; } } setRouteType(e) { this._routeTypesServiceProxy.getAll( undefined, undefined, undefined, undefined, undefined, undefined, 1000).subscribe(data => { data.items.forEach(value => { if (value.routeType.id == e) { this.routeTypeRouteTypeCode = value.routeType.routeTypeCode; jQuery(document).ready(function () { $('.kt-select2').select2(); $('.select2-container').css('width', '100%'); }); } }); }); } editDriver() { this.activeTabIndex = 1; } getManagers(){ this._orderIncidentAppService.getManagers().subscribe(result => { this.managers = result; }) } close(): void { this.active = false; this.modal.hide(); } filterRouteManager(): void { this._routeDefinitionsServiceProxy.getManagerList().subscribe(result => { this.getManagerListDto = result; }); } validateForm(id): void{ this.routeDefinition.locationId = Number((document.getElementById('txtLocation')).value); this.routeDefinition.routeTypeId = Number((document.getElementById('routeType')).value); this.routeDefinition.defaultContactId = Number((document.getElementById('routeStop')).value); this.routeDefinition.routeStartContactId = Number((document.getElementById('routeStart')).value); /*if(this.eta == undefined) { this.saving = false; (document.getElementById('etaDiv')).style.color="#fd397a"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); }else{ (document.getElementById('etaDiv')).style.color="#646c9a"; } if(this.routeDefinition.cutOff == undefined) { this.saving = false; (document.getElementById('cutOffDiv')).style.color="#fd397a"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); }else{ (document.getElementById('cutOffDiv')).style.color="#646c9a"; }*/ if(this.routeDefinition.routeCode == undefined || this.routeDefinition.routeCode == "") { this.saving = false; (document.getElementById('routeCodeDiv')).style.color="#fd397a"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); }else{ (document.getElementById('routeCodeDiv')).style.color="#646c9a"; } if(this.routeDefinition.routeTypeId === 0) { this.saving = false; (document.getElementById('routeTypeDiv')).style.color="#fd397a"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); }else{ (document.getElementById('routeTypeDiv')).style.color="#646c9a"; } if((this.routeDefinition.routeStartContactId === 0 || this.routeDefinition.routeStartContactId == undefined) && this.routeDefinition.routeStartInactive === false) { this.saving = false; (document.getElementById('routeStartDiv')).style.color="#FF3300"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); } else{ (document.getElementById('routeStartDiv')).style.color="#646c9a"; } if(this.routeDefinition.defaultContactId === 0) { this.saving = false; (document.getElementById('routeStopDiv')).style.color="#FF3300"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); } else{ (document.getElementById('routeStopDiv')).style.color="#646c9a"; } if(this.routeDefinition.locationId === 0) { this.saving = false; (document.getElementById('locationDiv')).style.color="#FF3300"; this.staticTabs.tabs[1].active = false; this.staticTabs.tabs[2].active = false; this.staticTabs.tabs[3].active = false; this.message.warn('','Please fill in all required fields in Route Details Tab'); } else{ (document.getElementById('locationDiv')).style.color="#646c9a"; } if(this.routeDefinition.locationId !== 0 && this.routeDefinition.defaultContactId !== 0 && this.routeDefinition.routeTypeId !== 0 && (this.routeDefinition.routeCode == undefined || this.routeDefinition.routeCode == "")&& this.eta == undefined && this.routeDefinition.cutOff == undefined) { this.staticTabs.tabs[0].active = false; if(id==1){ this.staticTabs.tabs[1].active = true; } else if(id==2) { this.staticTabs.tabs[2].active = true; } else{ this.staticTabs.tabs[3].active = true; } } } }