import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef } from '@angular/core'; import { ManageDiscountCodeService } from './managediscountcode.service'; import { Router } from '@angular/router'; import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { DiscountCode } 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'; @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; modalAlerts: string; divClass: string = ''; topPos: string = '2000px'; selectedRow: number = 0; datePipe: DatePipe = new DatePipe('en-US'); bsValue1: Date = new Date(); bsValue2: Date = new Date(); bsValue3: Date = new Date(); bsValue4: Date = new Date(); selectedId: number = 0; modalRef: BsModalRef; constructor(private manageDiscountCodeService: ManageDiscountCodeService, private router: Router, private fb: FormBuilder, private modalService: BsModalService) { } 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.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); } openModal(template: TemplateRef) { this.modalRef = this.modalService.show(template); } public InsertUpdateDiscountCode(template: TemplateRef) { this.alerts = ''; if(parseInt(this.insertUpdateDiscountCodeFrm.value.percentage) > 100){ this.alerts = 'Percentage must be between 0 to 100
'; } if(Date.parse(this.insertUpdateDiscountCodeFrm.controls['startDate'].value) > Date.parse(this.insertUpdateDiscountCodeFrm.controls['endDate'].value)){ this.alerts += 'Discount start date must be lower than discount end date'; } if(this.alerts == ''){ var obj = this.insertUpdateDiscountCodeFrm.value; if(obj.discountId == 0){ return this.manageDiscountCodeService.InsertDiscountCode(obj) .subscribe( n => (this.AfterInsertData(n, template)), error => this.error = error); } else{ return this.manageDiscountCodeService.UpdateDiscountCode(obj) .subscribe( n => (this.AfterUpdateData(n, template)), error => this.error = error); } } } AfterInsertData(data, template) { if (data.Status == "false") { this.alerts = "Discount code save unsuccessfull"; } else { this.modalAlerts = "

Discount code saved successfully

"; this.modalRef = this.modalService.show(template); } } AfterUpdateData(data, template) { if (data.Status == "false") { this.alerts = "Discount code update unsuccessfull"; } else { this.modalAlerts = "

Discount code updated successfully

"; this.modalRef = this.modalService.show(template); } } BindFormFields(data){ this.selectedRow = 0; this.discountCodes = data; this.discountCode = this.discountCodes[0]; 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); if(this.discountCode.IsActive){ this.insertUpdateDiscountCodeFrm.controls['isActive'].setValue('true'); } else{ this.insertUpdateDiscountCodeFrm.controls['isActive'].setValue('false'); } } 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)); } }