licenseAgreement.component.ts 8.35 KB
import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform,HostListener, TemplateRef } from '@angular/core';
import { LicenseService } from './license.service';
import { Router } from '@angular/router';
import { GlobalService } from '../../shared/global';
import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { License } from '../userentity/datamodel';
import { ConfirmService } from '../../shared/confirm/confirm.service';
import { LoadingService } from '../../shared/loading.service';
import * as jQuery from 'jquery';
declare var $:JQueryStatic;
@Component({
  templateUrl: './licenseAgreement.component.html'
})

export class licenseAgreement implements OnInit {

  lstLicenceType: any;
  lstCountry: any;
  lstState: any;
  lstLicense: any;
  lstAccountType: any;
  lstEdition: any;
  lstEditionLogins: any;
  license: License;
  LicenseAgreementFrm: FormGroup;
  error: any;
  tempLstAccountNumbers: any;
  UserType:number;
  constructor(private _loadingService: LoadingService,private licenseService: LicenseService, private globalService: GlobalService, 
     private fb: FormBuilder, private router: Router) {
      this.UserType =this.globalService.UserType;
    }
    
  ngOnInit(): void {
    this._loadingService.ShowLoading("global-loading");
    if( this.UserType<=2)
    {
      $('#accountSelect').chosen({allow_single_deselect:true,width:'300px',placeholder_text_single:'Select Account',search_contains:true});
    }
    else
    {
     $("#accountsection").css("display","none");
    }

   
    this.license = new License();
  	this.license.LicenseId=0;
	  this.UserType =this.globalService.UserType;
    this.LicenseAgreementFrm = this.fb.group({
      licenseId: [0],
      accountNumber: [''],
      accountTypeId: [0],
	  accountTypeName: [''],
      licenseeFirstName: [''],
      licenseeLastName: [''],
      licenseTypeId: [1],
	  licenseTypeName: [''],
      institutionName: [''],
      address1: [''],
      address2: [''],
      city: [''],
      stateId: [0],
	  stateName: [''],
      countryId: [0],
	  countryName: [''],
      zip: [''],
      emailId: [''],
      phone: [''],
      editionLoginArr: this.fb.array([]),
      editionLogins: ['']
    });
   
    this.GetLicenseType();  
    this.GetState();
    this.GetAccountType();
    this.GetCountry();
   
    this.licenseService.GetEditions()
      .subscribe(x => {
        this.lstEdition = x;
      }, error => this.error = <any>error);
	  
	  
    if (this.UserType <=2) {  
      this._loadingService.ShowLoading("global-loading");
      this.GetLicenseAccounts();
    }
	
    $('#accountSelect')
      .on('change', (e, args) => {
        var selectedValue = Number(args.selected);
        var selectedText= $(".chosen-single span" ).text();
        this.AccountNumberChanged(selectedValue,selectedText);
      });  

  }
  
  GetLicenseType() {
    this.licenseService.GetLicenceType()
      .subscribe(x => { 
	  this.lstLicenceType = x; 
	  }, error => this.error = <any>error);
  }

  GetAccountType() {
    this.licenseService.GetAccountType()
      .subscribe(x => { 
	  this.lstAccountType = x; 
	  }, error => this.error = <any>error);
  }
  
  GetState() {
    this.licenseService.GetState()
      .subscribe(st => { 
	  this.lstState = st; 
	  }, error => this.error = <any>error);
  }

  GetCountry() {
    this.licenseService.GetCountry()
      .subscribe(y => { 
	  this.lstCountry = y; 
	  
		  if (this.UserType > 2) {
			 //waiting for data
		    this.AccountNumberChanged(this.globalService.AccLicId,this.globalService.AccountNumber);
		  }
     
	  }, error => this.error = <any>error);
  }

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

  
  AccountNumberChanged(LicenseId: number,AccountNumber:string) {
	  
	this.LicenseAgreementFrm.reset();  
    if (LicenseId == 0) {     
      return;
    }
    this._loadingService.ShowLoading("global-loading");
	 
	this.license.LicenseId = LicenseId;
    this.GetLicenseById();
  }

  GetLicenseById() {
    if (this.license.LicenseId != 0) {
      this.licenseService.GetLicenseById(this.license.LicenseId)
        .subscribe(st => {
          this.license = st;
 
          this.LicenseAgreementFrm.controls['licenseId'].setValue(this.license.LicenseId);
          this.LicenseAgreementFrm.controls['licenseTypeId'].setValue(this.license.LicenseTypeId);
          this.LicenseAgreementFrm.controls['accountNumber'].setValue(this.license.AccountNumber);
          this.LicenseAgreementFrm.controls['accountTypeId'].setValue(this.license.AccountTypeId);
          this.LicenseAgreementFrm.controls['licenseeFirstName'].setValue(this.license.LicenseeFirstName);
          this.LicenseAgreementFrm.controls['licenseeLastName'].setValue(this.license.LicenseeLastName);
          this.LicenseAgreementFrm.controls['institutionName'].setValue(this.license.InstitutionName);
          this.LicenseAgreementFrm.controls['address1'].setValue(this.license.Address1);
          this.LicenseAgreementFrm.controls['address2'].setValue(this.license.Address2);
          this.LicenseAgreementFrm.controls['city'].setValue(this.license.City); 
          this.LicenseAgreementFrm.controls['stateId'].setValue(this.license.StateId);
          this.LicenseAgreementFrm.controls['countryId'].setValue(this.license.CountryId);
          this.LicenseAgreementFrm.controls['zip'].setValue(this.license.Zip);
          this.LicenseAgreementFrm.controls['emailId'].setValue(this.license.EmailId);
          this.LicenseAgreementFrm.controls['editionLogins'].setValue(this.license.EditionLogins);

          this.LicenseAgreementFrm.controls['phone'].setValue(this.license.Phone);
		  
		  var LicenseType= this.lstLicenceType.find(ft=>ft.Id==this.license.LicenseTypeId).Title;
		  var AccountType= this.lstAccountType.find(ft=>ft.Id==this.license.AccountTypeId).Title;
		  var state= this.lstState.find(ft=>ft.Id==this.license.StateId).StateName;
		  var country= this.lstCountry.find(ft=>ft.Id==this.license.CountryId).CountryName;
		  
		  this.LicenseAgreementFrm.controls['licenseTypeName'].setValue(LicenseType);
		  this.LicenseAgreementFrm.controls['accountTypeName'].setValue(AccountType);
		  this.LicenseAgreementFrm.controls['stateName'].setValue(state);
		  this.LicenseAgreementFrm.controls['countryName'].setValue(country);

          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]), Checked: 1 });
                    Check = true;
                  }
                });
                if (!Check) {
                  this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0, Checked: 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]), Checked: 1 });
                  Check = true;
                }
              });
              if (!Check) {
                this.lstEditionLogins.push({ Id: element.Id, Title: element.Title, Login: 0, Checked: 0 });
              }
            }
          });
          this.LicenseAgreementFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins));

          this._loadingService.HideLoading("global-loading");
        },
        error => this.error = <any>error);
    }
  }

  CloseLicense() {
    this.router.navigate(['/']);
  }

}