import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef, ViewChild,HostListener } from '@angular/core'; import { LicenseService } from './license.service'; import { Router, ActivatedRoute } from '@angular/router'; import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { License } from '../userentity/datamodel'; import { BsModalService } from 'ngx-bootstrap/modal'; import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service'; import { PagerComponent } from '../../shared/pager/pager.component'; import { LoadingService } from '../../shared/loading.service'; import { GlobalService } from '../../shared/global'; import { ConfirmService } from '../../shared/confirm/confirm.service'; declare var $:JQueryStatic; declare var jQuery:any; @Component({ templateUrl: './sitelicenseaccount.component.html' }) export class SiteLicenseAccount implements OnInit { lstCountry: any; lstState: any; lstAccountNumbers: any; lstLicenseSites: any; licenseSite: any; mode: string = 'Search'; license: License; insertUpdateSiteLicenseFrm: FormGroup; error: any; alerts: string; modalAlerts: string; divClass: string = ''; topPos: string = '2000px'; selectedRow: number = -1; selectedId: number = -1; modalRef: BsModalRef; lstEdition: any; lstEditionLogins: any; lstSiteAccountEditions: any; lstClientAdmin: any; NoRecord: string; @ViewChild(PagerComponent) pagerComponent: PagerComponent; recordCount: number; pageNo: number; pageLength: number; lstLicenseSiteAdmin: any; LicenseId:number=0; AccountNumber:string=''; // Declare height and width variables scrHeight:any; scrWidth:any; @HostListener('window:resize', ['$event']) getScreenSize(event?) { var $ua = navigator.userAgent; if (($ua.match(/(iPod|iPhone|iPad|android)/i))) { this.scrHeight = window.innerHeight-410; } else { this.scrHeight = window.innerHeight-360; } } constructor(private licenseService: LicenseService, private router: Router, private activeRoute: ActivatedRoute, private fb: FormBuilder, private modalService: BsModalService, public global: GlobalService, private _loadingService: LoadingService, private _confirmService: ConfirmService) {this.getScreenSize(); } ngOnInit(): void { this._loadingService.ShowLoading("global-loading"); $('#accountSelect').chosen({allow_single_deselect:true,width:'200px',placeholder_text_single:'Select Account',search_contains:true}); this.divClass = 'col-sm-12'; this.license = new License(); this.license.LicenseId = 0; this.alerts = ''; this.insertUpdateSiteLicenseFrm = this.fb.group({ licenseId: [0], accountNumber: [{value: '', disabled: true}], siteId: [0], siteUrl: ['', [Validators.required,this.noWhitespaceValidator]], siteUrlTo: [''], siteMasterUrlTo: [''], buildAccName: [''], institutionName: ['', [Validators.required,this.noWhitespaceValidator]], departmentName: [''], address1: ['', [Validators.required,this.noWhitespaceValidator]], address2: [''], city: ['', [Validators.required,this.noWhitespaceValidator]], countryId: [0, Validators.min(1)], stateId: [0, Validators.min(1)], zip: ['', [Validators.required,this.noWhitespaceValidator]], phone: ['', [Validators.required, Validators.pattern('^([0-9]{3})-([0-9]{3})-([0-9]{4})$'),this.noWhitespaceValidator]], //phone: ['', Validators.required], clientAdminId: [0, Validators.min(1)], clientAdminEmail: [{value: '', disabled: true}, this.noWhitespaceValidator], isActive: [0], isMaster: [0], creationDate: [''], modifiedDate: [''], editionLoginArr: this.fb.array([]), }); this.GetCountry(); this.GetState(); this.GetEditions(); if (this.global.UserType > 2) { this.lstAccountNumbers=[]; this.lstAccountNumbers.push({m_Item1:this.global.AccLicId,m_Item2:this.global.AccountNumber}); this.AccountNumberChanged(this.global.AccLicId,this.global.AccountNumber); setTimeout(function(){ $('#accountSelect').prop('disabled', true).trigger("chosen:updated"); $('#accountSelect').trigger('chosen:updated'); }, 500); this._loadingService.HideLoading("global-loading"); } else { this.GetLicenseAccounts(); } $('#accountSelect') .on('change', (e, args) => { var selectedValue = Number(args.selected); var selectedText= $(".chosen-single span" ).text(); this.AccountNumberChanged(selectedValue,selectedText); }); jQuery('#fixed_hdr2').fxdHdrCol({ fixedCols: 0, width: "100%", height: this.scrHeight, colModal: [ { width: 200, align: 'center' }, { width: 200, align: 'center' }, { width: 200, align: 'Center' }, { width: 200, align: 'Center' }, { width: 250, align: 'Center' }, { width: 200, align: 'Center' }, { width: 200, align: 'Center' }, { width: 200, align: 'Center' }, { width: 200, align: 'Center' }, ], sort: true }); this.selectedRow = -1; this.selectedId = -1; this.pageNo = 1; this.pageLength = 10; this.NoRecord = ''; this.recordCount = 0; this.pagerComponent = new PagerComponent(); if(document.getElementById("fixed_table_rc") != null){ document.getElementById("fixed_table_rc").remove(); var testScript = document.createElement("script"); testScript.setAttribute("id", "fixed_table_rc"); testScript.setAttribute("src", "../assets/scripts/fixed_table_rc.js"); testScript.setAttribute("type", "text/javascript"); document.body.appendChild(testScript); } } public noWhitespaceValidator(control: FormControl) { // new validation for intial whaite space //****Birendra *****/ var isValid=false; if(control.value!=null) { var controlLen=control.value.length; if(controlLen==undefined)//undefined for integer value { isValid=true; } else if(controlLen!=0) { const isWhitespace = (control.value || '').trim().length === 0; isValid = !isWhitespace; if(!isValid) { control.setValue(''); } } } // can use also on page of input control // return isValid ? null: { 'whitespace': true }; } onKeyUp(event: any) { var mobno = event.target.value; var newnum=mobno; if(mobno!="" && event.key!="Backspace") { var tempArr = mobno.split('-'); if(tempArr.length==1) { var countdigit=tempArr[0].length; newnum=tempArr[0]; if(countdigit==3) { newnum=tempArr[0]+"-"; } else if(countdigit>3) { newnum=tempArr[0].substr(0,3)+"-"+tempArr[0].substr(3,1); } } else if(tempArr.length==2) { newnum=tempArr[0]+"-"+tempArr[1]; var countdigit=tempArr[1].length; if(countdigit==3) { newnum=tempArr[0]+"-"+tempArr[1]+"-"; } else if(countdigit>3) { newnum= tempArr[0]+"-"+tempArr[1].substr(0,3)+"-"+tempArr[1].substr(3,1); } } else { newnum=tempArr[0]+"-"+tempArr[1]+"-"+tempArr[2]; } } this.insertUpdateSiteLicenseFrm.controls['phone'].setValue(newnum); }; openModal(template: TemplateRef) { this.modalRef = this.modalService.show(template); } public SetClickedRow(i: number, item: any) { this.selectedRow = i; this.selectedId = item['Id']; this.licenseSite = item; } BindFormFields(data){ this.recordCount = data.RecordCount; this.lstLicenseSites = data.LicenseSiteList; if(this.recordCount > 0){ if(this.selectedRow > -1){ this.selectedId = this.lstLicenseSites[this.selectedRow].siteId; this.SetClickedRow(this.selectedRow, this.lstLicenseSites[this.selectedRow]); } } this.GetLicenseSiteAdmin(); if (this.lstLicenseSites.length > 0) { this.NoRecord = ''; this._loadingService.HideLoading("global-loading"); } if (this.lstLicenseSites.length == 0) { this.NoRecord = this.global.NoRecords; this._loadingService.HideLoading("global-loading"); } } BindAdminFormFields(data){ this.lstLicenseSiteAdmin = data.LicenseSiteAdminList; this.insertUpdateSiteLicenseFrm.controls['clientAdminId'].setValue(0); this.insertUpdateSiteLicenseFrm.controls['clientAdminEmail'].setValue(''); var tempArr = []; this.lstLicenseSiteAdmin.forEach(element => { tempArr.push({ "Id": element.m_Item1, "Name": element.m_Item2, "Email": element.m_Item3 }); }); this.lstClientAdmin = tempArr; } GetLicenseAccounts() { this.lstAccountNumbers=[]; this.licenseService.GetLicenseAccounts(3) .subscribe(st => { var newOption = $(''); $('#accountSelect').append(newOption); this.lstAccountNumbers=st; setTimeout(function(){ $('#accountSelect').trigger('chosen:updated'); }, 500); this._loadingService.HideLoading("global-loading"); }, error => this.error = error); } AccountNumberChanged(LicenseId: number,AccountNumber:string) { this._loadingService.ShowLoading("global-loading"); this.selectedRow = -1; this.LicenseId = LicenseId; this.AccountNumber=AccountNumber; this.lstLicenseSites = null; this.NoRecord = ''; this.GetLicenseById(); } ClientAdminChanged(UserId: number){ if(UserId == 0){ this.insertUpdateSiteLicenseFrm.controls['clientAdminEmail'].setValue(''); return; } let tempmail = this.lstLicenseSiteAdmin.find(elm => elm.m_Item1 == UserId).m_Item3; this.insertUpdateSiteLicenseFrm.controls['clientAdminEmail'].setValue(tempmail); } 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); } GetSiteById(){ this.licenseService.GetSiteById(this.selectedId) .subscribe(st => { this.licenseSite = st; this.insertUpdateSiteLicenseFrm.controls['siteId'].setValue(this.licenseSite.Id); this.insertUpdateSiteLicenseFrm.controls['buildAccName'].setValue(this.licenseSite.Title); this.insertUpdateSiteLicenseFrm.controls['siteUrl'].setValue(this.licenseSite.Ip); this.insertUpdateSiteLicenseFrm.controls['siteUrlTo'].setValue(this.licenseSite.SiteIpTo); this.insertUpdateSiteLicenseFrm.controls['siteMasterUrlTo'].setValue(this.licenseSite.MasterIpTo); this.insertUpdateSiteLicenseFrm.controls['institutionName'].setValue(this.licenseSite.InstituteName); this.insertUpdateSiteLicenseFrm.controls['departmentName'].setValue(this.licenseSite.Department); this.insertUpdateSiteLicenseFrm.controls['address1'].setValue(this.licenseSite.Address1); this.insertUpdateSiteLicenseFrm.controls['address2'].setValue(this.licenseSite.Address2); this.insertUpdateSiteLicenseFrm.controls['city'].setValue(this.licenseSite.City); this.insertUpdateSiteLicenseFrm.controls['phone'].setValue(this.licenseSite.Phone); this.insertUpdateSiteLicenseFrm.controls['zip'].setValue(this.licenseSite.Zip); this.insertUpdateSiteLicenseFrm.controls['countryId'].setValue(this.licenseSite.CountryId); this.insertUpdateSiteLicenseFrm.controls['stateId'].setValue(this.licenseSite.StateId); this.insertUpdateSiteLicenseFrm.controls['isActive'].setValue(this.licenseSite.IsActive); this.insertUpdateSiteLicenseFrm.controls['isMaster'].setValue(this.licenseSite.IsMaster); this.insertUpdateSiteLicenseFrm.controls['creationDate'].setValue(this.licenseSite.CreationDate); this.insertUpdateSiteLicenseFrm.controls['modifiedDate'].setValue(this.licenseSite.ModifiedDate); this.insertUpdateSiteLicenseFrm.controls['clientAdminId'].setValue(this.licenseSite.SiteUserId); this.insertUpdateSiteLicenseFrm.controls['clientAdminEmail'].setValue(this.licenseSite.SiteUserEmailId); this.GetSiteAccountEditions(); }, error => this.error = error); } GetSiteAccountEditions(){ this.licenseService.GetSiteAccountEditions(this.licenseSite.Id, this.license.LicenseId) .subscribe(st => { this.lstSiteAccountEditions = st; this.lstEditionLogins.forEach(element => { this.lstSiteAccountEditions.forEach(elm => { if(elm.m_Item2 == element.Id){ element.Login = 1; } }); }); this.insertUpdateSiteLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins)); }, error => this.error = error); } GetLicenseSites(evt: any) { if (this.alerts != '') return; if(this.license.LicenseId == 0){ return; } this._loadingService.ShowLoading("global-loading"); var tempArr = evt.split(','); this.pageNo = parseInt(tempArr[0]); this.pageLength = parseInt(tempArr[1]); this._loadingService.ShowLoading("global-loading"); this.licenseService.GetLicenseSites(this.license.AccountNumber, this.pageNo, this.pageLength) .subscribe(x => { this.BindFormFields(x) }, error => this.error = error); } GetLicenseSiteAdmin() { this.licenseService.GetLicenseSiteAdmin(this.license.AccountNumber) .subscribe(x => { this.BindAdminFormFields(x) }, error => this.error = error); } SearchRecords() { this.selectedRow = -1; this.selectedId = -1; this.GetLicenseSites('1, ' + this.pageLength); } GetEditions() { this.licenseService.GetEditions() .subscribe(x => { this.lstEdition = x; }, error => this.error = error); } onChange(item: any, isChecked: boolean){ if(this.license.LicenseTypeId == 3){ if(isChecked){ item.Login = 1; } else{ item.Login = 0; } } } GetLicenseById() { if(this.LicenseId != 0) { this.licenseService.GetLicenseById(this.LicenseId) .subscribe(st => { this.license = st; this.insertUpdateSiteLicenseFrm.controls['licenseId'].setValue(this.license.LicenseId); this.insertUpdateSiteLicenseFrm.controls['accountNumber'].setValue(this.license.AccountNumber); if(this.license.EditionLogins == null) return; var TempArr = this.license.EditionLogins.split('|'); this.lstEditionLogins = new Array(); this.lstEdition.forEach(element => { TempArr.forEach(elm => { var TempInnerArr = elm.split('-'); if(TempInnerArr[0] == element.Id){ this.lstEditionLogins.push({Id: element.Id, Title: element.Title, Login: 0}); } }); }); this.insertUpdateSiteLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins)); this._loadingService.HideLoading("global-loading"); }, error => this.error = error); } } AfterDeleteData(data, template) { if (data.Status == "false") { this.alerts = "Site account delete unsuccessfull"; } else { this._confirmService.activate("Site account deleted successfully.", "alertMsg"); //this.modalAlerts = "

Site account deleted successfully

"; //this.modalRef = this.modalService.show(template); this.SearchRecords(); } } AfterInsertData(data, template) { if (data == "0") { this.alerts = "License site save unsuccessfull."; } else { this.mode = 'Edit'; this.selectedId = data; this.GetSiteById(); //this.modalAlerts = "

License site saved successfully.

"; this._confirmService.activate("License site saved successfully.", "alertMsg"); // this.modalRef = this.modalService.show(template); } } AfterUpdateData(data, template) { if (data == "0") { this.alerts = "License site update unsuccessfull."; } else { //this.modalAlerts = "

License site updated successfully.

"; this._confirmService.activate("License site updated successfully.", "alertMsg"); // this.modalRef = this.modalService.show(template); } } InsertUpdateSiteAccount(template: TemplateRef) { this.alerts = ''; var obj = this.insertUpdateSiteLicenseFrm.value; var temptext = ''; obj.editionLoginArr.forEach(element => { if (element.Login == 1) { temptext += element.Id + ','; } }); if (temptext == '') { this.alerts = "Please select a product edition"; } if (this.alerts == '') { if (obj.siteId == 0) { this.licenseService.InsertUpdateSiteAccount(obj) .subscribe( n => (this.AfterInsertData(n, template)), error => this.error = error); } else { this.licenseService.InsertUpdateSiteAccount(obj) .subscribe( n => (this.AfterUpdateData(n, template)), error => this.error = error); } } } DeleteSiteAccount(template: TemplateRef){ this.modalRef.hide(); this.alerts = ''; if(this.alerts == ''){ var obj = this.licenseSite; obj.LicenseId = this.license.LicenseId; return this.licenseService.DeleteSiteAccount(obj) .subscribe( data => (this.AfterDeleteData(data, template)), error => { this.error = error; this.alerts = "" + this.error + ""; }); } } AddLicenseSite(){ this.mode = 'Add'; this.topPos = '100px'; this.alerts = ''; this.insertUpdateSiteLicenseFrm.controls['siteId'].setValue(0); this.insertUpdateSiteLicenseFrm.controls['buildAccName'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['siteUrl'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['siteUrlTo'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['institutionName'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['departmentName'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['address1'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['address2'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['city'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['phone'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['zip'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['countryId'].setValue(0); this.insertUpdateSiteLicenseFrm.controls['stateId'].setValue(0); this.insertUpdateSiteLicenseFrm.controls['isActive'].setValue(1); this.insertUpdateSiteLicenseFrm.controls['isMaster'].setValue(0); this.insertUpdateSiteLicenseFrm.controls['creationDate'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['modifiedDate'].setValue(''); this.insertUpdateSiteLicenseFrm.controls['clientAdminId'].setValue(0); this.insertUpdateSiteLicenseFrm.controls['clientAdminEmail'].setValue(''); } OnCountryChange(element: any) { this.license.CountryId = parseInt(element.value); if (this.lstCountry.find(C => C.Id == this.license.CountryId).CountryName != "United States") { this.license.StateId = this.lstState.find(C => C.StateName == "Other").Id; this.insertUpdateSiteLicenseFrm.controls['stateId'].setValue(this.license.StateId); // this.insertUpdateSiteLicenseFrm.controls['phone'].clearValidators(); // this.insertUpdateSiteLicenseFrm.controls['phone'].setValidators([Validators.required,this.noWhitespaceValidator]); } else { this.license.StateId = 0; this.insertUpdateSiteLicenseFrm.controls['stateId'].setValue(this.license.StateId); // this.insertUpdateSiteLicenseFrm.controls['phone'].clearValidators(); // this.insertUpdateSiteLicenseFrm.controls['phone'].setValidators([Validators.required, Validators.pattern('^([0-9]{3})-([0-9]{3})-([0-9]{4})$'),this.noWhitespaceValidator]); } // this.insertUpdateSiteLicenseFrm.controls['phone'].updateValueAndValidity(); } OnStateChange(element: any) { this.license.StateId = parseInt(element.value); if (this.lstState.find(C => C.Id == this.license.StateId).StateName != "Other") { this.license.CountryId = this.lstCountry.find(C => C.CountryName == "United States").Id; this.insertUpdateSiteLicenseFrm.controls['countryId'].setValue(this.license.CountryId); // this.insertUpdateSiteLicenseFrm.controls['phone'].clearValidators(); // this.insertUpdateSiteLicenseFrm.controls['phone'].setValidators([Validators.required, Validators.pattern('^([0-9]{3})-([0-9]{3})-([0-9]{4})$'),this.noWhitespaceValidator]); } else { this.license.CountryId = 0; this.insertUpdateSiteLicenseFrm.controls['countryId'].setValue(this.license.CountryId); // this.insertUpdateSiteLicenseFrm.controls['phone'].clearValidators(); // this.insertUpdateSiteLicenseFrm.controls['phone'].setValidators([Validators.required,this.noWhitespaceValidator]); } // this.insertUpdateSiteLicenseFrm.controls['phone'].updateValueAndValidity(); } EditLicenseSite(){ this.mode = 'Edit'; this.topPos = '100px'; this.alerts = ''; this.GetSiteById(); } CancelAddEdit(){ this.mode = 'Search'; this.topPos = '2000px'; this.GetLicenseSites(this.pageNo + ', ' + this.pageLength); } }