managediscountcode.component.ts 4.23 KB
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 '../UserEntity/datamodel';
//import { BsDatepickerModule } from 'ngx-bootstrap';
import { Http, Response } from '@angular/http';

//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<DiscountCode>;
manageDiscountCodeFrm: FormGroup;
insertUpdateDiscountCodeFrm: FormGroup;
error: any;
alerts: string;
divClass: string = '';
topPos: string = '2000px';
bsValue: Date = new Date();

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: ['', Validators.required],
            startDate: ['', Validators.required],
            endDate: ['', Validators.required],
            percentage: ['', Validators.required],
            isActive: ['']
        });
        this.SearchDiscountCodes();
    }

    SearchDiscountCodes() {  
        console.log(this.manageDiscountCodeFrm.controls['searchDiscountCode'].value + ', ' + 
        this.manageDiscountCodeFrm.controls['searchStartDate'].value + ', ' + 
        this.manageDiscountCodeFrm.controls['searchEndDate'].value);      
        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(this.alerts == ''){
            var obj = this.insertUpdateDiscountCodeFrm.value;
            return this.manageDiscountCodeService.InsertDiscountCode(obj)
              .subscribe(
              n => (this.AfterInsertData(n)),
              error => this.error = <any>error);
        }
    }

    AfterInsertData(data) {
        if (data.Status == "false") {
            this.alerts = "<span>Password change unsuccessfully</span>";
        } else {
            this.alerts = "<span>Password changed successfully</span>";
        }
    }

    BindFormFields(data){
        this.discountCodes = data;
        this.manageDiscountCodeFrm.setControl('discountCodes', this.fb.array(this.discountCodes));
    }

    ResetFormFields(){
        this.manageDiscountCodeFrm.reset()
        //this.manageDiscountCodeFrm.controls['loginId'].setValue(this.user.LoginId);
        //this.manageDiscountCodeFrm.controls['oldPassword'].setValue('');
        //this.manageDiscountCodeFrm.controls['newPassword'].setValue('');
        //this.manageDiscountCodeFrm.controls['confirmPassword'].setValue('');
        this.alerts = '';
    }

    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';
    }

    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';
    }

    CancelAddEdit(){
        this.Mode = 'Manage';
        this.topPos = '2000px';
        this.divClass = 'col-sm-12';
    }
}