import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { AppConsts } from '@shared/AppConsts'; import { Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { EntityDtoOfInt64, DriversServiceProxy, UserListDto, UserServiceProxy, GetUserIdInputForResetPassword, DriverClientServiceProxy, LoginUnlockByIdInputDto, GetDriverListDto, LocationServiceProxy, FleetServiceProxy } from '@shared/service-proxies/service-proxies'; import { FileDownloadService } from '@shared/utils/file-download.service'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from 'primeng/components/table/table'; import { CreateDriverComponent } from './create-driver.component'; import { HttpClient } from '@angular/common/http'; import { FileUpload } from 'primeng/fileupload'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment-timezone'; import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component'; import * as _ from 'lodash'; @Component({ templateUrl: './drivers.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./drivers.component.less'], animations: [appModuleAnimation()] }) export class DriversComponent extends AppComponentBase { @ViewChild('dataTable', {static: true}) dataTable: Table; @ViewChild('paginator', {static: true}) paginator: Paginator; @ViewChild('entityTypeHistoryModal', { static: true }) entityTypeHistoryModal: EntityTypeHistoryModalComponent; _entityTypeFullName = 'SprintTek.Authorization.Users.User'; entityHistoryEnabled = false; //Filters filters : any; nameFilter : string surnameFilter : string emailAddressFilter : string emailCompletedFilter : boolean = false; advancedFiltersAreShown = false; saving : boolean completed: boolean = false; uploadUrl: string; onlyLockedUsers: boolean =false; isEmailConfirmed: boolean =false; utcNow: Date = moment.utc().toDate(); filteredLocations: any; locationIds: any; fleets : any; fleetIds :any; constructor( injector: Injector, private _driverService: DriversServiceProxy, private _fileDownloadService: FileDownloadService, private _activatedRoute: ActivatedRoute, private _httpClient: HttpClient, private _router: Router, private _userServiceProxy: UserServiceProxy, private _driverClientServiceProxy: DriverClientServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _fleetServiceProxy: FleetServiceProxy, private router: Router, ) { super(injector); this.uploadUrl = AppConsts.remoteServiceBaseUrl + '/Import/ImportFromExcel'; } ngOnInit(){ $('.kt-select2').select2({ placeholder: "Select.." }); this.setIsEntityHistoryEnabled(); this.filterLocations(); this.filterFleets(); } private setIsEntityHistoryEnabled(): void { let customSettings = (abp as any).custom; this.entityHistoryEnabled = customSettings.EntityHistory && customSettings.EntityHistory.isEnabled && _.filter(customSettings.EntityHistory.enabledEntities, entityType => entityType === this._entityTypeFullName).length === 1; } showHistory(role: GetDriverListDto): void { this.entityTypeHistoryModal.show({ entityId: role.id.toString(), entityTypeFullName: this._entityTypeFullName, entityTypeDescription: "Driver - " +role.name +"" +role.surname }); } exportToExcel(event?: LazyLoadEvent): void { this.locationIds = $("#locationFilter").val(); this.fleetIds = $("#driverFleetFilter").val(); this.spinnerService.show(); this._driverService.getDriversToExcel( this.filters || this.nameFilter || this.surnameFilter || this.emailAddressFilter, undefined, undefined, undefined, this.emailCompletedFilter, this.completed, this.onlyLockedUsers, this.locationIds, this.fleetIds, this.primengTableHelper.getSorting(this.dataTable), this.primengTableHelper.getMaxResultCount(this.paginator, event), this.primengTableHelper.getSkipCount(this.paginator, event) ).subscribe(result => { if(result!=undefined){ if(result.fileUrl!=undefined){ this.router.navigate([]).then(result1 => { window.open(result.fileUrl, '_blank'); }); this.spinnerService.hide(); }else{ this.spinnerService.hide(); } }else{ this.message.warn('', "No data to export"); this.spinnerService.hide(); } }) } getContact(event?: LazyLoadEvent) { this.spinnerService.show(); if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } this.spinnerService.show(); this.locationIds = $("#locationFilter").val(); this.fleetIds = $("#driverFleetFilter").val(); this._driverService.getDrivers( this.filters || this.nameFilter || this.surnameFilter || this.emailAddressFilter, undefined, undefined, undefined, this.emailCompletedFilter, this.completed, this.onlyLockedUsers, this.locationIds, this.fleetIds, this.primengTableHelper.getSorting(this.dataTable), this.primengTableHelper.getMaxResultCount(this.paginator, event), this.primengTableHelper.getSkipCount(this.paginator, event) ).subscribe(result => { this.primengTableHelper.totalRecordsCount = result.totalCount; this.primengTableHelper.records = result.items; console.log(result.items); this.spinnerService.hide(); }); } reloadPage(): void { this.paginator.changePage(this.paginator.getPage()); } deleteContact(driver: UserListDto): void { this.message.confirm( this.l('DriverDeleteWarningMessage', driver.name+" "+driver.surname), this.l('AreYouSure'), (isConfirmed) => { if (isConfirmed) { this._driverService.deleteDriver(driver.id) .subscribe(() => { this.reloadPage(); this.notify.success(this.l('SuccessfullyDeleted')); }); } } ); } createOrEditDriver(url: any, id?: number){ if(id != null){ var myurl = `${url}/${id}`; }else{ var myurl = `${url}`; } this._router.navigateByUrl(myurl); } uploadExcel(data: { files: File }): void { //this.importRouteScheduleTracking.clearMessages(); // this.importRouteScheduleTracking.modal.show(); const formData: FormData = new FormData(); const file = data.files[0]; formData.append('file', file, file.name); formData.append('key','driver'); // formData.append('entity','addresses','addresses'); this._httpClient .post(this.uploadUrl, formData) .subscribe(response => { if (response.success) { this.notify.success(this.l('ImportStart')); } else if (response.error != null) { this.notify.error(this.l('ImportFailed')); } }); } unlockUser(record): void { this._driverService.unlockDriver(new EntityDtoOfInt64({ id: record.id })).subscribe(() => { this.notify.success(this.l('UnlockedTheUser', record.name +" " +record.surname)); }); this.filters = "" ; this.nameFilter = ""; this.surnameFilter = ""; this.emailAddressFilter = ""; var that = this; setTimeout(function () { that.getContact() }, 1000) } lockUser(record): void { this._driverService.lockDriver(new EntityDtoOfInt64({ id: record.id })).subscribe(() => { this.notify.success(this.l('LockedUser')+ " " +record.name +" " +record.surname); this.filters = "" ; this.nameFilter = ""; this.surnameFilter = ""; this.emailAddressFilter = ""; var that = this; setTimeout(function () { that.getContact() }, 500) }); } setLocked(record):void { if(record.isLocked){ this.unlockUser(record); }else{ this.lockUser(record); } } resetPassword(userId){ let input = new GetUserIdInputForResetPassword input.userId = userId var that = this; this.spinnerService.show this._driverService.updateResetUserPassword(input) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('ResetPasswordSuccess')); this.spinnerService.hide this.filters = "" ; this.nameFilter = ""; this.surnameFilter = ""; this.emailAddressFilter = ""; setTimeout(function () { that.getContact() }, 200) }); } setEmailConfirm(record): void { this._driverService.driverEmailConfirm(new EntityDtoOfInt64({ id: record.id })).subscribe(() => { this.notify.success(this.l('EmailConfirm', record.name +" " +record.surname)); }); this.filters = "" ; this.nameFilter = ""; this.surnameFilter = ""; this.emailAddressFilter = ""; var that = this; setTimeout(function () { that.getContact() }, 1000) } driverAppUnlock(record): void { console.log('username=' + record.id); this._driverClientServiceProxy.loginUnlockById(new LoginUnlockByIdInputDto({ "userId": record.id })) .subscribe(result => { this.notify.success(this.l('DriverAppUnlockConfirm') + record.name + " " + record.surname); }); } filterLocations(): void { this._locationServiceProxy.getLocationNames( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredLocations = result; }); } filterFleets(): void { this._fleetServiceProxy.getFleets() .subscribe(result => { this.fleets = result }) } }