import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter } from '@angular/core'; import { SubscriptionPriceService } from './subscriptionprice.service'; import { Router } from '@angular/router'; import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { SubscriptionPriceModel } from '../UpdateProfile/datamodel'; import { BsDatepickerModule } from 'ngx-bootstrap'; import { Http, Response } from '@angular/http'; import { Pipe, PipeTransform } from '@angular/core'; import { DatePipe } from '@angular/common'; //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 = 'Manage'; subscriptionPrice: SubscriptionPriceModel; subscriptionPrices: Array; subscriptionPriceFrm: FormGroup; insertUpdateSubscriptionPriceFrm: FormGroup; error: any; alerts: string; divClass: string = ''; topPos: string = '2000px'; selectedRow: number = 0; datePipe: DatePipe = new DatePipe('en-US'); bsValue3: Date = new Date(); bsValue4: Date = new Date(); selectedId: number = 0; constructor(private subscriptionPriceService: SubscriptionPriceService, private router: Router, private fb: FormBuilder) { } ngOnInit(): void { this.divClass = 'col-sm-12'; this.subscriptionPrice = new SubscriptionPriceModel(); this.alerts = ''; this.subscriptionPriceFrm = this.fb.group({ searchSubscriptionPriceType: [''], subscriptionPrices: this.fb.array([]) }); this.insertUpdateSubscriptionPriceFrm = this.fb.group({ discountId: [''], discountCode: [''], startDate: ['', Validators.required], endDate: ['', Validators.required], percentage: ['', [Validators.required, Validators.pattern('[0-9.]*')]], isActive: [true] }); this.SearchSubscriptionPrices(); } public SetClickedRow(i: number, item: any) { this.selectedRow = i; this.selectedId = item['Id']; this.subscriptionPrice = item; } public SearchSubscriptionPrices() { this.selectedRow = -1; this.subscriptionPriceService.GetSubscriptionPrices( { subscriptionPriceType: this.subscriptionPriceFrm.controls['searchSubscriptionPriceType'].value, }) .subscribe(x => { this.BindFormFields(x) }, error => this.error = error); } public InsertUpdateSubscriptionPrice() { console.log('InsertUpdateSubscriptionPrice'); this.alerts = ''; if(parseInt(this.insertUpdateSubscriptionPriceFrm.value.percentage) > 100){ this.alerts = 'Percentage must be between 0 to 100'; } if(this.alerts == ''){ var obj = this.insertUpdateSubscriptionPriceFrm.value; if(obj.discountId == 0){ return this.subscriptionPriceService.InsertDiscountCode(obj) .subscribe( n => (this.AfterInsertData(n)), error => this.error = error); } else{ return this.subscriptionPriceService.UpdateDiscountCode(obj) .subscribe( n => (this.AfterUpdateData(n)), error => this.error = error); } } } AfterInsertData(data) { if (data.Status == "false") { this.alerts = "Subscription price save unsuccessfull"; } else { this.alerts = "Subscription price saved successfully"; } } AfterUpdateData(data) { if (data.Status == "false") { this.alerts = "Subscription price update unsuccessfull"; } else { this.alerts = "Subscription price updated successfully"; } } BindFormFields(data){ this.subscriptionPrices = data; this.subscriptionPriceFrm.setControl('subscriptionPrices', this.fb.array(this.subscriptionPrices)); } AddDiscountCode(){ 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.insertUpdateSubscriptionPriceFrm.reset(); this.alerts = ''; this.insertUpdateSubscriptionPriceFrm.controls['discountId'].setValue(0); this.insertUpdateSubscriptionPriceFrm.controls['discountCode'].setValue(''); this.insertUpdateSubscriptionPriceFrm.controls['startDate'].setValue(''); this.insertUpdateSubscriptionPriceFrm.controls['endDate'].setValue(''); this.insertUpdateSubscriptionPriceFrm.controls['percentage'].setValue(''); this.insertUpdateSubscriptionPriceFrm.controls['isActive'].setValue(true); } EditDiscountCode(){ this.Mode = 'Edit'; 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.alerts = ''; this.insertUpdateSubscriptionPriceFrm.controls['discountId'].setValue(this.subscriptionPrice.Id); this.insertUpdateSubscriptionPriceFrm.controls['discountCode'].setValue(this.subscriptionPrice.DiscountCode); this.insertUpdateSubscriptionPriceFrm.controls['startDate'].setValue(this.datePipe.transform(this.subscriptionPrice.StartDate, 'MM/dd/yyyy')); this.insertUpdateSubscriptionPriceFrm.controls['endDate'].setValue(this.datePipe.transform(this.subscriptionPrice.EndDate, 'MM/dd/yyyy')); this.insertUpdateSubscriptionPriceFrm.controls['percentage'].setValue(this.subscriptionPrice.Percentage); this.insertUpdateSubscriptionPriceFrm.controls['isActive'].setValue(this.subscriptionPrice.IsActive); } CancelAddEdit(){ this.Mode = 'Manage'; this.topPos = '2000px'; this.divClass = 'col-sm-12'; this.SearchSubscriptionPrices(); this.selectedRow = this.subscriptionPrices.findIndex(C => C.Id == this.selectedId); this.SetClickedRow(this.selectedRow, this.subscriptionPriceFrm.controls['subscriptionPrices'].value.find(C => C.Id == this.selectedId)); } }