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 { BsModalService } from 'ngx-bootstrap/modal'; import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service'; import { ConfirmService } from '../../shared/confirm/confirm.service'; import { LoadingService } from '../../shared/loading.service'; import * as jQuery from 'jquery'; declare var $:JQueryStatic; @Component({ templateUrl: './licensemodestysettings.component.html' }) export class LicenseModestySettings implements OnInit { tempLstAccountNumbers: any; lstLicenseSites: any; lstLicenseEditionModesty: any; //license: License; updateModestySettingsFrm: FormGroup; error: any; alerts: string; modalAlerts: string; modalMessage: string; modalRef: BsModalRef; selectedSiteId: number = 0; isBuildingLevel: boolean = false; LicenseId:number=0; AccountNumber:string=''; constructor(private _loadingService: LoadingService,private licenseService: LicenseService, public globalService: GlobalService, private router: Router, private activeRoute: ActivatedRoute, private fb: FormBuilder, private modalService: BsModalService, private _confirmService: ConfirmService) { } ngOnInit(): void { this._loadingService.ShowLoading("global-loading"); $('#accountSelect').chosen({allow_single_deselect:true,width:'200px',placeholder_text_single:'Select Account',search_contains:true}); this.alerts = ''; this.updateModestySettingsFrm = this.fb.group({ licenseId: [0], accountNumber: ['', Validators.required], siteId: [0], isSiteUser:[false], lstModesty: [this.fb.array([])], }); $('#AccountNumber').prop('disabled', true); if (this.globalService.UserType > 2) { this.tempLstAccountNumbers=[]; this.tempLstAccountNumbers.push({m_Item1:this.globalService.AccLicId,m_Item2:this.globalService.AccountNumber}); this.AccountNumberChanged(this.globalService.AccLicId,this.globalService.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); }); } openModal(template: TemplateRef) { this.modalRef = this.modalService.show(template); } GetLicenseAccounts() { this.tempLstAccountNumbers=[]; this.licenseService.GetLicenseAccounts(0) .subscribe(st => { var newOption = $(''); $('#accountSelect').append(newOption); this.tempLstAccountNumbers=st; setTimeout(function(){ $('#accountSelect').trigger('chosen:updated'); }, 500); this._loadingService.HideLoading("global-loading"); }, error => this.error = error); } ShowModestyorSites(template: TemplateRef) { this._loadingService.ShowLoading("global-loading"); this.lstLicenseEditionModesty = null; this.lstLicenseSites = null; this.selectedSiteId = 0; this.updateModestySettingsFrm.controls['siteId'].setValue(0); if (!this.isBuildingLevel) { this.GetLicenseEditionModesty(); } else { //this service function also use in building level account this.licenseService.GetLicenseSites({AccountNumber:this.AccountNumber,sortColumn:'Id',sortOrder:'asc',pageNo:1,pageLength:1000}) .subscribe(st => { this.lstLicenseSites = st.LicenseSiteList; if (this.lstLicenseSites.length == 0) { this.modalMessage = 'Account is not a building level account.'; this.openModal(template); } this._loadingService.HideLoading("global-loading"); }, error => this.error = error); } } GetLicenseById() { //use for to check is siteuser or not if (this.LicenseId != 0) { this._loadingService.ShowLoading("global-loading"); this.licenseService.GetLicenseById(this.LicenseId) .subscribe(st => { if (st.LicenseId > 0) { if(st.LicenseTypeId==3) { this.updateModestySettingsFrm.controls['isSiteUser'].setValue(true); } else { this.updateModestySettingsFrm.controls['isSiteUser'].setValue(false); } this._loadingService.HideLoading("global-loading"); } }, error => this.error = error); } } GetLicenseEditionModesty() { this.licenseService.GetLicenseModestySettings(this.LicenseId, this.selectedSiteId) .subscribe(st => { this.lstLicenseEditionModesty = st; this.updateModestySettingsFrm.setControl('lstModesty', this.fb.array(this.lstLicenseEditionModesty)); this._loadingService.HideLoading("global-loading"); }, error => this.error = error); } LicenseSiteChanged(siteId: number) { this.selectedSiteId = siteId; if (this.selectedSiteId == 0) { this.lstLicenseEditionModesty = null; return; } this._loadingService.ShowLoading("global-loading"); this.GetLicenseEditionModesty(); } AccountNumberChanged(LicenseId: number,AccountNumber:string) { this.lstLicenseEditionModesty = null; this.lstLicenseSites = null; this.selectedSiteId = 0; this.isBuildingLevel = false; this.LicenseId=LicenseId; this.AccountNumber=AccountNumber; this.updateModestySettingsFrm.controls['licenseId'].setValue(LicenseId); this.updateModestySettingsFrm.controls['accountNumber'].setValue(AccountNumber); this.GetLicenseById() } AfterUpdateData(data, template) { if (data.Status == "false") { this.alerts = "License modesty settings update unsuccessfull."; } else { this._confirmService.activate("License modesty settings updated successfully.", "alertMsg"); } this._loadingService.HideLoading("global-loading"); } UpdateLicenseModestySettings(template: TemplateRef) { this.alerts = ''; if (this.alerts == '') { this._loadingService.ShowLoading("global-loading"); var obj = this.updateModestySettingsFrm.value; return this.licenseService.UpdateLicenseModestySettings(obj) .subscribe( n => (this.AfterUpdateData(n, template)), error => this.error = error); } } }