subscriptionprice.component.ts 7 KB
import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, OnChanges, 
    SimpleChanges, TemplateRef, Pipe, PipeTransform } from '@angular/core';
import { SubscriptionPriceService } from './subscriptionprice.service';
import { Router } from '@angular/router';
import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { SubscriptionPriceModel }  from '../UserEntity/datamodel';
import { BsDatepickerModule } from 'ngx-bootstrap';
import { Http, Response } from '@angular/http';
import { DatePipe } from '@angular/common';
import { ContenteditableModelDirective } from '../../shared/contenteditabledirective'
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
//import { Global } from '../../Shared/global';
//import { DBOperation } from 'S';
//import { Observable } from 'rxjs/Observable';

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

export class SubscriptionPrice implements OnInit {

Mode: string = 'Search';
subscriptionPrice: SubscriptionPriceModel;
subscriptionPrices: Array<SubscriptionPriceModel>;
subscriptionPriceFrm: FormGroup;
insertSubscriptionPriceFrm: FormGroup;
checkedRecords: Array<number>;
error: any;
alerts: string;
modalAlerts: string;
topPos: string = '2000px';
datePipe: DatePipe = new DatePipe('en-US');
selectedEditionId: number = 1;
divClass: string = '';
modalRef: BsModalRef;

constructor(private subscriptionPriceService: SubscriptionPriceService, private router: Router, private fb: FormBuilder, private modalService: BsModalService) { }
  
    ngOnInit(): void {
        this.divClass = 'col-sm-12';
        this.subscriptionPrice = new SubscriptionPriceModel();
        this.alerts = '';
        this.subscriptionPriceFrm = this.fb.group({
            subscriptionPrices: this.fb.array([])
        });
        this.insertSubscriptionPriceFrm = this.fb.group({
            subscriptionPriceId: [''],
            title: ['', Validators.required],
            price: ['', [Validators.required, Validators.pattern('[0-9.]*')]],
            duration: ['', [Validators.required, Validators.pattern('[0-9]*')]],
            editionId: [''],
            isActive: [false]
        });
        this.SearchSubscriptionPrices(this.selectedEditionId);
    }

    public SearchSubscriptionPrices(searchEditionId: number) { 
        this.selectedEditionId = searchEditionId;
        this.subscriptionPriceService.GetSubscriptionPrices(
            {
                editionId: this.selectedEditionId, 
            })
        .subscribe(x => { this.BindFormFields(x) }, error => this.error = error);    
    }

    openModal(template: TemplateRef<any>) {
        if(this.checkedRecords.filter(C => C > 0).length == 0) return;
        this.modalRef = this.modalService.show(template);
    }

    onChange(Idx: number, Id: number, isChecked: boolean){
        if(isChecked){
            this.checkedRecords[Idx] = Id;
        }
        else{
            this.checkedRecords[Idx] = 0;
        }
    }

    AfterDeleteData(data, template) {
        if (data.Status == "false") {
            this.alerts = "<span>Subscription prices delete unsuccessfull</span>";
        } else {
            this.modalAlerts = "<p>Subscription prices deleted successfully</p>";
            this.modalRef = this.modalService.show(template);
            this.SearchSubscriptionPrices(this.selectedEditionId);
        }
    }

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

    AfterUpdateData(data, template) {
        if (data.Status == "false") {
            this.alerts = "<span>Subscription prices update unsuccessfull</span>\n";
        } else {
            this.modalAlerts = "<p>Subscription prices updated successfully</p>";
            this.modalRef = this.modalService.show(template);
        }
    }

    BindFormFields(data){
        this.subscriptionPrices = data;
        this.checkedRecords = new Array<number>(this.subscriptionPrices.length);
        this.subscriptionPriceFrm.setControl('subscriptionPrices', this.fb.array(this.subscriptionPrices));
    }

    UpdateSubscriptionPrices(template: TemplateRef<any>){
        console.log(this.subscriptionPriceFrm.value);  
        this.alerts = '';
        if(this.alerts == ''){
            var obj = this.subscriptionPriceFrm.controls['subscriptionPrices'].value;   
                this.subscriptionPriceService.UpdateSubscriptionPrices(obj).subscribe(
                data => (this.AfterUpdateData(data, template)),
                error => {
                    this.error = <any>error; 
                    this.alerts = "<span>" + this.error + "</span>";
                });
        }
    }

    InsertSubscriptionPrice(template: TemplateRef<any>){
        console.log(this.insertSubscriptionPriceFrm.value);  
        this.alerts = '';
        if(this.alerts == ''){
            var obj = this.insertSubscriptionPriceFrm.value;
                return this.subscriptionPriceService.InsertSubscriptionPrice(obj)
                .subscribe(
                data => (this.AfterInsertData(data, template)),
                error => {
                    this.error = <any>error; 
                    this.alerts = "<span>" + this.error + "</span>";
                });
        }
    }

    DeleteSubscriptionPrices(template: TemplateRef<any>){
        console.log(this.checkedRecords);  
        this.alerts = '';
        if(this.alerts == ''){
            var obj =  this.checkedRecords.filter(C => C > 0);
                return this.subscriptionPriceService.DeleteSubscriptionPrices(obj)
                .subscribe(
                data => (this.AfterDeleteData(data, template)),
                error => {
                    this.error = <any>error; 
                    this.alerts = "<span>" + this.error + "</span>";
                });
        }
    }

    AddSubscriptionPrice(){
        this.Mode = 'Add';
        this.topPos = '100px';
        this.divClass = 'col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3';
        this.insertSubscriptionPriceFrm.reset();
        this.alerts = '';
        this.insertSubscriptionPriceFrm.controls['subscriptionPriceId'].setValue(0);
        this.insertSubscriptionPriceFrm.controls['title'].setValue('');
        this.insertSubscriptionPriceFrm.controls['price'].setValue('');
        this.insertSubscriptionPriceFrm.controls['duration'].setValue('');
        this.insertSubscriptionPriceFrm.controls['editionId'].setValue(this.selectedEditionId);
        this.insertSubscriptionPriceFrm.controls['isActive'].setValue(false);
    }

    CancelAdd(){
        this.Mode = 'Search';
        this.topPos = '2000px';
        this.divClass = 'col-sm-12';
        this.insertSubscriptionPriceFrm.reset();
        this.alerts = '';
        this.SearchSubscriptionPrices(this.selectedEditionId);
    }

}