licensemodestysettings.component.ts 7.16 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: './licensemodestysettings.component.html'
})

export class LicenseModestySettings implements OnInit {

  lstAccountNumbers: any;
  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;
  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.license.LicenseId = 0;
    this.alerts = '';
    this.accountDropDownText = 'Select';
    this.loopIdx1 = 0;
    this.loopIdx2 = 0;
    this.lastScrollPos = 0;
    this.updateModestySettingsFrm = this.fb.group({
      licenseId: [0],
      accountNumber: ['', Validators.required],
      siteId: [0],
      lstModesty: [this.fb.array([])],
    });
    this.GetLicenseAccounts();
  }

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

  GetLicenseAccounts() {
    this.licenseService.GetLicenseAccounts(0)
      .subscribe(st => {
        this.lstAccountNumbers = st;
        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);
  }

  ShowModestyorSites(template: TemplateRef<any>) {
    this.lstLicenseEditionModesty = null;
    this.lstLicenseSites = null;
    this.selectedSiteId = 0;
    if (!this.isBuildingLevel) {
      this.GetLicenseEditionModesty();
    }
    else {
      this.licenseService.GetLicenseSites(this.license.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);
          }
        }, error => this.error = <any>error);
    }
  }

  GetLicenseEditionModesty() {
    this.licenseService.GetLicenseModestySettings(this.license.LicenseId, this.selectedSiteId)
      .subscribe(st => {
        this.lstLicenseEditionModesty = st;
        this.updateModestySettingsFrm.setControl('lstModesty', this.fb.array(this.lstLicenseEditionModesty));
      }, error => this.error = <any>error);
  }

  LicenseSiteChanged(siteId: number) {
    this.selectedSiteId = siteId;
    if (this.selectedSiteId == 0) {
      this.lstLicenseEditionModesty = null;
      return;
    }
    this.GetLicenseEditionModesty();
  }

  GetLicenseById() {
    if (this.license.LicenseId != 0) {
      this.licenseService.GetLicenseById(this.license.LicenseId)
        .subscribe(st => {
          this.license = st;
          this.updateModestySettingsFrm.controls['licenseId'].setValue(this.license.LicenseId);
          this.updateModestySettingsFrm.controls['accountNumber'].setValue(this.license.AccountNumber);
        },
        error => this.error = <any>error);
    }
  }

  AccountNumberChanged(LicenseId: number, SelectText: string) {
    this.accountDropDownText = SelectText;
    this.license.LicenseId = LicenseId;
    this.lstLicenseEditionModesty = null;
    this.lstLicenseSites = null;
    this.selectedSiteId = 0;
    this.isBuildingLevel = false;
    this.GetLicenseById();
    return false;
  }

  onScroll($event) {
    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 (!data) {
      this.loopIdx1 = 0;
      this.loopIdx2 = 0;
      this.tempLstAccountNumbers = [];
      this.lastScrollPos = 0;
      for (var i = 0; i < 50; i++) {
        this.tempLstAccountNumbers.push(this.lstAccountNumbers[this.loopIdx2++]);
      }
    }
  }

  onKeyPress($event) {
    let FindItem = this.lstAccountNumbers.find(C => (C.m_Item2.toLowerCase().indexOf($event.key) == 0));
    let prevIdx = 0;
    if (FindItem != null) {
      this.tempLstAccountNumbers = [];
      for (var i = 0; i < this.lstAccountNumbers.length; i++) {
        if (FindItem.m_Item1 == this.lstAccountNumbers[i].m_Item1) {
          prevIdx = i;
          break;
        }
      }
      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 modesty settings update unsuccessfull.</span>";
    } else {
      this._confirmService.activate("License modesty settings updated successfully.", "alertMsg");
      // this.modalAlerts = "<p>License modesty setings updated successfully.</p>";
      // this.modalRef = this.modalService.show(template);
    }
  }

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


}