import { Component, Injector, ViewChild, ViewEncapsulation, ElementRef, Directive, Input } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { CreateOrUpdateContactUserInput, DriversServiceProxy, LocationServiceProxy, CreateLocationInput, UpdateLocationInput, UserLocationServiceProxy, CreateUserLocationInput, UpdateUserLocationInput, PasswordComplexitySetting, UserRoleDto, UserEditDto, UpdateContactInput, UpdateAddressInput, CreateContactInput, ContactServiceProxy, AddressTypeServiceProxy, PostalCodeServiceProxy, EmailServiceProxy, PhoneServiceProxy, ProfileServiceProxy, AddressServiceProxy, CreateAddressInput, CreateOrUpdateDriverInfoInput, VehicleServiceProxy, CreatePostalCodeInput, GetDriverForEditOutput, GetEmployeeForEditOutput, CreateDriverInput, } from '@shared/service-proxies/service-proxies'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment-timezone'; import * as _ from 'lodash'; import { colorSets } from '@swimlane/ngx-charts/release/utils'; import { ContactFormComponent } from '@app/shared/layout/form/contact.form.component'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; import { Location } from '@angular/common'; declare var KTWizard: any; declare var $: any; declare var KTApp: any; declare let swal: any; @Component({ templateUrl: './driver-edit.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()] }) // @Directive({ // selector: '[var]', // exportAs: 'var' // }) export class DriverEditComponent extends AppComponentBase { @ViewChild(ContactFormComponent, { static: false }) contactForm; @ViewChild(AddressFormComponent, { static: false }) addressForm; @ViewChild("locationSelectInput", { static: false }) locationSelectInput: ElementRef; @Input() var: any; active = false; saving = false; id: any; driver: UserEditDto = new UserEditDto(); contact: UpdateContactInput = new UpdateContactInput(); address: UpdateAddressInput = new UpdateAddressInput(); addressInput: CreateAddressInput = new CreateAddressInput(); contactInput: CreateContactInput = new CreateContactInput(); driverInput: CreateOrUpdateDriverInfoInput = new CreateOrUpdateDriverInfoInput(); postalCodeInput: CreatePostalCodeInput = new CreatePostalCodeInput(); defaultLocation:any; selectedLocation: any; location: any; filteredLocations: any; defaultAddressType: any; defaultCountry: any; defaultState: any; defaultPostal: any; defaultCity: any; selectedAddressType: any; selectedCountry: any; selectedPostalCode: any; selectedState: any; selectedCity: any; fName: string; sName: string; pNumber: any; emailAddress: any; country: any; filteredCountries: any; city: any; filteredCities: any; state: any; filteredStates: any; postal: any; filteredPostal: any; addressType: any; filteredAddressType: any; canChangeUserName = true; isActive = true; skipOrders = false; // isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled'); isTwoFactorEnabled: boolean = false; isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled'); passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting(); roles: UserRoleDto[]; sendActivationEmail = false; shouldChangePasswordOnNextLogin = false; setRandomPassword = false; passwordComplexityInfo = ''; isContact = true; StartDate: moment.Moment = moment(); EndDate: moment.Moment = moment(); DriverLicenseDate: moment.Moment = moment(); driverInfoEmpty: boolean = false; todaysDate: moment.Moment = moment(); filteredVehicle: any; vDriver: GetDriverForEditOutput = new GetDriverForEditOutput(); _driver: CreateDriverInput = new CreateDriverInput(); postalCodeId: number; stateFeature: boolean; countrySelected: string; skipOrderFeature:boolean = false; constructor( injector: Injector, private _locationService: LocationServiceProxy, private _driverService: DriversServiceProxy, private _userLocationService: UserLocationServiceProxy, private _contactService: ContactServiceProxy, private _addressTypeService: AddressTypeServiceProxy, private _postalCodeService: PostalCodeServiceProxy, private _profileService: ProfileServiceProxy, private _addressService: AddressServiceProxy, private _router: Router, private route: ActivatedRoute, private _location: Location, private _vehicleService: VehicleServiceProxy, private _postalCodeServiceProxy: PostalCodeServiceProxy ) { super(injector); } setPasswordComplexityInfo(): void { this.passwordComplexityInfo = ''; } ngOnInit() { this.stateFeature = abp.features.isEnabled('App.StateFeature'); this.skipOrderFeature = abp.features.isEnabled('App.SkipOrders'); this.route.paramMap.subscribe(params => { this.id = params.get("id") }); this.filterVehicle(); this.filterAddressTypes(1000); // $('.kt-select2').select2(); this.validateForm(); $("#countrySelectInput").change(function () { this.selectedCountry = $("#countrySelectInput option:selected").text(); $("#selectedCountry").text(this.selectedCountry + ","); }); $("#locationSelectInput").change(function () { this.locationSelected = $("#locationSelectInput option:selected").text(); $("#locationSelected").text(this.locationSelected); }); $("#addressTypeSelectInput").change(function () { this.selectedAddressType = $("#addressTypeSelectInput option:selected").text(); $("#selectedAddressType").text(this.selectedAddressType); }); this.spinnerService.show(); this._driverService.getDriverForEdit(this.id).subscribe(result => { this.driver = result.user; this.fName = result.user.name; this.sName = result.user.surname; this.pNumber = result.user.phoneNumber; this.emailAddress = result.user.emailAddress; this.driver.userName = result.user.userName; this.driver.password = result.user.password; this.shouldChangePasswordOnNextLogin = result.user.shouldChangePasswordOnNextLogin; this.sendActivationEmail = result.user.sendActivationEmail; this.isActive = result.user.isActive; this.isLockoutEnabled = result.user.isLockoutEnabled; this.isTwoFactorEnabled = result.user.isTwoFactorEnabled; this.vDriver = result; if(result.contact==null) { this.selectedCountry = ""; this.selectedPostalCode = ""; this.selectedState = ""; this.selectedCity = ""; this.selectedAddressType = ""; this.contact.id = null; this.contact.addressId = null; this.postalCodeId = null; }else{ let that = this // this.contact.id = this.vDriver.contact.id; // this.contact.addressId = this.vDriver.contact.address.id; this.selectedCountry = this.vDriver.contact.address.postalCode.country; // this.selectedPostalCode = this.vEmployee.contact.address.postalCode.value; // this.selectedState = this.vEmployee.contact.address.postalCode.state; // this.selectedCity = this.vEmployee.contact.address.postalCode.city; this.addressForm.country = this.vDriver.contact.address.postalCode.country; // console.log(this.addressForm.country) this.addressForm.value = this.vDriver.contact.address.postalCode.value; this.addressForm.state = this.vDriver.contact.address.postalCode.state; this.addressForm.city = this.vDriver.contact.address.postalCode.city; this.addressForm.latitude = this.vDriver.contact.address.geocode == null ? '' : this.vDriver.contact.address.geocode.latitude; this.addressForm.longitude = this.vDriver.contact.address.geocode == null ? '' : this.vDriver.contact.address.geocode.longitude; this.addressForm.formattedAddress = this.vDriver.contact.address.formattedAddress; this.selectedAddressType = this.vDriver.contact.address.addressType ? this.vDriver.contact.address.addressType.name : ""; this.address.addressLine1 = this.vDriver.contact.address.addressLine1; this.address.addressLine2 = this.vDriver.contact.address.addressLine2; this.address.addressTypeId = this.vDriver.contact.address.addressType ? this.vDriver.contact.address.addressType.id : null; this.address.postalCodeId = this.vDriver.contact.address.postalCode.id; this.postalCodeId = this.vDriver.contact.address.postalCode.id; } if(this.vDriver.driverProfile!=null) { this.driverInfoEmpty = false; this.driverInput.startDate = this.vDriver.driverProfile.endDate ? moment.tz(this.vDriver.driverProfile.startDate, localStorage.getItem('timeZoneId')) : null; this.driverInput.endDate = this.vDriver.driverProfile.endDate ? moment.tz(this.vDriver.driverProfile.endDate, localStorage.getItem('timeZoneId')) : null; this.driverInput.driverLicenseExpirationDate = this.vDriver.driverProfile.driverLicenseExpirationDate ? moment.tz(this.vDriver.driverProfile.driverLicenseExpirationDate, localStorage.getItem('timeZoneId')) : null; this.driverInput.driverLicenseNumber = this.vDriver.driverProfile.driverLicenseNumber; this.driverInput.driverLicenseState = this.vDriver.driverProfile.driverLicenseState; this.driverInput.dailyRate = this.vDriver.driverProfile.dailyRate; this.driverInput.hourlyRate = this.vDriver.driverProfile.hourlyRate; this.driverInput.perSprintRate = this.vDriver.driverProfile.perSprintRate; this.driverInput.isActive = this.vDriver.driverProfile.isActive; this.driverInput.isActive = this.vDriver.driverProfile.isActive; this.driverInput.ownVehicle = this.vDriver.driverProfile.ownVehicle; this.driverInput.onDemand = this.vDriver.driverProfile.onDemand; this.driverInput.vehicleId = this.vDriver.driverProfile.vehicleId; this.skipOrders = this.vDriver.driverProfile.skipOrders; }else{ this.driverInfoEmpty = true; } let values = []; result.locationId.forEach(el => { values.push(el); }); this.defaultLocation = values; // console.log(this.driverInput.vehicleId) // console.log(this.defaultLocation); this.filterLocations(1000); this.spinnerService.hide(); }); setTimeout(function(){ $('.kt-select2').select2(); this.locationSelected = $("#locationSelectInput option:selected").text(); $("#locationSelected").text(this.locationSelected); this.countrySelected = $("#countrySelectInput option:selected").text(); $("#selectedCountry").text(this.countrySelected + ","); // console.log('trigerred') }, 1500); } ngAfterViewInit() { let maxcount = 1000; // this.filterPostal(maxcount); var that = this; // setTimeout(function(){ // $('.kt-select2').select2(); // this.locationSelected = $("#locationSelectInput option:selected").text(); // $("#locationSelected").text(this.locationSelected); // this.countrySelected = $("#countrySelectInput option:selected").text(); // $("#selectedCountry").text(this.countrySelected + ","); // // console.log('trigerred') // }, 1500); } trackLocation(index: number, item: any): string { // console.log(item.id); // if(item.id===1){ // return item.id; // }else{ // return item.name; // } // if(!item) return null; return item.id; } save(): void { this.saving = true; let timeId = localStorage.getItem('timeZoneId'); if (this.contactForm.pNumber == null) { this.contactForm.pNumber = "n/a"; } this._driver.user.id = this.id; this._driver.user.name = this.contactForm.fName; this._driver.user.surname = this.contactForm.sName; this._driver.user.userName = this.contactForm.fName; this._driver.user.userName = this.driver.userName; this._driver.user.password = this.driver.password; this._driver.user.phoneNumber = this.contactForm.pNumber; this._driver.user.emailAddress = this.contactForm.emailAddress; this._driver.user.isLockoutEnabled = this.isLockoutEnabled; this._driver.user.isActive = this.isActive; this._driver.skipOrders = this.skipOrders; this._driver.user.isTwoFactorEnabled = this.isTwoFactorEnabled; this._driver.user.shouldChangePasswordOnNextLogin = this.shouldChangePasswordOnNextLogin; this._driver.user.sendActivationEmail = this.sendActivationEmail; this._driver.setRandomPassword = this.setRandomPassword; this._driver.sendActivationEmail = this.sendActivationEmail; this._driver.location = $("#locationSelectInput").val(); this._driver.address = this.addressForm.address; if(this.vDriver.contact!=null){ this._driver.address.id = this.vDriver.contact.address.id; this._driver.postalCodeId = this.postalCodeId; } this._driver.latitude = this.addressForm.latitude == '' || this.addressForm.latitude === undefined || this.addressForm.latitude == null ? 0 : this.addressForm.latitude; this._driver.longitude = this.addressForm.longitude == '' || this.addressForm.longitude === undefined || this.addressForm.longitude == null ? 0 : this.addressForm.longitude; this._driver.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value) == 0 ? null : Number((document.getElementById('addressTypeSelectInput')).value); this._driver.address.formattedAddress = this.addressForm.formattedAddress; this.contactInput.lastName = this.contactForm.sName; this.contactInput.firstName = this.contactForm.fName; this._driver.contact = this.contactInput; this._driver.contactId = this.vDriver.contact==null ? null : this.vDriver.contact.id; this.postalCodeInput.city = this.addressForm.city; this.postalCodeInput.country = this.addressForm.country; if(!this.stateFeature){ this.postalCodeInput.state = ""; } else{ this.postalCodeInput.state = this.addressForm.state; } this.postalCodeInput.value = this.addressForm.value; //this.postalCodeInput.country = $('#countrySelectInput').val(); this._driver.postalCode = this.postalCodeInput; this._driver.driverInfo = this.driverInput; this._driver.driverInfo.startDate = this.driverInput.startDate ? moment.tz(this.driverInput.startDate, timeId) : null; this._driver.driverInfo.endDate = this.driverInput.endDate ? moment.tz(this.driverInput.endDate, timeId) : null; this._driver.driverInfo.driverLicenseExpirationDate = this.driverInput.driverLicenseExpirationDate ? moment.tz(this.driverInput.driverLicenseExpirationDate, timeId) : null; this.driverInput.vehicleId = Number((document.getElementById('vehicleName')).value) ? Number((document.getElementById('vehicleName')).value) : null; if(this.postalCodeInput.city == null && this.postalCodeInput.country == null && this.postalCodeInput.state == null&& this.postalCodeInput.value == null){ this.saving = false this.message.warn( // "Please Add Address" this.l("AddressValidation") ); } else if(this._driver.location == null || this._driver.location.length <= 0){ this.saving = false this.message.warn( // "Please Add Location" this.l("LocationValidation") ); } else{ this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) .pipe() .subscribe(result => { this._driver.postalCodeId = result this._driverService.createOrUpdateDriverAPI(this._driver) .pipe(finalize(()=>{ this.saving=false })) .subscribe( () => { this.notify.info(this.l('SavedSuccessfully')); this._router.navigate(['/app/sprintship/drivers']); }, error => { this.saving = false }); }, error => { this.saving = false }); } // this.saving = true; // let userInput = new CreateOrUpdateContactUserInput({ // user: new UserEditDto({ // id: this.driver.id, // name: this.contactForm.fName, // surname: this.contactForm.sName, // userName: this.driver.userName, // password: this.driver.password, // phoneNumber: this.contactForm.pNumber, // emailAddress: this.contactForm.emailAddress, // isLockoutEnabled: this.isLockoutEnabled, // isActive: this.isActive, // isTwoFactorEnabled: this.isTwoFactorEnabled, // shouldChangePasswordOnNextLogin: this.shouldChangePasswordOnNextLogin, // sendActivationEmail: this.sendActivationEmail // }), // assignedRoleNames: _.map( // _.filter(this.roles, { isAssigned: true }), role => role.roleName // ), // sendActivationEmail: this.sendActivationEmail, // setRandomPassword: this.setRandomPassword, // location: $("#locationSelectInput").val() // }); // // this.addressInput.postalCodeId = Number((document.getElementById('postalSelectInput')).value); // // console.log("Address"); // // console.log(this.addressForm.address.addressLine1); // // console.log(this.addressForm.address); // // console.log("Address Input"); // // console.log(this.addressInput.postalCodeId); // // console.log(this.addressInput); // // console.log("Driver Input"); // // console.log(this.driverInput.startDate); // // console.log(this.driverInput.hourlyRate); // // console.log(this.driverInput.dailyRate); // // console.log("Driver Location"); // // console.log($("#locationSelectInput").val() + "Driver LOcation"); // if (this.addressForm.address.addressLine1 == null && this.addressInput.postalCodeId == 0) { // // // // this.saving = false; // this._driverService.createOrUpdateDriver(userInput).subscribe(success => { // this.saving = false; // this.notify.info(this.l('SavedSuccessfully')); // this.router.navigate(['/app/sprintship/drivers']); // }); // } else { // this._driverService.createOrUpdateDriver(userInput).subscribe(success => { // if (this.contact.id == null) { // this.addressInput = this.addressForm.address // // this.addressInput.postalCodeId = Number((document.getElementById('postalSelectInput')).value); // if (Number((document.getElementById('addressTypeSelectInput')).value) == 0) { // this.addressInput.addressTypeId = null; // } else { // this.addressInput.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value); // } // this.postalCodeInput.city = this.addressForm.city; // this.postalCodeInput.state = this.addressForm.state; // this.postalCodeInput.value = this.addressForm.value; // this.postalCodeInput.country = this.addressForm.country // this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) // .pipe() // .subscribe(result => { // this.addressInput.postalCodeId = result // this._addressService.createAddress(this.addressInput) // .subscribe( // addressResult => { // this.contactInput.userId = success; // this.contactInput.addressId = addressResult; // this.contactInput.firstName = this.contactForm.fName; // this.contactInput.lastName = this.contactForm.sName; // this._contactService.createContact(this.contactInput) // .pipe(finalize(() => { // this.saving = false; // })) // .subscribe(contactId => { // this.driverInput.userId = this.id; // this.driverInput.contactId = contactId; // this.driverInput.vehicleId = Number((document.getElementById('vehicleName')).value) ? Number((document.getElementById('vehicleName')).value) : null // this._driverService.getDriverProfile(this.id).subscribe(driverProfile => { // if (driverProfile.userId) { // this._driverService.updateDriverInfo(this.driverInput).subscribe(driverProfile => { console.log("Update"); }); // } else { // this._driverService.createDriverInfo(this.driverInput).subscribe(driverProfile => { console.log("CREATE"); }); // } // this.notify.info(this.l('SavedSuccessfully')); // this.router.navigate(['/app/sprintship/drivers']); // }); // this.saving = false; // }); // }); // }); // } else { // this.address = this.addressForm.address // // this.address.postalCodeId = Number((document.getElementById('postalSelectInput')).value); // if (Number((document.getElementById('addressTypeSelectInput')).value) == 0) { // this.address.addressTypeId = null; // } else { // this.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value); // } // // console.log(this.address); // this.postalCodeInput.city = this.addressForm.city; // this.postalCodeInput.state = this.addressForm.state; // this.postalCodeInput.value = this.addressForm.value; // this.postalCodeInput.country = this.addressForm.country // this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) // .pipe() // .subscribe(result => { // this.address.postalCodeId = result // this._addressService.updateAddress(this.address) // .subscribe( // addressResult => { // this.contact.addressId = this.address.id; // this.contact.firstName = this.driver.name; // this.contact.lastName = this.driver.surname; // this.contact.userId = success; // this._contactService.updateContact(this.contact) // .pipe(finalize(() => { // this.saving = false; // })) // .subscribe(() => { // this.driverInput.userId = this.id; // this.driverInput.contactId = this.contact.id; // this.driverInput.vehicleId = Number((document.getElementById('vehicleName')).value) ? Number((document.getElementById('vehicleName')).value) : null // this._driverService.getDriverProfile(this.id).subscribe(driverProfile => { // if (driverProfile.userId) { // this._driverService.updateDriverInfo(this.driverInput).subscribe(driverProfile => { }); // } else { // this._driverService.createDriverInfo(this.driverInput).subscribe(driverProfile => { }); // } // this.notify.info(this.l('SavedSuccessfully')); // this.router.navigate(['/app/sprintship/drivers']); // }); // this.saving = false; // }); // }); // }); // } // }); // } } filterLocations(maxcount): void { this._locationService.getLocationNames( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { let key, key2, arrAny = {}, arrHolder = []; for (key in result) { arrAny = { 'name': result[key].name, 'id': result[key].id, 'selected': false }; for (key2 in this.defaultLocation) { if (this.defaultLocation[key2] == result[key].id) { arrAny = { 'name': result[key].name, 'id': result[key].id, 'selected': true }; } } arrHolder.push(arrAny); } this.filteredLocations = arrHolder; }); } // filterPostal(maxCount): void { // this._postalCodeService.getPostalCode( // undefined, // undefined, // undefined, // maxCount, // undefined // ).subscribe(result => { // this.filteredPostal = result.items; // }); // } filterAddressTypes(maxCount): void { this._addressTypeService.getAddressType( undefined, undefined, undefined, undefined, maxCount, undefined ).subscribe(result => { this.filteredAddressType = result.items; }); } // Class definition KTWizard2(): void { // Base elements var wizardEl; var formEl; var validator; var wizard; // Private functions var initWizard = function () { // Initialize form wizard wizard = new KTWizard('kt_wizard_v2', { startStep: 1, }); // Validation before going to next page wizard.on('beforeNext', function (wizardObj) { if (wizardObj.currentStep == 1) { if ($('#Name').val() == '' || ($('#Surname').val() == '') // || ($('#PhoneNumber').val() == ''|| $('#minutesPerStopInput').val()==0) || $('#EmailAddress').val() == '' || $('#UserName').val() == '' ) { wizardObj.stop(); // don't go to the next step } } else if (wizardObj.currentStep == 2) { if ($('#locationSelectInput').val() == null || $('#locationSelectInput').val() == 0 // || $('#address1Input').val() == '' // || ($('#countrySelectInput').val() == null || $('#countrySelectInput').val() == 0) // || ($('#postalSelectInput').val() == null || $('#postalSelectInput').val() == 0) // || ($('#stateSelectInput').val() == null || $('#stateSelectInput').val() == 0) // || ($('#citySelectInput').val() == null || $('#citySelectInput').val() == 0) // || ($('#locationSelectInput').val() == null || $('#locationSelectInput').val() == 0) // || ($('#addressTypeSelectInput').val() == null || $('#addressTypeSelectInput').val() == 0) ) { wizardObj.stop(); // don't go to the next step } } }); // Change event wizard.on('change', function (wizard) { KTUtil.scrollTop(); }); } wizardEl = KTUtil.get('kt_wizard_v2'); formEl = $('#kt_form').serialize(); return initWizard(); //return initValidation(); //return initSubmit(); } goBack() { this._location.back(); } filterVehicle(): void { this._driverService.getAvailableVehicle( undefined, undefined, undefined, this.id, undefined, undefined, undefined ).subscribe(result => { this.filteredVehicle = result; }); } validateForm(): void { 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: { Name: { required: !0 }, Surname: { required: !0 }, EmailAddress: { required: !0, email: !0 }, UserName: { required: !0 }, locationSelectInput: { required: !0 }, Address1: { required: !0 }, postalSelectInput: { required: !0 }, countrySelectInput: { required: !0 }, stateSelectInput: { required: !0 }, citySelectInput: { required: !0 } }, invalidHandler: function (e, r) { }, submitHandler: function (e) { } }) } } }(); jQuery(document).ready(function () { KTWizard2.init(); }); } }