licensemodulesettings.component.ts 7.97 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='';
  aodModuleStatus: boolean;
  aodAllCourseList=[];
  dropdownList = [];
  selectedItems = [];
  dropdownSettings = {};

  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.aodModuleStatus=false;
    
    this._loadingService.ShowLoading("global-loading");
    $('#accountSelect').chosen({allow_single_deselect:true,width:'250px',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([])],
      courselist: []
    });

    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;
       var aod= st.filter(item => item.m_Item1 ==17);  //aod module
       if(aod[0].m_Item2)
       {
        this.aodModuleStatus=true;
       }

        this.updateModuleSettingsFrm.setControl('lstModuleStatus', this.fb.array(this.lstModuleStatus));
        this._loadingService.HideLoading("global-loading");

        if(this.aodModuleStatus)
        {
          this.LoadAODCourseList();
        }  
        
      }, 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.aodModuleStatus=false;
    this.aodAllCourseList=[];

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

  }

  LoadAODCourseList()
  {
    this._loadingService.ShowLoading("global-loading");
    this.aodAllCourseList=[];
    this.dropdownList = [];
    this.selectedItems = [];
    this.dropdownSettings = { 
      singleSelection: false, 
      text:"AOD Courses List",
      selectAllText:'Select All',
      unSelectAllText:'UnSelect All',
      enableSearchFilter: true,
      maxHeight:200,
      classes:"myclass custom-class"
    };  

    this.licenseService.GetLicenseAodCourse(this.LicenseId)
      .subscribe(courselist => {
        this.aodAllCourseList = courselist;
       var selectedCourse= courselist.filter(item => item.Status ==true);  //aod module
      
      for(var i=0;i<this.aodAllCourseList.length;i++)
      {
        this.dropdownList.push(
          {"id":this.aodAllCourseList[i].CourseId.trim(),
           "itemName":this.aodAllCourseList[i].CourseName
          })
      }
      for(var i=0;i<selectedCourse.length;i++)
      {
        this.selectedItems.push(
          {"id":selectedCourse[i].CourseId.trim(),
           "itemName":selectedCourse[i].CourseName
          })
      }

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

  }
  updateAodCourseItemList(ischached:any,id:any)
  {
    let itemIndex = this.aodAllCourseList.findIndex(item => item.CourseId ==id);
    this.aodAllCourseList[itemIndex].Status = ischached;
  }
  onItemSelect(item:any){
   // console.log(item);
   // console.log(this.selectedItems);
    this.updateAodCourseItemList(true,item.id);
  }
  OnItemDeSelect(item:any){
     // console.log(item);
      this.updateAodCourseItemList(false,item.id);
  }
  onSelectAll(items: any){
      //console.log(items);
      //console.log(this.selectedItems);
      for(var ind=0;ind<items.length;ind++)
      {
        this.updateAodCourseItemList(true,items[ind].id);
      }
     
  }
  onDeSelectAll(items: any){
      //console.log(items);
      //console.log(this.selectedItems);
     // console.log(this.dropdownList);
      for(var ind=0;ind<this.dropdownList.length;ind++)
      {
        this.updateAodCourseItemList(false,this.dropdownList[ind].id);
      }
  }
  handleChange(evmoduleName,moduleValue) {
    if (evmoduleName=='A.D.A.M. OnDemand') {
        if(moduleValue==true)
        {
          this.aodModuleStatus=true;
          if(this.aodAllCourseList.length==0)
          {
            this.LoadAODCourseList();
          }
        }
        else
        {
          this.aodModuleStatus=false;
        }
      }
  }

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

  ErrorUpdateData(Error:any, template) {
      this._confirmService.activate("License module update failed.", "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,this.aodModuleStatus,this.aodAllCourseList)
        .subscribe(
        n =>{
          this.AfterUpdateData(n, template)
        },
        error =>{
          this.ErrorUpdateData(error, template)
          });
    }
  }


}