import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { RouteOptimizersServiceProxy, RouteOptimizerDto, CreateOrEditRouteOptimizerDto, RouteOptimizerLocationLookupTableDto, CreateOrEditRouteOptimizerRuleDto, OrderStatusTypeServiceProxy, OrderStatusTypeListDto, OrderTypesServiceProxy, LocationServiceProxy, LocationTableListDto } from '@shared/service-proxies/service-proxies'; import { NotifyService } from '@abp/notify/notify.service'; import { AppComponentBase } from '@shared/common/app-component-base'; import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { Table } from 'primeng/components/table/table'; import { Paginator } from 'primeng/components/paginator/paginator'; import {Location} from '@angular/common'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { FileDownloadService } from '@shared/utils/file-download.service'; import * as _ from 'lodash'; import * as moment from 'moment'; import { subscribeOn } from 'rxjs/operators'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; declare var KTWizard: any; declare var $: any; declare let swal: any; @Component({ templateUrl: './create-update-route-optimizer.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()] }) export class CreateUpdateRouteOptimizersComponent extends AppComponentBase { @ViewChild('dataTable', { static: true }) dataTable: Table; @ViewChild('paginator', { static: true }) paginator: Paginator; @ViewChild('locationList', {static: true}) locationList: CommonLookupModalComponent; advancedFiltersAreShown = false; filterText = ''; nameFilter = ''; trafficFilter = -1; highwaysFilter = -1; ferriesFilter = -1; indoorFilter = -1; activeFilter = -1; maxStartPeriodFilter : moment.Moment; minStartPeriodFilter : moment.Moment; maxEndPeriodFilter : moment.Moment; minEndPeriodFilter : moment.Moment; locationNameFilter = ''; id: any; optimizerData: CreateOrEditRouteOptimizerDto = new CreateOrEditRouteOptimizerDto(); allLocations: RouteOptimizerLocationLookupTableDto[]; rulesToSave: Array; ruleType: number; locationId: number; sequence: number; distance: number; orderSequence: number; time: any; orderPosition: number; optimizersRulesArray: CreateOrEditRouteOptimizerRuleDto[] = new Array(); status: OrderStatusTypeListDto[] = new Array(); statusS: any; orderType: any; orderTypeS: any; startDate: any = new Date();; endDate: any; saving = false; useGoogle: boolean = true; locations: LocationTableListDto[] = []; laboratoryMapFeatureEdition: boolean = false; textLocation constructor( injector: Injector, private _routeOptimizersServiceProxy: RouteOptimizersServiceProxy, private _tokenAuth: TokenAuthServiceProxy, private _activatedRoute: ActivatedRoute, private route: ActivatedRoute, private _location: Location, private _orderStatusType: OrderStatusTypeServiceProxy, private _orderType: OrderTypesServiceProxy, private router: Router, private _locationService: LocationServiceProxy ) { super(injector); } ngOnInit(): void { this.laboratoryMapFeatureEdition = abp.features.isEnabled('App.LaboratoryMapFilter'); if(this.laboratoryMapFeatureEdition){ this.useGoogle = true; } this.initLocationField(); this.locationListModalInit(); this.getAllOrderType(); this. getAllStatus(); this.ruleType = 1; $('.kt-select2').select2(); this.route.paramMap.subscribe(params => { this.id = params.get("id") }); if(this.id != '' && this.id != undefined && this.id != null){ this._routeOptimizersServiceProxy.getRouteOptimizerForEdit(this.id) .subscribe(result => { this.optimizerData = result; this.optimizersRulesArray = result.optimizersRules; this.optimizerData.optimizersRules = result.optimizersRules; this.optimizerData.optimizersRules.forEach(x => { if(x.useGoogle){ x.useGoogle = true }else{ x.useGoogle = false } }) this.startDate = result.startPeriod.toDate(); this.locationId = result.locationId; if(result.indefinite == true){ this.endDate = undefined; }else{ this.endDate = result.endPeriod.toDate(); } this.initLocationField(); }) }else{ //this.optimizerData.locationId = this.allLocations[0].id; } var KTWizard2=function(){ var e,r,i; return{init:function(){var t;KTUtil.get("kt_wizard_v2"),e=$("#kt_form"), (i=new KTWizard("kt_wizard_v2", {startStep:1,clickableSteps:!0})).on("beforeNext",function(e){!0!==r.form()&&e.stop()}), i.on("beforePrev",function(e){!0!==r.form()&&e.stop()}),i.on("change",function(e){KTUtil.scrollTop()}), r=e.validate({ignore:":hidden", rules:{}, invalidHandler:function(e,r){KTUtil.scrollTop(),swal.fire({title:"",text:"There are some errors in your submission. Please correct them.", type:"error",confirmButtonClass:"btn btn-secondary"})}, submitHandler:function(e){}})}}}(); jQuery(document).ready(function(){KTWizard2.init()}); let that = this; $("#locationId").change(function(){ that.optimizerData.locationId = Number((document.getElementById('locationId')).value); }); $("#ruleType").change(function(){ that.ruleType = Number((document.getElementById('ruleType')).value); }); } ngAfterViewInit():void { } goBack(){ this._location.back(); } selectLocation(item){ var resultId = item.value var name = item.name this.locationId = resultId; this.textLocation = name; } getLocation(filter : string){ this.locationList.show(); this.locationList.filterText = filter; } addField(){ console.log(this.ruleType); let rule = new CreateOrEditRouteOptimizerRuleDto; rule.ruleType = this.ruleType; if(this.ruleType == 5){ rule.orderStatusTypeId = this.statusS; } if(this.ruleType != 5){ rule.orderTypeId = this.orderTypeS; } rule.useGoogle = this.useGoogle; this.optimizersRulesArray.push(rule); this.optimizerData.optimizersRules = this.optimizersRulesArray; } getAllOrderType(){ this._orderType.getAll( undefined, undefined, undefined, -1, undefined, undefined, undefined, 100) .subscribe(result =>{ this.orderType = result.items; }); } remove(r: number){ this.optimizerData.optimizersRules.splice(r, 1); } statusName(value: number): string { if(value!=undefined){ let s = this.status.find(s => s.id == value); return s.description; } } typeName(value?: number): string { if(value!=undefined){ let s = this.orderType.find(s => s.orderType.id == value); return s.orderType.description; } } getAllStatus(){ this._orderStatusType.getOrderStatusTypeAll().subscribe(result =>{ this.status = result.items; }); } // locationName (value?: number): string { // if(value != undefined){ // let s = this.allLocations.find(s => s.id == value); // return s.displayName; // } // } clearEndPeriod(){ this.endDate = undefined; } save(){ this.saving = true; this.optimizerData.locationId = this.locationId; this.spinnerService.show(); this.optimizerData.startPeriod = this.startDate; if(this.endDate == undefined){ this.optimizerData.indefinite = true; }else{ this.optimizerData.indefinite = false; } this.optimizerData.endPeriod = this.endDate; this._routeOptimizersServiceProxy.createOrEdit(this.optimizerData) .subscribe(result=>{ this.saving = false; this.notify.info(this.l('SavedSuccessfully')); this.spinnerService.hide(); this.router.navigate(['/app/sprintship/route-optimizer']); }, (error) => { this.spinnerService.hide(); this.saving = false; }); } locationListModalInit(){ this.locationList.configure({ title: this.l('Select location'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?:number) => { let input = {filter,maxResultCount,skipCount}; input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._locationService.getPagedLocation( input.maxResultCount,input.skipCount,input.filter); } }); } clearLocation(){ this.locationId = null; this.textLocation = ''; } initLocationField(): void { this._locationService.getLocationTableOnly(this.locationId, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { this.locations = result; this.textLocation = this.locationId != null? this.locations.filter(c => c.id == this.locationId)[0].name : ''; }); } }