import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef } from '@angular/core'; import { LicenseService } from './license.service'; import { GlobalService } from '../../Shared/global'; import { Router, ActivatedRoute } from '@angular/router'; import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { License } from '../UserEntity/datamodel'; import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap'; import { Http, Response } from '@angular/http'; import { DatePipe } from '@angular/common'; import { BsModalService } from 'ngx-bootstrap/modal'; import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service'; import { ContenteditableModelDirective } from '../../shared/contenteditabledirective'; import { NumberOnlyDirective } from '../../shared/numberonlydirective'; @Component({ templateUrl: './addlicense.component.html' }) export class AddLicense implements OnInit { lstLicenceType: any; lstCountry: any; lstState: any; lstLicense: any; lstAccountType: any; lstEdition: any; lstSecurityQuestion: any; lstEditionLogins: any; totalLogins: number = 0; license: License; insertUpdateLicenseFrm: FormGroup; error: any; alerts: string; modalAlerts: string; divClass: string = ''; topPos: string = '2000px'; datePipe: DatePipe = new DatePipe('en-US'); bsValue1: Date = new Date(); bsValue2: Date = new Date(); bsValue3: Date = new Date(); modalRef: BsModalRef; editionLoginsText: string; rowIndex: number = 0; ParamsObj: any; minDate = new Date(1110, 11, 1); maxDate = new Date(9999, 11, 31); bsConfig: Partial; dateStartInvalid: boolean = false; dateEndInvalid: boolean = false; dateRenewInvalid: boolean = false; constructor(private licenseService: LicenseService, private globalService: GlobalService, private router: Router, private activeRoute: ActivatedRoute, private fb: FormBuilder, private modalService: BsModalService) { } ngOnInit(): void { this.bsConfig = Object.assign({}, { containerClass: 'theme-dark-blue' }); this.divClass = 'col-sm-12'; this.license = new License(); this.alerts = ''; this.insertUpdateLicenseFrm = this.fb.group({ licenseId: [0], accountNumber: ['', Validators.required], accountTypeId: [0], productId: [''], licenseeFirstName: ['', Validators.required], licenseeLastName: ['', Validators.required], licenseTypeId: [1], institutionName: ['', Validators.required], address1: ['', Validators.required], address2: [''], city: ['', Validators.required], stateId: [0, Validators.min(1)], countryId: [0, Validators.min(1)], zip: ['', Validators.required], emailId: ['', [Validators.email, Validators.required]], phone: ['', [Validators.required, Validators.pattern('^([0-9]{3})-([0-9]{3})-([0-9]{4})$')]], editionLoginArr: this.fb.array([]), editionLogins: [''], totalLogins: [0], subscriptionPrice: ['', [Validators.required, Validators.pattern('[0-9.]*')]], subscriptionStartDate: ['', Validators.required], subscriptionEndDate: ['', Validators.required], noOfImages: [0, [Validators.required, Validators.pattern('[0-9]*')]], masterSiteUrl: ['a', Validators.required], siteUrlFrom: [''], siteUrlTo: [''], login: ['a', [Validators.required, Validators.minLength(8)]], password: ['a', [Validators.required, Validators.minLength(8)]], securityQuestionId: [1], answer: ['a', Validators.required], testLicenseEditionId: [1], creatorId: [this.globalService.loggedInUser.Id], renew: [false], renewDate: [''], totalRenewals: [0], isActive: ['false'] }); this.GetLicenseType(); this.GetCountry(); this.GetState(); this.GeAccountType(); this.GeEditions(); this.GeSecurityQuestions(); this.license.LicenseId = 0; this.license.LoginId = 'abcdefgh'; this.license.Password = 'abcdefgh'; this.license.SecurityQuestionId = 1; this.license.Answer = 'a'; this.insertUpdateLicenseFrm.controls['login'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['password'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1); this.insertUpdateLicenseFrm.controls['answer'].setValue('a'); this.activeRoute.queryParams .subscribe(params => { if (params['Id'] != null) { this.ParamsObj = params; this.license.LicenseId = +params['Id']; this.rowIndex = +params['row']; this.GetLicenseById(); } }); } openModal(template: TemplateRef) { this.modalRef = this.modalService.show(template); } onChange(item: any, isChecked: boolean) { if (isChecked) { if (this.license.LicenseTypeId == 3) { item.Login = 1; } } else { item.Login = 0; } this.OnLoginBlur(); } DateChange(dateValue: any) { this.alerts = ''; if (this.insertUpdateLicenseFrm.dirty) { if (dateValue._datepicker._elementRef.nativeElement.id == 'SubscriptionStartDate') { if (dateValue._bsValue == null) { if (dateValue._datepicker._elementRef.nativeElement.value != '') { this.dateStartInvalid = true; } else { this.dateStartInvalid = false; } } else { this.dateStartInvalid = false; this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].setValue(dateValue._bsValue); } } if (dateValue._datepicker._elementRef.nativeElement.id == 'SubscriptionEndDate') { if (dateValue._bsValue == null) { if (dateValue._datepicker._elementRef.nativeElement.value != '') { this.dateEndInvalid = true; } else { this.dateEndInvalid = false; } } else { this.dateEndInvalid = false; this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].setValue(dateValue._bsValue); } } if (dateValue._datepicker._elementRef.nativeElement.id == 'RenewDate') { if (dateValue._bsValue == null) { if (dateValue._datepicker._elementRef.nativeElement.value != '') { this.dateRenewInvalid = true; } else { this.dateRenewInvalid = false; } } else { this.dateRenewInvalid = false; this.insertUpdateLicenseFrm.controls['renewDate'].setValue(dateValue._bsValue); } } if (!this.dateStartInvalid && !this.dateEndInvalid && !this.dateRenewInvalid) { if (Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].value) > Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].value)) { this.alerts = 'Subscription start date must be less than the subscription end date
'; } if (Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].value) > Date.parse(this.insertUpdateLicenseFrm.controls['renewDate'].value)) { this.alerts += 'Subscription start date must be less than the subscription renew date
'; } } } } GetLicenseType() { this.licenseService.GetLicenceType() .subscribe(x => { this.lstLicenceType = x; }, error => this.error = error); } GeAccountType() { this.licenseService.GetAccountType() .subscribe(x => { this.lstAccountType = x; }, error => this.error = error); } GeEditions() { this.licenseService.GetEditions() .subscribe(x => { this.lstEdition = x; this.lstEditionLogins = new Array(); this.lstEdition.forEach(element => { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0 }); }); this.insertUpdateLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins)); }, error => this.error = error); } GeSecurityQuestions() { this.licenseService.GetSecurityQuestions() .subscribe(x => { this.lstSecurityQuestion = x; }, error => this.error = error); } GetCountry() { this.licenseService.GetCountry() .subscribe(y => { this.lstCountry = y; }, error => this.error = error); } GetState() { this.licenseService.GetState() .subscribe(st => { this.lstState = st; }, error => this.error = error); } GetLicenseById() { if (this.license.LicenseId != 0) { this.licenseService.GetLicenseById(this.license.LicenseId) .subscribe(st => { this.license = st; if (this.license.LicenseId > 0) { this.license.LoginId = 'abcdefgh'; this.license.Password = 'abcdefgh'; this.license.SecurityQuestionId = 1; this.license.Answer = 'a'; } if (this.license.ProductKey == null) { this.license.ProductKey = ''; } if (this.license.LicenseTypeId != 3) { this.license.MasterSiteUrl = 'a'; } if (this.license.SiteUrlFrom == null) { this.license.SiteUrlFrom = ''; } if (this.license.SiteUrlTo == null) { this.license.SiteUrlTo = ''; } this.LicenseTypeChanged(this.license.LicenseTypeId); this.insertUpdateLicenseFrm.controls['licenseId'].setValue(this.license.LicenseId); this.insertUpdateLicenseFrm.controls['licenseTypeId'].setValue(this.license.LicenseTypeId); this.insertUpdateLicenseFrm.controls['accountNumber'].setValue(this.license.AccountNumber); this.insertUpdateLicenseFrm.controls['accountTypeId'].setValue(this.license.AccountTypeId); this.insertUpdateLicenseFrm.controls['productId'].setValue(this.license.ProductKey); this.insertUpdateLicenseFrm.controls['licenseeFirstName'].setValue(this.license.LicenseeFirstName); this.insertUpdateLicenseFrm.controls['licenseeLastName'].setValue(this.license.LicenseeLastName); this.insertUpdateLicenseFrm.controls['institutionName'].setValue(this.license.InstitutionName); this.insertUpdateLicenseFrm.controls['address1'].setValue(this.license.Address1); this.insertUpdateLicenseFrm.controls['address2'].setValue(this.license.Address2); this.insertUpdateLicenseFrm.controls['city'].setValue(this.license.City); this.insertUpdateLicenseFrm.controls['stateId'].setValue(this.license.StateId); this.insertUpdateLicenseFrm.controls['countryId'].setValue(this.license.CountryId); this.insertUpdateLicenseFrm.controls['zip'].setValue(this.license.Zip); this.insertUpdateLicenseFrm.controls['emailId'].setValue(this.license.EmailId); this.insertUpdateLicenseFrm.controls['phone'].setValue(this.license.Phone); this.insertUpdateLicenseFrm.controls['editionLogins'].setValue(this.license.EditionLogins); this.insertUpdateLicenseFrm.controls['totalLogins'].setValue(this.license.TotalLogins); this.insertUpdateLicenseFrm.controls['subscriptionPrice'].setValue(this.license.Price); this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].setValue(this.datePipe.transform(this.license.SubscriptionStartDate, 'MM/dd/yyyy')); this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].setValue(this.datePipe.transform(this.license.SubscriptionEndDate, 'MM/dd/yyyy')); this.insertUpdateLicenseFrm.controls['renewDate'].setValue(this.datePipe.transform(this.license.Renewdate, 'MM/dd/yyyy')); this.insertUpdateLicenseFrm.controls['noOfImages'].setValue(this.license.NoOfImages); this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue(this.license.MasterSiteUrl); this.insertUpdateLicenseFrm.controls['siteUrlFrom'].setValue(this.license.SiteUrlFrom); this.insertUpdateLicenseFrm.controls['siteUrlTo'].setValue(this.license.SiteUrlTo); this.insertUpdateLicenseFrm.controls['login'].setValue(this.license.LoginId); this.insertUpdateLicenseFrm.controls['password'].setValue(this.license.Password); this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(this.license.SecurityQuestionId); this.insertUpdateLicenseFrm.controls['answer'].setValue(this.license.Answer); this.insertUpdateLicenseFrm.controls['totalRenewals'].setValue(this.license.TotalRenewals); this.insertUpdateLicenseFrm.controls['renew'].setValue(false); //this.insertUpdateLicenseFrm.controls['creatorId'].setValue(this.license.UserId); if (this.license.IsActive) { this.insertUpdateLicenseFrm.controls['isActive'].setValue('true'); } else { this.insertUpdateLicenseFrm.controls['isActive'].setValue('false'); } if (this.license.EditionLogins == null) return; var TempArr = this.license.EditionLogins.split('|'); this.lstEditionLogins = new Array(); this.lstEdition.forEach(element => { if (this.license.LicenseTypeId == 4) { if (element.Id == 1 || element.Id == 3) { var Check: boolean = false; TempArr.forEach(elm => { var TempInnerArr = elm.split('-'); if (TempInnerArr[0] == element.Id) { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: parseInt(TempInnerArr[1]) }); Check = true; } }); if (!Check) { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0 }); } } } else { var Check: boolean = false; TempArr.forEach(elm => { var TempInnerArr = elm.split('-'); if (TempInnerArr[0] == element.Id) { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: parseInt(TempInnerArr[1]) }); Check = true; } }); if (!Check) { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0 }); } } }); this.insertUpdateLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins)); }, error => this.error = error); } } AfterInsertData(data, template) { if (data.Status == "false") { this.alerts = "License save unsuccessfull."; } else { this.modalAlerts = "

License saved successfully.

"; this.modalRef = this.modalService.show(template); } } AfterUpdateData(data, template) { if (data.Status == "false") { this.alerts = "License update unsuccessfull."; } else { this.modalAlerts = "

License updated successfully.

"; this.modalRef = this.modalService.show(template); } } OnLoginBlur() { this.totalLogins = 0; this.insertUpdateLicenseFrm.controls['editionLoginArr'].value.forEach(element => { if (!isNaN(parseInt(element.Login))) { this.totalLogins += parseInt(element.Login); } }); this.insertUpdateLicenseFrm.controls['totalLogins'].setValue(this.totalLogins); this.license.TotalLogins = this.totalLogins; } OnAccountNoBlur(AccountNo: string) { if (this.license.LicenseId == 0) { this.alerts = this.alerts.replace('Account number already exists. Enter a different account number.', ''); this.licenseService.CheckAccountNumber(AccountNo) .subscribe( response => { if (response == 'True') { this.alerts += 'Account number already exists. Enter a different account number.'; } }, error => this.error = error); } } LicenseTypeChanged(LicenseTypeId: number) { this.totalLogins = 0; this.insertUpdateLicenseFrm.controls['noOfImages'].setValue(''); this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(0); this.insertUpdateLicenseFrm.controls['answer'].setValue(''); this.insertUpdateLicenseFrm.controls['login'].setValue(''); this.insertUpdateLicenseFrm.controls['password'].setValue(''); this.insertUpdateLicenseFrm.controls['subscriptionPrice'].setValue(''); this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue(''); this.insertUpdateLicenseFrm.controls['accountNumber'].setValue(''); this.lstEditionLogins = new Array(); this.lstEdition.forEach(element => { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0 }); }); this.insertUpdateLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins)); if (LicenseTypeId == 1) { this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1); this.insertUpdateLicenseFrm.controls['answer'].setValue('a'); this.insertUpdateLicenseFrm.controls['login'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['password'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue('a'); } if (LicenseTypeId == 2) { this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue('a'); } if (LicenseTypeId == 3) { this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1); this.insertUpdateLicenseFrm.controls['answer'].setValue('a'); this.insertUpdateLicenseFrm.controls['login'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['password'].setValue('abcdefgh'); } if (LicenseTypeId == 4) { this.lstEditionLogins = new Array(); this.lstEdition.forEach(element => { if (element.Id == 1 || element.Id == 3) { this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0 }); } }); this.insertUpdateLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins)); this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1); this.insertUpdateLicenseFrm.controls['answer'].setValue('a'); this.insertUpdateLicenseFrm.controls['login'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['password'].setValue('abcdefgh'); this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue('a'); this.insertUpdateLicenseFrm.controls['accountNumber'].setValue('a'); } if (LicenseTypeId == 5) { this.insertUpdateLicenseFrm.controls['noOfImages'].setValue('5'); this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1); this.insertUpdateLicenseFrm.controls['answer'].setValue('a'); this.insertUpdateLicenseFrm.controls['subscriptionPrice'].setValue('5'); this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue('a'); } } TestLicenseEditionChanged(EditionId: number) { this.insertUpdateLicenseFrm.controls['testLicenseEditionId'].setValue(EditionId); } InsertUpdateLicense(template: TemplateRef) { this.alerts = ''; if (Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].value) > Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].value)) { this.alerts = 'Subscription start date must be lower than subscription end date
'; } if (Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].value) > Date.parse(this.insertUpdateLicenseFrm.controls['renewDate'].value)) { this.alerts += 'Subscription start date must be lower than subscription renew date'; } this.editionLoginsText = ''; this.insertUpdateLicenseFrm.controls['editionLoginArr'].value.forEach(element => { this.editionLoginsText += element.Id.toString() + '-' + element.Login + '|'; }); if (this.editionLoginsText != '') { this.editionLoginsText = this.editionLoginsText.substr(0, this.editionLoginsText.length - 1); } this.insertUpdateLicenseFrm.controls['editionLogins'].setValue(this.editionLoginsText); var obj = this.insertUpdateLicenseFrm.value; if (obj.licenseId == 0) { this.licenseService.CheckAccountNumber(this.insertUpdateLicenseFrm.controls['accountNumber'].value) .subscribe( response => { if (response == 'True') { this.alerts += 'Account number already exists. Enter a different account number.'; } if (this.alerts == '') { return this.licenseService.InsertLicense(obj) .subscribe( n => (this.AfterInsertData(n, template)), error => this.error = error); } }, error => this.error = error); } else { if (this.insertUpdateLicenseFrm.controls['renew'].value && this.insertUpdateLicenseFrm.controls['renewDate'].value == undefined) { this.alerts = 'Renew date is required'; } if (this.alerts == '') { return this.licenseService.UpdateLicense(obj) .subscribe( n => (this.AfterUpdateData(n, template)), error => this.error = error); } } } CancelAddEdit() { this.router.navigate(['/searchlicense'], { queryParams: this.ParamsObj , skipLocationChange: true }); } }