sitelicenseaccount.component.ts 15.9 KB
import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef, ViewChild } from '@angular/core';
import { LicenseService } from './license.service';
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 { PagerComponent } from '../../shared/Pager/pager.component';
import { LoadingService } from '../../shared/loading.service';
import { GlobalService } from '../../Shared/global';
import { ConfirmService } from '../../Shared/Confirm/confirm.service';
declare var $:any;

@Component({
    templateUrl: './sitelicenseaccount.component.html' 
})

export class SiteLicenseAccount implements OnInit {

    lstCountry: any;
    lstState: any;
    lstAccountNumbers: any;
    lstLicenseSites: any;
    licenseSite: any;
    mode: string = 'Search';
    license: License;
    insertUpdateSiteLicenseFrm: FormGroup;
    error: any;
    alerts: string;
    modalAlerts: string;
    divClass: string = '';
    topPos: string = '2000px';
    selectedRow: number = -1;
    selectedId: number = -1;
    modalRef: BsModalRef;
    lstEdition: any;
    lstEditionLogins: any;
    lstSiteAccountEditions: any;
    lstClientAdmin: any;
    NoRecord: string;
    @ViewChild(PagerComponent) pagerComponent: PagerComponent;
    recordCount: number;
    pageNo: number;
    pageLength: number;
  
    constructor(private licenseService: LicenseService,
      private router: Router, private activeRoute: ActivatedRoute,
      private fb: FormBuilder, private modalService: BsModalService,
      private global: GlobalService, private _loadingService: LoadingService, 
      private _confirmService: ConfirmService) { }

    ngOnInit(): void
    {
        this.divClass = 'col-sm-12';
        this.license = new License();
        this.license.LicenseId = 0;
        this.alerts = '';
        this.insertUpdateSiteLicenseFrm = this.fb.group({
            licenseId: [0],
            accountNumber: [{value: '', disabled: true}],
            siteId: [0],
            siteUrl: ['', Validators.required],
            siteUrlTo: [''],
            siteMasterUrlTo: [''],
            buildAccName: [''],
            institutionName: ['', Validators.required],
            departmentName: [''],
            address1: ['', Validators.required],
            address2: [''],
            city: ['', Validators.required],
            countryId: [0, Validators.min(1)],
            stateId: [0, Validators.min(1)],
            zip: ['', Validators.required],
            phone: ['', Validators.required],
            clientAdminId: [0, Validators.min(1)],
            clientAdminEmail: [{value: '', disabled: true}, Validators.email],
            isActive: [0],
            isMaster: [0],
            creationDate: [''],
            modifiedDate: [''],
            editionLoginArr: this.fb.array([]),
        });
        this.GetCountry();
        this.GetState();
        this.GetEditions();
        this.GetLicenseAccounts();
        $('#fixed_hdr2').fxdHdrCol({
			fixedCols: 0,
			width: "100%",
			height: 330,
			colModal: [
			{ width: 200, align: 'center' },
			{ width: 200, align: 'center' },
			{ width: 200, align: 'Center' },
			{ width: 200, align: 'Center' },
			{ width: 250, align: 'Center' },
			{ width: 200, align: 'Center' },
			{ width: 200, align: 'Center' },
			{ width: 200, align: 'Center' },
			{ width: 200, align: 'Center' },
			],
			sort: true
		});
        this.selectedRow = -1;
        this.selectedId = -1;
        this.pageNo = 1;
        this.pageLength = 10;
        this.NoRecord = this.global.NoRecords;
        this.recordCount = 0;
        this.pagerComponent = new PagerComponent();

        if(document.getElementById("fixed_table_rc") != null){
            document.getElementById("fixed_table_rc").remove();
            var testScript = document.createElement("script");
            testScript.setAttribute("id", "fixed_table_rc");
            testScript.setAttribute("src", "../assets/scripts/fixed_table_rc.js");
            testScript.setAttribute("type", "text/javascript");
            document.body.appendChild(testScript);
        }
    }

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

    public SetClickedRow(i: number, item: any) {
        this.selectedRow = i;
        this.selectedId = item['Id'];   
        this.licenseSite = item;
    }

    BindFormFields(data){
        this.recordCount = data.RecordCount;
        this.lstLicenseSites = data.LicenseSiteList;
        if(this.selectedRow > -1){
            this.selectedId = this.lstLicenseSites[this.selectedRow].siteId; 
            this.SetClickedRow(this.selectedRow, this.lstLicenseSites[this.selectedRow]);
        }
        this.insertUpdateSiteLicenseFrm.controls['clientAdminId'].setValue(this.lstLicenseSites[0].SiteUserId);
        this.insertUpdateSiteLicenseFrm.controls['clientAdminEmail'].setValue(this.lstLicenseSites[0].SiteUserEmailId);
        var tempArr = [];
        tempArr.push(
            {
                "Id": this.lstLicenseSites[0].SiteUserId, 
                "Name": this.lstLicenseSites[0].SiteUserFirstName
        });
        this.lstClientAdmin = tempArr;
        if (this.lstLicenseSites.length > 0) {
            this.NoRecord = '';
            this._loadingService.HideLoading("global-loading");
        }
        if (this.lstLicenseSites.length == 0) {
            this.NoRecord = this.global.NoRecords;
            this._loadingService.HideLoading("global-loading");
        }
    }

    GetLicenseAccounts() { 
        this.licenseService.GetLicenseAccounts(3)
        .subscribe(st => { this.lstAccountNumbers = st; }, error => this.error = <any>error); 
    }

    AccountNumberChanged(LicenseId: number){
        this.selectedRow = -1;
        this.license.LicenseId = LicenseId;
        this.lstLicenseSites = null;
        if(this.license.LicenseId == 0){
            return;
        }
        this.GetLicenseById();
    }

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

    GetSiteById(){
        this.licenseService.GetSiteById(this.selectedId)
        .subscribe(st => { 
            this.licenseSite = st;
            this.insertUpdateSiteLicenseFrm.controls['siteId'].setValue(this.licenseSite.Id);
            this.insertUpdateSiteLicenseFrm.controls['buildAccName'].setValue(this.licenseSite.Title);
            this.insertUpdateSiteLicenseFrm.controls['siteUrl'].setValue(this.licenseSite.Ip);
            this.insertUpdateSiteLicenseFrm.controls['siteUrlTo'].setValue(this.licenseSite.SiteIpTo);
            this.insertUpdateSiteLicenseFrm.controls['siteMasterUrlTo'].setValue(this.licenseSite.MasterIpTo);
            this.insertUpdateSiteLicenseFrm.controls['institutionName'].setValue(this.licenseSite.InstituteName);
            this.insertUpdateSiteLicenseFrm.controls['departmentName'].setValue(this.licenseSite.Department);
            this.insertUpdateSiteLicenseFrm.controls['address1'].setValue(this.licenseSite.Address1);
            this.insertUpdateSiteLicenseFrm.controls['address2'].setValue(this.licenseSite.Address2);
            this.insertUpdateSiteLicenseFrm.controls['city'].setValue(this.licenseSite.City);
            this.insertUpdateSiteLicenseFrm.controls['phone'].setValue(this.licenseSite.Phone);
            this.insertUpdateSiteLicenseFrm.controls['zip'].setValue(this.licenseSite.Zip);
            this.insertUpdateSiteLicenseFrm.controls['countryId'].setValue(this.licenseSite.CountryId);
            this.insertUpdateSiteLicenseFrm.controls['stateId'].setValue(this.licenseSite.StateId);
            this.insertUpdateSiteLicenseFrm.controls['isActive'].setValue(this.licenseSite.IsActive);
            this.insertUpdateSiteLicenseFrm.controls['isMaster'].setValue(this.licenseSite.IsMaster);
            this.insertUpdateSiteLicenseFrm.controls['creationDate'].setValue(this.licenseSite.CreationDate);
            this.insertUpdateSiteLicenseFrm.controls['modifiedDate'].setValue(this.licenseSite.ModifiedDate);
            this.insertUpdateSiteLicenseFrm.controls['clientAdminId'].setValue(this.lstLicenseSites[0].SiteUserId);
            this.GetSiteAccountEditions();
         }, error => this.error = <any>error); 
    }

    GetSiteAccountEditions(){
        this.licenseService.GetSiteAccountEditions(this.licenseSite.Id, this.license.LicenseId)
        .subscribe(st => { 
            this.lstSiteAccountEditions = st; 
            this.lstEditionLogins.forEach(element => {
                this.lstSiteAccountEditions.forEach(elm => {
                    if(elm.m_Item2 == element.Id){
                        element.Login = 1;
                    }
                });
            });
            this.insertUpdateSiteLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins));
        }, error => this.error = <any>error); 
    }

    GetLicenseSites(evt: any) {
        if (this.alerts != '') return;
        if(this.license.LicenseId == 0){
            return;
        }
        this._loadingService.ShowLoading("global-loading");
        var tempArr = evt.split(',');
        this.pageNo = parseInt(tempArr[0]);
        this.pageLength = parseInt(tempArr[1]);
        this._loadingService.ShowLoading("global-loading");
        this.licenseService.GetLicenseSites(this.license.AccountNumber, this.pageNo, this.pageLength)
          .subscribe(x => { this.BindFormFields(x) }, error => this.error = error);
    }

    SearchRecords() {
        this.selectedRow = -1;
        this.selectedId = -1;
        this.GetLicenseSites('1, ' + this.pageLength);
    }

    GetEditions() {
        this.licenseService.GetEditions()
        .subscribe(x => { 
            this.lstEdition = x; 
        }, error => this.error = <any>error);
    }

    onChange(item: any, isChecked: boolean){
        if(this.license.LicenseTypeId == 3){
            if(isChecked){
                item.Login = 1;
            }
            else{
                item.Login = 0;
            }
        }
    }

    GetLicenseById() { 
        if(this.license.LicenseId != 0)
        {
            this.licenseService.GetLicenseById(this.license.LicenseId)
            .subscribe(st => { 
                this.license = st; 
                this.insertUpdateSiteLicenseFrm.controls['licenseId'].setValue(this.license.LicenseId);
                this.insertUpdateSiteLicenseFrm.controls['accountNumber'].setValue(this.license.AccountNumber);
                if(this.license.EditionLogins == null) return;
                var TempArr = this.license.EditionLogins.split('|');
                this.lstEditionLogins = new Array();
                this.lstEdition.forEach(element => {
                    TempArr.forEach(elm => {
                        var TempInnerArr = elm.split('-');
                        if(TempInnerArr[0] == element.Id){
                            this.lstEditionLogins.push({Id: element.Id, Title: element.Title, Login: 0});
                        }
                    });
                });
                this.insertUpdateSiteLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins));
            }, 
            error => this.error = <any>error); 
        }
    }
    
    AfterDeleteData(data, template) {
        if (data.Status == "false") {
            this.alerts = "<span>Site account delete unsuccessfull</span>";
        } else {
          this._confirmService.activate("Site account deleted successfully.", "alertMsg");
            //this.modalAlerts = "<p>Site account deleted successfully</p>";
            //this.modalRef = this.modalService.show(template);
            this.SearchRecords();
        }
    }

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

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

    InsertUpdateSiteAccount(template: TemplateRef<any>) {
        this.alerts = '';
        var obj = this.insertUpdateSiteLicenseFrm.value;
        var temptext = '';
        obj.editionLoginArr.forEach(element => {
            if(element.Login == 1){
                temptext += element.Id + ',';
            }
        });
        if(temptext == ''){
            this.alerts = "<span>Please select a product edition</span>";
        }
        if(this.alerts == ''){
                return this.licenseService.InsertUpdateSiteAccount(obj)
                .subscribe(
                n => (this.AfterInsertData(n, template)),
                error => this.error = <any>error);
        }
    }

    DeleteSiteAccount(template: TemplateRef<any>){
        this.modalRef.hide();
        this.alerts = '';
        if(this.alerts == ''){
            var obj = this.licenseSite;
            obj.LicenseId = this.license.LicenseId;
            return this.licenseService.DeleteSiteAccount(obj)
                .subscribe(
                data => (this.AfterDeleteData(data, template)),
                error => {
                    this.error = <any>error; 
                    this.alerts = "<span>" + this.error + "</span>";
                });
        }
    }

    AddLicenseSite(template: TemplateRef<any>){
        this.mode = 'Add';
        this.topPos = '100px';
        this.alerts = '';
        this.insertUpdateSiteLicenseFrm.controls['siteId'].setValue(0);
        this.insertUpdateSiteLicenseFrm.controls['buildAccName'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['siteUrl'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['siteUrlTo'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['institutionName'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['departmentName'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['address1'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['address2'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['city'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['phone'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['zip'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['countryId'].setValue(0);
        this.insertUpdateSiteLicenseFrm.controls['stateId'].setValue(0);
        this.insertUpdateSiteLicenseFrm.controls['isActive'].setValue(1);
        this.insertUpdateSiteLicenseFrm.controls['isMaster'].setValue(0);
        this.insertUpdateSiteLicenseFrm.controls['creationDate'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['modifiedDate'].setValue('');
        this.insertUpdateSiteLicenseFrm.controls['clientAdminId'].setValue(0);
}
    
    EditLicenseSite(template: TemplateRef<any>){
        this.mode = 'Edit';
        this.topPos = '100px';
        this.alerts = '';
        this.GetSiteById();
    }
    
    CancelAddEdit(){
        this.mode = 'Search';
        this.topPos = '2000px';
        this.GetLicenseSites(this.pageNo + ', ' + this.pageLength);
    }
}