editlicensebasicsettings.component.ts 9.65 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 { BsDatepickerModule } from 'ngx-bootstrap';
import { Http, Response } from '@angular/http';
import { DatePipe } from '@angular/common';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { ContenteditableModelDirective } from '../../shared/contenteditabledirective'
import { ConfirmService } from '../../shared/confirm/confirm.service';
@Component({
  templateUrl: './editlicensebasicsettings.component.html'
})

export class EditLicenseBasicSettings implements OnInit {

  lstAccountNumbers: any;
  tempLstAccountNumbers: any;
  lstCountry: any;
  lstState: any;
  license: License;
  updateLicenseBasicSettingsFrm: FormGroup;
  error: any;
  alerts: string;
  modalAlerts: string;
  modalRef: BsModalRef;
  ConvertedPhoneno: string;
  MinusCharater: number;
  accountDropDownText: string;
  loopIdx1: number;
  loopIdx2: number;
  lastScrollPos: number;

  constructor(private licenseService: LicenseService,
    public globalService: GlobalService, private router: Router,
    private activeRoute: ActivatedRoute, private fb: FormBuilder,
    private modalService: BsModalService, private _confirmService: ConfirmService) { }

  ngOnInit(): void {
    this.license = new License();
    this.alerts = '';
    this.accountDropDownText = 'Select';
    this.loopIdx1 = 0;
    this.loopIdx2 = 0;
    this.lastScrollPos = 0;
    this.updateLicenseBasicSettingsFrm = this.fb.group({
      licenseId: [0],
      accountNumber: ['', Validators.required],
      licenseeFirstName: ['', Validators.required],
      licenseeLastName: ['', Validators.required],
      institutionName: ['', Validators.required],
      address1: ['', Validators.required],
      address2: [''],
      city: ['', Validators.required],
      stateId: [0],
      countryId: [0],
      zip: ['', Validators.required],
      emailId: ['', [Validators.required, Validators.email]],
      phone: ['', [Validators.required, Validators.pattern('^([0-9]{3})-([0-9]{3})-([0-9]{4})$')]],
    });
    this.GetCountry();
    this.GetState();
    this.GetLicenseAccounts();
  }

  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template);
  }

  GetCountry() {
    this.licenseService.GetCountry()
      .subscribe(y => { this.lstCountry = y; }, error => this.error = <any>error);
  }

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

  GetLicenseAccounts() {
    this.licenseService.GetLicenseAccounts(0)
      .subscribe(st => {
        this.lstAccountNumbers = st;
        this.tempLstAccountNumbers = [];
        this.loopIdx1 = 0;
        this.loopIdx2 = 0;
        this.lastScrollPos = 0;
        this.tempLstAccountNumbers = [];
        if (this.globalService.UserType > 2) {
          this.accountDropDownText = this.globalService.AccountNumber;
          this.tempLstAccountNumbers.push(this.accountDropDownText);
          this.AccountNumberChanged(this.globalService.AccLicId, this.accountDropDownText);
        }
        else {
          for (var i = 0; i < 50; i++) {
            this.tempLstAccountNumbers.push(this.lstAccountNumbers[this.loopIdx2++]);
          }
        }
      },
      error => this.error = <any>error);
  }

  GetLicenseById() {
    if (this.license.LicenseId != 0) {
      this.licenseService.GetLicenseById(this.license.LicenseId)
        .subscribe(st => {
          this.license = st;

          this.updateLicenseBasicSettingsFrm.controls['licenseId'].setValue(this.license.LicenseId);
          this.updateLicenseBasicSettingsFrm.controls['accountNumber'].setValue(this.license.AccountNumber);
          this.updateLicenseBasicSettingsFrm.controls['licenseeFirstName'].setValue(this.license.LicenseeFirstName);
          this.updateLicenseBasicSettingsFrm.controls['licenseeLastName'].setValue(this.license.LicenseeLastName);
          this.updateLicenseBasicSettingsFrm.controls['institutionName'].setValue(this.license.InstitutionName);
          this.updateLicenseBasicSettingsFrm.controls['address1'].setValue(this.license.Address1);
          this.updateLicenseBasicSettingsFrm.controls['address2'].setValue(this.license.Address2);
          this.updateLicenseBasicSettingsFrm.controls['city'].setValue(this.license.City);
          this.updateLicenseBasicSettingsFrm.controls['stateId'].setValue(this.license.StateId);
          this.updateLicenseBasicSettingsFrm.controls['countryId'].setValue(this.license.CountryId);
          this.updateLicenseBasicSettingsFrm.controls['zip'].setValue(this.license.Zip);
          this.updateLicenseBasicSettingsFrm.controls['emailId'].setValue(this.license.EmailId);
          //this.updateLicenseBasicSettingsFrm.controls['phone'].setValue(this.license.Phone);
          var str = this.license.Phone;
          var origPh = this.license.Phone;
          str = str.replace(" ", "");
          str = str.replace("-", "").replace("-", "");
          if (str.length >= 10) {
            origPh = str.substr(0, 3) + "-" + str.substr(3, 3) + "-" + str.substr(5, 4)
          }
          else {
            this.MinusCharater = 10 - str.length;
            this.AddZeroInPhoneNo(this.MinusCharater, str);
            origPh = this.ConvertedPhoneno.substr(0, 2) + "-" + this.ConvertedPhoneno.substr(2, 5) + "-" + this.ConvertedPhoneno.substr(5, 9)


          }
          this.updateLicenseBasicSettingsFrm.controls['phone'].setValue(origPh);
          //if (str.search("-") == -1) {
          //  alert(str);
          //}
          //else {
          //  var splitted = str.split("-");
          //  if (splitted.length >= 2) {

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

  AccountNumberChanged(LicenseId: number, SelectText: string) {
    this.accountDropDownText = SelectText;
    if (LicenseId == 0) {
      this.updateLicenseBasicSettingsFrm.reset();
      this.updateLicenseBasicSettingsFrm.controls['licenseId'].setValue(0);
      this.updateLicenseBasicSettingsFrm.controls['countryId'].setValue(0);
      this.updateLicenseBasicSettingsFrm.controls['stateId'].setValue(0);
      return;
    }
    this.license.LicenseId = LicenseId;
    this.GetLicenseById();
    return false;
  }

  onScroll($event) {
    if (this.globalService.loggedInUser.UserTypeId > 2) return;
    if (this.lastScrollPos > $event.target.scrollTop) {
      for (var i = 0; i < 1000; i++) {
        if (this.loopIdx1 > 0)
          this.tempLstAccountNumbers.unshift(this.lstAccountNumbers[this.loopIdx1--]);
      }
    }
    else {
      for (var i = 0; i < 1000; i++) {
        if (this.loopIdx2 < this.lstAccountNumbers.length)
          this.tempLstAccountNumbers.push(this.lstAccountNumbers[this.loopIdx2++]);
      }
    }
    this.lastScrollPos = $event.target.scrollTop;
  }

  onOpenChange(data: boolean): void {
    if (this.globalService.loggedInUser.UserTypeId > 2) return;
    if (!data) {
      this.loopIdx1 = 0;
      this.loopIdx2 = 0;
      this.tempLstAccountNumbers = [];
      for (var i = 0; i < 50; i++) {
        this.tempLstAccountNumbers.push(this.lstAccountNumbers[this.loopIdx2++]);
      }
    }
  }

  onKeyPress($event) {
    if (this.globalService.loggedInUser.UserTypeId > 2) return;
    let FindItem = this.lstAccountNumbers.find(C => (C.m_Item2.toLowerCase().indexOf($event.key) == 0));
    let prevIdx = 0;
    if (FindItem != null) {
      for (var i = 0; i < this.lstAccountNumbers.length; i++) {
        if (FindItem.m_Item1 == this.lstAccountNumbers[i].m_Item1) {
          prevIdx = i;
          break;
        }
      }
      this.tempLstAccountNumbers = [];
      this.loopIdx1 = prevIdx;
      this.loopIdx2 = prevIdx;
      for (var i = 0; i < 50; i++) {
        if (this.loopIdx2 < this.lstAccountNumbers.length)
          this.tempLstAccountNumbers.push(this.lstAccountNumbers[this.loopIdx2++]);
      }
      this.lastScrollPos = 0;
      $event.target.nextElementSibling.scrollTop = 0;
    }
  }

  AfterUpdateData(data, template) {
    if (data.Status == "false") {
      this.alerts = "<span>License profile update unsuccessfull.</span>";
    } else {
      //this.modalAlerts = "<p>License updated successfully</p>";
      this._confirmService.activate("License profile updated successfully.", "alertMsg");
      //  this.modalRef = this.modalService.show(template);
    }
  }

  UpdateLicenseBasicSettings(template: TemplateRef<any>) {
    this.alerts = '';
    if (this.alerts == '') {
      var obj = this.updateLicenseBasicSettingsFrm.value;
      return this.licenseService.UpdateLicenseBasicSettings(obj)
        .subscribe(
        n => (this.AfterUpdateData(n, template)),
        error => this.error = <any>error);
    }

  }
  AddZeroInPhoneNo(num: number, phone: string) {
    if (num = 1) { this.ConvertedPhoneno = phone + "0"; }
    if (num = 2) { this.ConvertedPhoneno = phone + "00"; }
    if (num = 3) { this.ConvertedPhoneno = phone + "000"; }
    if (num = 4) { this.ConvertedPhoneno = phone + "0000"; }
    if (num = 5) { this.ConvertedPhoneno = phone + "00000"; }
    if (num = 6) { this.ConvertedPhoneno = phone + "000000"; }
    if (num = 7) { this.ConvertedPhoneno = phone + "0000000"; }
    if (num = 8) { this.ConvertedPhoneno = phone + "00000000"; }
    if (num = 9) { this.ConvertedPhoneno = phone + "000000000"; }
    if (num = 10) { this.ConvertedPhoneno = phone + "0000000000"; }


  }

}