import { Component, Injector, ViewChild, ViewEncapsulation, ɵConsole } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { AddressServiceProxy, ContactServiceProxy, UpdateContactInput, UpdateAddressInput,
PostalCodeServiceProxy, VendorsServiceProxy,
PasswordComplexitySetting,UserEditDto,UserRoleDto,
CreateOrUpdateContactUserInput, AddressTypeServiceProxy, CreatePostalCodeInput, UserLocationServiceProxy, LocationServiceProxy, FleetsServiceProxy, CreateContactInput, GetVendorForEditOutput, CreateOrEditVendorInput, CompanyServiceProxy} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import * as _ from 'lodash';
import {Location} from '@angular/common';
import { ContactFormComponent } from '@app/shared/layout/form/contact.form.component';
import { AddressFormComponent } from '@app/shared/layout/form/address-form.component';
import { BookmarkCollectionBaseApi } from 'devexpress-richedit/lib/base-api/collections/bookmark-collection';
import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component';
declare var KTWizard: any;
declare var $: any;
declare var KTApp: any;
declare let swal: any;
@Component({
templateUrl: './edit-vendor.component.html',
encapsulation: ViewEncapsulation.None,
animations: [appModuleAnimation()]
})
export class EditVendorComponent extends AppComponentBase {
@ViewChild(ContactFormComponent, { static: false }) contactForm;
@ViewChild(AddressFormComponent, { static: false }) addressForm;
postalCodeInput: CreatePostalCodeInput = new CreatePostalCodeInput();
@ViewChild('companiesList', { static: true }) companiesList: CommonLookupModalComponent;
setRandomPassword = true;
userPasswordRepeat = '';
passwordComplexityInfo = '';
active = false;
saving = false;
id: number = null;
contactInput: CreateContactInput = new CreateContactInput();
vendor: GetVendorForEditOutput = new GetVendorForEditOutput();
vendorInput: CreateOrEditVendorInput = new CreateOrEditVendorInput();
address: UpdateAddressInput = new UpdateAddressInput();
filteredLocations: any;
getUserLocationId: any;
getAddressId: any;
getLocationId: any;
selectedAddressType: any;
selectedCountry: any;
selectedPostalCode: any;
selectedState: any;
selectedCity: any;
filteredAddressType: any;
canChangeUserName = true;
isActive = true;
passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();
sendActivationEmail = true;
shouldChangePasswordOnNextLogin = true;
firstName: string;
surName: string;
email: string;
phoneNumber: string;
userName: string;
password: string;
postalCodeId: number;
locationSelected: any;
isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
stateFeature: boolean;
countrySelected: string;
fleets: any;
fleetId: number;
fleetSelected: any;
// IdNumber: string;
// StartDate: moment.Moment = moment();
// HourlyRate: number=0.0;
// DailyRate: number=0.0;
constructor(
injector: Injector,
private _locationService: LocationServiceProxy,
private _addressTypeService: AddressTypeServiceProxy,
private router: Router,
private route: ActivatedRoute,
private _location: Location,
private _vendorServiceProxy: VendorsServiceProxy,
private _postalCodeServiceProxy: PostalCodeServiceProxy,
private _fleetService: FleetsServiceProxy,
) {
super(injector);
}
setPasswordComplexityInfo(): void {
this.passwordComplexityInfo = '
';
if (this.passwordComplexitySetting.requireDigit) {
this.passwordComplexityInfo += '- ' + this.l('PasswordComplexity_RequireDigit_Hint') + '
';
}
if (this.passwordComplexitySetting.requireLowercase) {
this.passwordComplexityInfo += '- ' + this.l('PasswordComplexity_RequireLowercase_Hint') + '
';
}
if (this.passwordComplexitySetting.requireUppercase) {
this.passwordComplexityInfo += '- ' + this.l('PasswordComplexity_RequireUppercase_Hint') + '
';
}
if (this.passwordComplexitySetting.requireNonAlphanumeric) {
this.passwordComplexityInfo += '- ' + this.l('PasswordComplexity_RequireNonAlphanumeric_Hint') + '
';
}
if (this.passwordComplexitySetting.requiredLength) {
this.passwordComplexityInfo += '- ' + this.l('PasswordComplexity_RequiredLength_Hint', this.passwordComplexitySetting.requiredLength) + '
';
}
this.passwordComplexityInfo += '
';
}
ngOnInit() {
this.stateFeature = abp.features.isEnabled('App.StateFeature');
$('.kt-select2').select2({
placeholder: "Select.."
});
this.route.paramMap.subscribe(params => {
this.id = Number(params.get("id"))
});
$("#FleetsVendorEdit").change(function () {
this.fleetSelected = $("#FleetsVendorEdit option:selected").text();
$("#fleetSelectedEdit").text(this.fleetSelected);
});
this.initial();
this.getvendorForEdit();
this.locationSelected = $("#locationSelectInput option:selected").text();
// this.selectedCountry = $("#countrySelectInput option:selected").text();
}
ngAfterViewInit(userId?: number) {
let maxcount = 1000;
this.filterAddressTypes(maxcount);
this.locationSelected = $("#locationSelectInput option:selected").text();
// this.selectedCountry = $("#countrySelectInput option:selected").text();
}
getvendorForEdit(){
this.spinnerService.show();
this._vendorServiceProxy.getVendorForEdit(this.id).subscribe(result => {
this.vendor = result;
if(this.vendor.contact==null)
{
this.selectedCountry = "";
this.selectedPostalCode = "";
this.selectedState = "";
this.selectedCity = "";
this.selectedAddressType = "";
}else{
this.selectedCountry = this.vendor.contact.address.postalCode.country;
// this.selectedPostalCode = this.vendor.contact.address.postalCode.value;
// this.selectedState = this.vendor.contact.address.postalCode.state;
// this.selectedCity = this.vendor.contact.address.postalCode.city;
this.addressForm.formattedAddress = this.vendor.contact.address.formattedAddress;
this.addressForm.country = this.vendor.contact.address.postalCode.country;
this.addressForm.value = this.vendor.contact.address.postalCode.value;
this.addressForm.state = this.vendor.contact.address.postalCode.state;
this.addressForm.city = this.vendor.contact.address.postalCode.city;
this.addressForm.latitude = this.vendor.contact.address.geocode.latitude;
this.addressForm.longitude = this.vendor.contact.address.geocode.longitude;
this.addressForm.address.addressLine1 = this.vendor.contact.address.addressLine1;
this.addressForm.address.addressLine2 = this.vendor.contact.address.addressLine2;
this.selectedAddressType = this.vendor.contact.address.addressType ? this.vendor.contact.address.addressType.name : "";
//this.address.addressLine1 = this.vendor.contact.address.addressLine1;
//this.address.addressLine2 = this.vendor.contact.address.addressLine2;
this.address.addressTypeId = this.vendor.contact.address.addressType ? this.vendor.contact.address.addressType.id : null;
this.postalCodeId = this.vendor.contact.address.postalCode.id;
}
if(this.vendor.vendor!=null)
{
this.vendorInput.idNumber = this.vendor.vendor.idNumber;
this.vendorInput.startDate =this.vendor.vendor.startDate ? moment.tz(this.vendor.vendor.startDate, localStorage.getItem('timeZoneId')) : null;
this.vendorInput.hourlyRate = this.vendor.vendor.hourlyRate;
this.vendorInput.dailyRate = this.vendor.vendor.dailyRate;
}
if(result.vendor == undefined) {
this.fleetId = 0;
}
else {
this.fleetId = result.vendor.fleet==null ? 0 : result.vendor.fleet.id;
}
setTimeout(() => {
this.setRandomPassword = false;
}, 0);
this.firstName = this.vendor.user.name;
this.surName = this.vendor.user.surname;
this.email = this.vendor.user.emailAddress;
this.phoneNumber = this.vendor.user.phoneNumber;
this.userName = this.vendor.user.userName;
this.password = this.vendor.user.password;
this.isActive = this.vendor.user.isActive;
this.shouldChangePasswordOnNextLogin = result.user.shouldChangePasswordOnNextLogin;
this.sendActivationEmail = result.user.sendActivationEmail;
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);
this.filterLocations();
this.spinnerService.hide();
this.getFleets();
});
}
save(): void {
this.saving = true;
if (this.contactForm.pNumber == null) {
this.contactForm.pNumber = "n/a";
}
this.vendorInput.user.id = this.vendor.user.id;
this.vendorInput.user.userName = this.userName;
this.vendorInput.user.password = this.password;
this.vendorInput.user.name = this.contactForm.fName;
this.vendorInput.user.surname = this.contactForm.sName;
this.vendorInput.user.phoneNumber = this.contactForm.pNumber;
this.vendorInput.user.emailAddress = this.contactForm.emailAddress;
this.vendorInput.user.shouldChangePasswordOnNextLogin = this.shouldChangePasswordOnNextLogin;
this.vendorInput.user.sendActivationEmail = this.sendActivationEmail;
this.vendorInput.setRandomPassword = this.setRandomPassword;
this.vendorInput.sendActivationEmail = this.sendActivationEmail;
this.vendorInput.user.isActive = this.isActive;
this.vendorInput.user.isLockoutEnabled = this.isLockoutEnabled;
this.vendorInput.user.isTwoFactorEnabled = this.isTwoFactorEnabled;
this.vendorInput.location = $("#locationSelectInput").val();
this.vendorInput.fleetId = $("#FleetsVendor").val();
this.vendorInput.address = this.addressForm.address;
this.vendorInput.longitude = this.addressForm.longitude;
this.vendorInput.latitude = this.addressForm.latitude;
if(this.vendor.contact!=null) {
this.vendorInput.address.id = this.vendor.contact.address.id;
}
// this.vendor.address.postalCodeId = Number((document.getElementById('postalSelectInput')).value);
this.vendorInput.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value) == 0
? null : Number((document.getElementById('addressTypeSelectInput')).value);
this.vendorInput.address.formattedAddress = this.addressForm.formattedAddress;
this.contactInput.lastName = this.contactForm.sName;
this.contactInput.firstName = this.contactForm.fName;
this.vendorInput.contact = this.contactInput;
this.vendorInput.contactId = this.vendor.contact==null ? null : this.vendor.contact.id;
this.vendorInput.startDate = this.vendorInput.startDate ? moment.utc(this.vendorInput.startDate) : null;
this.postalCodeInput.city = this.addressForm.city;
// this.postalCodeInput.state = this.addressForm.state;
if(!this.stateFeature){
this.postalCodeInput.state = "";
}
else{
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.vendorInput.address.postalCodeId = result
this._vendorServiceProxy.createOrUpdateVendor(this.vendorInput)
.pipe(finalize(()=>{ this.saving = false; }))
.subscribe(
() => {
this.saving = false;
this.notify.info(this.l('SavedSuccessfully'));
this.router.navigate(['/app/sprintship/vendors']);
});
});
}
filterLocations(): 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.vendor.locationId) {
if (this.vendor.locationId[key2] == result[key].id) {
arrAny = {
'name': result[key].name,
'id': result[key].id,
'selected': true
};
}
}
arrHolder.push(arrAny);
}
this.filteredLocations = arrHolder;
});
}
filterAddressTypes(maxCount): void {
this._addressTypeService.getAddressType(
undefined,
undefined,
undefined,
undefined,
maxCount,
undefined
).subscribe(result => {
this.filteredAddressType = result.items;
});
}
goBack() {
this._location.back();
}
initial(){
$("#addressTypeSelectInput").change(function () {
this.selectedAddressType = $("#addressTypeSelectInput option:selected").text();
$("#selectedAddressType").text(this.selectedAddressType);
});
$("#countrySelectInput").change(function () {
this.selectedCountry = $("#countrySelectInput option:selected").text();
$("#selectedCountry").text(this.selectedCountry + ",");
});
$("#postalSelectInput").change(function () {
this.selectedPostalCode = $("#postalSelectInput option:selected").text();
$("#selectedPostalCode").text(this.selectedPostalCode + ",");
});
$("#stateSelectInput").change(function () {
this.selectedState = $("#stateSelectInput option:selected").text();
$("#selectedState").text(this.selectedState + ",");
});
$("#citySelectInput").change(function () {
this.selectedCity = $("#citySelectInput option:selected").text();
$("#selectedCity").text(this.selectedCity + ",");
});
$("#locationSelectInput").change(function () {
this.locationSelected = $("#locationSelectInput option:selected").text();
$("#locationSelected").text(this.locationSelected);
});
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 },
locationSelectInput: { required: !0 },
UserName: { required: !0 },
Address1: { required: !0 },
postalSelectInput: { required: !0 },
},
invalidHandler: function (e, r) {
},
submitHandler: function (e) { }
})
}
}
}();
jQuery(document).ready(function () { KTWizard2.init() });
}
getFleets(){
this._fleetService.getAllFleets(undefined, undefined, undefined, undefined, undefined)
.subscribe(result=>{
this.fleets = result.items
// console.log(this.fleets)
})
}
isEmptyObject(obj) {
return (obj && (Object.keys(obj).length == 0));
}
}