licensemodulesettings.component.ts 4.72 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 { License } from '../userentity/datamodel';
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'; 
declare var $:JQueryStatic;
@Component({
  templateUrl: './licensemodulesettings.component.html'
})

export class LicenseModuleSettings implements OnInit {

  tempLstAccountNumbers: any;
  lstModuleStatus: any;
  license: License;
  updateModuleSettingsFrm: FormGroup;
  error: any;
  alerts: string;
  modalAlerts: string;
  modalRef: BsModalRef;
  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.license = new License();
    this.alerts = '';
    this.updateModuleSettingsFrm = this.fb.group({
      licenseId: [0],
      accountNumber: [''],
      lstModuleStatus: [this.fb.array([])],
    });

    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);
  }

  GetLicenseModulesStatus() {
    this.licenseService.GetLicenseModulesStatus(this.LicenseId)
      .subscribe(st => {
        this.lstModuleStatus = st;
        this.updateModuleSettingsFrm.setControl('lstModuleStatus', this.fb.array(this.lstModuleStatus));
        this._loadingService.HideLoading("global-loading");
      }, error => this.error = <any>error);
  }

  AccountNumberChanged(LicenseId: number,AccountNumber:string) {
    this._loadingService.ShowLoading("global-loading");
    this.lstModuleStatus = null;
    this.LicenseId=LicenseId;
    this.AccountNumber=AccountNumber;

    this.updateModuleSettingsFrm.controls['licenseId'].setValue(LicenseId);
    this.updateModuleSettingsFrm.controls['accountNumber'].setValue(AccountNumber);
    this.GetLicenseModulesStatus();

  }

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

  UpdateLicenseModulesStatus(template: TemplateRef<any>) {
    this.alerts = '';
    if (this.alerts == '') {
      this._loadingService.ShowLoading("global-loading");
      var obj = this.updateModuleSettingsFrm.value;
      return this.licenseService.UpdateLicenseModulesStatus(obj)
        .subscribe(
        n => (this.AfterUpdateData(n, template)),
        error => this.error = <any>error);
    }
  }


}