import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter } from '@angular/core'; import { ManageDiscountCodeService } from './managediscountcode.service'; import { Router } from '@angular/router'; import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { DiscountCode } 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: './managediscountcode.component.html' }) export class ManageDiscountCode implements OnInit { Mode: string = 'Manage'; discountCode: DiscountCode; discountCodes: Array; manageDiscountCodeFrm: FormGroup; insertUpdateDiscountCodeFrm: 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 manageDiscountCodeService: ManageDiscountCodeService, private router: Router, private fb: FormBuilder) { } ngOnInit(): void { this.divClass = 'col-sm-12'; this.discountCode = new DiscountCode(); this.alerts = ''; this.manageDiscountCodeFrm = this.fb.group({ searchDiscountCode: [''], searchStartDate: [''], searchEndDate: [''], discountCodes: this.fb.array([]) }); this.insertUpdateDiscountCodeFrm = this.fb.group({ discountId: [''], discountCode: [''], startDate: ['', Validators.required], endDate: ['', Validators.required], percentage: ['', [Validators.required, Validators.pattern('[0-9.]*')]], isActive: [true] }); this.SearchDiscountCodes(); } public SetClickedRow(i: number, item: any) { this.selectedRow = i; this.selectedId = item['Id']; this.discountCode = item; } public SearchDiscountCodes() { this.selectedRow = -1; this.manageDiscountCodeService.GetDiscountCodes( { discountCode: this.manageDiscountCodeFrm.controls['searchDiscountCode'].value, startDate: this.manageDiscountCodeFrm.controls['searchStartDate'].value, endDate: this.manageDiscountCodeFrm.controls['searchEndDate'].value }) .subscribe(x => { this.BindFormFields(x) }, error => this.error = error); } public InsertUpdateDiscountCode() { console.log('InsertUpdateDiscountCode'); this.alerts = ''; if(parseInt(this.insertUpdateDiscountCodeFrm.value.percentage) > 100){ this.alerts = 'Percentage must be between 0 to 100'; } if(this.alerts == ''){ var obj = this.insertUpdateDiscountCodeFrm.value; if(obj.discountId == 0){ return this.manageDiscountCodeService.InsertDiscountCode(obj) .subscribe( n => (this.AfterInsertData(n)), error => this.error = error); } else{ return this.manageDiscountCodeService.UpdateDiscountCode(obj) .subscribe( n => (this.AfterUpdateData(n)), error => this.error = error); } } } AfterInsertData(data) { if (data.Status == "false") { this.alerts = "Discount code save unsuccessfull"; } else { this.alerts = "Discount code saved successfully"; } } AfterUpdateData(data) { if (data.Status == "false") { this.alerts = "Discount code update unsuccessfull"; } else { this.alerts = "Discount code updated successfully"; } } BindFormFields(data){ this.discountCodes = data; this.manageDiscountCodeFrm.setControl('discountCodes', this.fb.array(this.discountCodes)); } 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.insertUpdateDiscountCodeFrm.reset(); this.alerts = ''; this.insertUpdateDiscountCodeFrm.controls['discountId'].setValue(0); this.insertUpdateDiscountCodeFrm.controls['discountCode'].setValue(''); this.insertUpdateDiscountCodeFrm.controls['startDate'].setValue(''); this.insertUpdateDiscountCodeFrm.controls['endDate'].setValue(''); this.insertUpdateDiscountCodeFrm.controls['percentage'].setValue(''); this.insertUpdateDiscountCodeFrm.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.insertUpdateDiscountCodeFrm.controls['discountId'].setValue(this.discountCode.Id); this.insertUpdateDiscountCodeFrm.controls['discountCode'].setValue(this.discountCode.DiscountCode); this.insertUpdateDiscountCodeFrm.controls['startDate'].setValue(this.datePipe.transform(this.discountCode.StartDate, 'MM/dd/yyyy')); this.insertUpdateDiscountCodeFrm.controls['endDate'].setValue(this.datePipe.transform(this.discountCode.EndDate, 'MM/dd/yyyy')); this.insertUpdateDiscountCodeFrm.controls['percentage'].setValue(this.discountCode.Percentage); this.insertUpdateDiscountCodeFrm.controls['isActive'].setValue(this.discountCode.IsActive); } CancelAddEdit(){ this.Mode = 'Manage'; this.topPos = '2000px'; this.divClass = 'col-sm-12'; this.SearchDiscountCodes(); this.selectedRow = this.discountCodes.findIndex(C => C.Id == this.selectedId); this.SetClickedRow(this.selectedRow, this.manageDiscountCodeFrm.controls['discountCodes'].value.find(C => C.Id == this.selectedId)); } }