searchlicense.component.ts 12.9 KB
import { Component, OnInit, AfterViewChecked, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef, ViewChild,HostListener } 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, BsDatepickerConfig } 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: './searchlicense.component.html'
})

export class SearchLicense implements OnInit, AfterViewChecked {

  public lstLicenceType: any;
  public lstCountry: any;
  public lstState: any;
  public lstLicense: any;
  mode: string = 'Search';
  license: License;
  licenses: Array<any>;
  searchLicenseFrm: FormGroup;
  insertUpdateLicenseFrm: FormGroup;
  error: any;
  alerts: string;
  modalAlerts: string;
  divClass: string = '';
  topPos: string = '2000px';
  selectedRow: number = 0;
  datePipe: DatePipe = new DatePipe('en-US');
  bsValue1: Date = null;//new Date();
  bsValue2: Date = null;//new Date();
  selectedId: number = 0;
  modalRef: BsModalRef;
  checkedRecords: Array<number>;
  minDate = new Date(1110, 11, 1);
  maxDate = new Date(9999, 11, 31);
  bsConfig: Partial<BsDatepickerConfig>;
  NoRecord: string;
  @ViewChild(PagerComponent) pagerComponent: PagerComponent;
  recordCount: number;
  pageNo: number;
  pageLength: number;
  returnFrom: boolean;
  dateStartInvalid: boolean = false;
  dateEndInvalid: boolean = false;
  tempSearchParams: any;
  // Declare height and width variables
  scrHeight:any;
  scrWidth:any;
  fieldName:string;
  sortType:string;
  @HostListener('window:resize', ['$event'])
  getScreenSize(event?) {

   var $ua = navigator.userAgent;
   if (($ua.match(/(iPod|iPhone|iPad|android)/i))) {
     this.scrHeight = window.innerHeight-515;
   }
   else
   {
     this.scrHeight = window.innerHeight-460;
   }
   
  // $(".ft_container").css("height",this.scrHeight)
  }
  constructor(private licenseService: LicenseService, private router: Router,
    private activeRoute: ActivatedRoute, private fb: FormBuilder,
    private modalService: BsModalService, public global: GlobalService,
    private _loadingService: LoadingService, private _confirmService: ConfirmService) { this.getScreenSize();}

  ngOnInit(): void {
    this._loadingService.ShowLoading("global-loading");
    this.bsConfig = Object.assign({}, { containerClass: 'theme-dark-blue' });
    this.divClass = 'col-sm-12';
    this.license = new License();
    this.alerts = '';
    this.NoRecord = this.global.NoRecords;
    this.searchLicenseFrm = this.fb.group({
      accountNumber: [''],
      licenseeFirstName: [''],
      licenseeLastName: [''],
      licenseTypeId: [0],
      institutionName: [''],
      stateId: [0],
      countryId: [0],
      emailId: ['', Validators.email],
      subscriptionStartDate: [''],
      subscriptionEndDate: [''],
      isActive: [true],
      licenses: this.fb.array([]),
    });
    this.GetLicenseType();
    this.GetCountry();
    this.GetState();
    this.activeRoute.queryParams.subscribe(params => {
      if (params['Id'] != null) {
        this.searchLicenseFrm.controls['accountNumber'].setValue(params['accountNumber']);
        this.searchLicenseFrm.controls['licenseeFirstName'].setValue(params['licenseeFirstName']);
        this.searchLicenseFrm.controls['licenseeLastName'].setValue(params['licenseeLastName']);
        this.searchLicenseFrm.controls['licenseTypeId'].setValue(+params['licenseTypeId']);
        this.searchLicenseFrm.controls['institutionName'].setValue(params['institutionName']);
        this.searchLicenseFrm.controls['stateId'].setValue(+params['stateId']);
        this.searchLicenseFrm.controls['countryId'].setValue(+params['countryId']);
        this.searchLicenseFrm.controls['subscriptionStartDate'].setValue(params['subscriptionStartDate']);
        this.searchLicenseFrm.controls['subscriptionEndDate'].setValue(params['subscriptionEndDate']);
        if(params['isActive'] == 'true'){
          this.searchLicenseFrm.controls['isActive'].setValue(true);
        }
        else{
          this.searchLicenseFrm.controls['isActive'].setValue(false);
        }
        this.searchLicenseFrm.controls['emailId'].setValue(params['emailId']);
        this.selectedId = +params['Id'];
        this.selectedRow = +params['row'];
        this.pageNo = params['pageNo'];
        this.pageLength = params['pageLength'];
        this.returnFrom = true;
      }
      else {
        this.selectedRow = -1;
        this.selectedId = -1;
        this.pageNo = 1;
        this.pageLength = 10;
        this.returnFrom = false;
      }
    });
    this.NoRecord = this.global.NoRecords;
    this.recordCount = 0;
    this.pagerComponent = new PagerComponent();
    $('#fixed_hdr2').fxdHdrCol({
      fixedCols: 0,
      width: "100%",
      height: this.scrHeight,
      colModal: [
        { width: 150, align: 'center' },
        { width: 150, align: 'center' },
        { width: 150, align: 'Center' },
        { width: 150, align: 'Center' },
        { width: 150, align: 'Center' },
        { width: 150, align: 'Center' },
        { width: 150, align: 'Center' },
        { width: 150, align: 'center' },
        { width: 150, align: 'Center' },
        { width: 200, align: 'center' },
        { width: 200, align: 'center' },
        { width: 200, align: 'center' },
        { width: 150, align: 'center' },
        { width: 200, align: 'center' },
        { width: 200, align: 'center' },
        { width: 200, align: 'Center' },
        { width: 150, align: 'Center' },
        { width: 150, align: 'center' },
        { width: 150, align: 'center' },
      ],
      sort: true
    });
    if ($('fixed_table_rc').length > 0) {
      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);
    }

   // $("#fixed_hdr2 thead tr th").unbind("click");
    // $(".ft_rwrapper table thead tr th").on("click",function(event){  
           
    //   this.fieldName=event.currentTarget.id;
    //   var isAscSort = $(".ft_rwrapper table thead tr #"+this.fieldName).hasClass('fx_sort_asc'); 
    //   $(".ft_rwrapper table thead tr th").removeClass('fx_sort_asc fx_sort_desc'); 
		
    //   if (isAscSort) { 
    //     $(".ft_rwrapper table thead tr #"+this.fieldName).addClass('fx_sort_desc').removeClass('fx_sort_asc'); 
    //     this.sortType="desc";
    //   } else { 
    //     $(".ft_rwrapper table thead tr #"+this.fieldName).addClass('fx_sort_asc').removeClass('fx_sort_desc'); 
    //     this.sortType="asc";
    //   }

    //   });
    
  }

  ngAfterViewChecked() {
    $('#fixed_hdr2 thead').css('width', $('#fixed_hdr2 tbody tr:eq(0)').width());
  }

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

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

  BindFormFields(data) {
    this.recordCount = data.RecordCount;
    this.licenses = data.LicenseList;
    this.license = this.licenses[0];
    this.searchLicenseFrm.setControl('licenses', this.fb.array(this.licenses));
    if(this.selectedId > -1){
      this.license = this.searchLicenseFrm.controls['licenses'].value.find(C => C.LicenseId == this.selectedId);
    }
    if (this.licenses.length > 0) {
      this.NoRecord = '';
      this._loadingService.HideLoading("global-loading");
    }
    if (this.licenses.length == 0) {
      this.NoRecord = this.global.NoRecords;
      this._loadingService.HideLoading("global-loading");
    }
  }

  DateChange(dateValue: any) {
    this.alerts = '';
    if (this.searchLicenseFrm.dirty) {
      if (dateValue._datepicker._elementRef.nativeElement.id == 'SubscriptionStartDate') {
        if (dateValue._bsValue == null) {
          if (dateValue._datepicker._elementRef.nativeElement.value != '') {
            this.dateStartInvalid = true;
          }
          else {
            this.dateStartInvalid = false;
          }
        }
        else {
          this.dateStartInvalid = false;
          this.searchLicenseFrm.controls['subscriptionStartDate'].setValue(dateValue._bsValue);
        }
      }

      if (dateValue._datepicker._elementRef.nativeElement.id == 'SubscriptionEndDate') {
        if (dateValue._bsValue == null) {
          if (dateValue._datepicker._elementRef.nativeElement.value != '') {
            this.dateEndInvalid = true;
          }
          else {
            this.dateEndInvalid = false;
          }
        }
        else {
          this.dateEndInvalid = false;
          this.searchLicenseFrm.controls['subscriptionEndDate'].setValue(dateValue._bsValue);
        }
      }

      if (!this.dateStartInvalid && !this.dateEndInvalid) {
        if (Date.parse(this.searchLicenseFrm.controls['subscriptionStartDate'].value) >
          Date.parse(this.searchLicenseFrm.controls['subscriptionEndDate'].value)) {
          this.alerts = '<span>Subscription start date must be less than the subscription end date</span><br/>';
        }
      }
    }

  }

  public SearchLicenses(evt: any) {
    if (this.alerts != '') return;
    if (!this.returnFrom) {
      this.selectedRow = -1;
      this.selectedId = -1;
    }
    if (this.returnFrom) this.returnFrom = false;
    this._loadingService.ShowLoading("global-loading");
    var tempArr = evt.split(',');
    this.pageNo = parseInt(tempArr[0]);
    this.pageLength = parseInt(tempArr[1]);
    this.licenseService.GetLicenses(this.searchLicenseFrm.value, this.pageNo, this.pageLength)
      .subscribe(x => { this.BindFormFields(x);
        this.tempSearchParams = {
          'accountNumber': this.searchLicenseFrm.controls['accountNumber'].value,
          'licenseeFirstName': this.searchLicenseFrm.controls['licenseeFirstName'].value,
          'licenseeLastName': this.searchLicenseFrm.controls['licenseeLastName'].value,
          'licenseTypeId': this.searchLicenseFrm.controls['licenseTypeId'].value,
          'institutionName': this.searchLicenseFrm.controls['institutionName'].value,
          'stateId': this.searchLicenseFrm.controls['stateId'].value,
          'countryId': this.searchLicenseFrm.controls['countryId'].value,
          'subscriptionStartDate': this.searchLicenseFrm.controls['subscriptionStartDate'].value,
          'subscriptionEndDate': this.searchLicenseFrm.controls['subscriptionEndDate'].value,
          'isActive': this.searchLicenseFrm.controls['isActive'].value,
          'emailId': this.searchLicenseFrm.controls['emailId'].value,
          'Id': this.selectedId,
          'row': this.selectedRow,
          'pageNo': this.pageNo,
          'pageLength': this.pageLength
        };
        this._loadingService.HideLoading("global-loading");

      }, error => this.error = error);
  }

  SearchRecords() {
    this.SearchLicenses('1, ' + this.pageLength);
  }

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

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

  
EditLicense() {
  this.router.navigate(['/editlicense'],
    {
      queryParams: this.tempSearchParams
      , skipLocationChange: true
    });
}

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

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