searchlicense.component.ts 8.32 KB
import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef } 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';

declare var $:any;

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

export class SearchLicense implements OnInit {

    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 = new Date();
    bsValue2: Date = new Date();
    selectedId: number = 0;
    modalRef: BsModalRef;
    checkedRecords: Array<number>;

    constructor(private licenseService: LicenseService, private router: Router, private activeRoute: ActivatedRoute, private fb: FormBuilder, private modalService: BsModalService) { }

    ngOnInit(): void
    {
        this.divClass = 'col-sm-12';
        this.license = new License();
        this.alerts = '';
        this.searchLicenseFrm = this.fb.group({
            accountNumber: [''],
            licenseeFirstName: [''],
            licenseeLastName: [''],
            licenseTypeId: [0],
            institutionName: [''],
            stateId: [0],
            countryId: [0],
            emailId: [''],
            subscriptionStartDate: [''],
            subscriptionEndDate: [''],  
            isActive: [false],
            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']),
                this.searchLicenseFrm.controls['isActive'].setValue(params['isActive'])
            }
         });
        this.SearchLicenses();
            $('#fixed_hdr2').fxdHdrCol({
                fixedCols: 0,
                width: "100%",
                height: 300,
                colModal: [
                { width: 180, align: 'center' },
                { width: 230, align: 'center' },
                { width: 150, align: 'Center' },
                { width: 150, align: 'Center' },
                { width: 350, align: 'Center' },
                { width: 500, align: 'Center' },
                { width: 130, align: 'Center' },
                { width: 120, align: 'center' },
                { width: 280, align: 'Center' },
                { width: 180, align: 'center' },
                { width: 200, align: 'center' },
                { width: 170, align: 'center' },
                { width: 80, align:  'center' },
                { width: 150, align: 'center' },
                { width: 150, align: 'center' },
                { width: 180, align: 'Center' },
                { width: 400, align: 'Center' },
                { width: 150, align: 'center' },
                { width: 110, align: 'center' },
                ],
                sort: true
            });
            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['LicenseId'];   
        this.license = item;
    }

    BindFormFields(data){
        this.selectedRow = 0;   
        this.licenses = data;
        this.license = this.licenses[0];
        this.selectedId = this.license.LicenseId; 
        this.activeRoute.queryParams.subscribe(params => {
            if(params['Id'] != null){
                this.selectedId = +params['Id']; 
                this.selectedRow =  +params['row'];
                this.license = this.searchLicenseFrm.controls['licenses'].value.filter(C => C.Id == this.selectedId);
            }
         });
        this.searchLicenseFrm.setControl('licenses', this.fb.array(this.licenses));
    }

    public SearchLicenses() {  
        this.licenseService.GetLicenses(this.searchLicenseFrm.value)
        .subscribe(x => { this.BindFormFields(x) }, error => this.error = error);    
    }

    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: 
            { 
                '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,
                'Id': this.selectedId,
                'row': this.selectedRow
            }
            , skipLocationChange: true 
        });
    }
    
    AfterDeleteData(data, template) {
        if (data.Status == "false") {
            this.alerts = "<span>License delete unsuccessfull</span>";
        } else {
            this.modalAlerts = "<p>License deleted successfully</p>";
            this.modalRef = this.modalService.show(template);
            this.SearchLicenses();
        }
    }

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