licensemodestysettings.component.ts 5.95 KB
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],
      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<any>) {
    this.modalRef = this.modalService.show(template);
  }

  GetLicenseAccounts() {
    this.tempLstAccountNumbers=[];
    this.licenseService.GetLicenseAccounts(0)
      .subscribe(st => {   
       var newOption = $('<option value=""></option>');
       $('#accountSelect').append(newOption);
        this.tempLstAccountNumbers=st;       
        setTimeout(function(){         
          $('#accountSelect').trigger('chosen:updated');  
       }, 500);
       this._loadingService.HideLoading("global-loading");

      }, error => this.error = <any>error);
  }

  ShowModestyorSites(template: TemplateRef<any>) {
    this._loadingService.ShowLoading("global-loading");
    this.lstLicenseEditionModesty = null;
    this.lstLicenseSites = null;
    this.selectedSiteId = 0;
    if (!this.isBuildingLevel) {
      this.GetLicenseEditionModesty();
    }
    else {
      this.licenseService.GetLicenseSites(this.AccountNumber, 1, 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 = <any>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 = <any>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);
  }


  AfterUpdateData(data, template) {
    if (data.Status == "false") {
      this.alerts = "<span>License modesty settings update unsuccessfull.</span>";
    } else {
      this._confirmService.activate("License modesty settings updated successfully.", "alertMsg");     
    }
    this._loadingService.HideLoading("global-loading");
  }

  UpdateLicenseModestySettings(template: TemplateRef<any>) {
    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 = <any>error);
    }
  }


}