discountcodereport.component.ts 7.28 KB
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
//import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import { DiscountCodeReports } from '../Reports/reportdatamodel';
import { ReportService } from '../Reports/report.service';
import { GlobalService } from '../../Shared/global';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { DatePipe } from '@angular/common';
import { BsDatepickerModule, BsDatepickerConfig   } from 'ngx-bootstrap';
import { Http, Response } from '@angular/http';
import { LoadingService } from '../../shared/loading.service';
import { PagerComponent } from '../../shared/Pager/pager.component';
import { ConfirmService } from '../../Shared/Confirm/confirm.service';
declare var $: any;

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

export class DiscountCodeReport implements OnInit {
    public lstDiscountCodeReport: any;
    public lstDiscountCode: any;
    DiscountCodeReportForm: FormGroup;
    DiscountCodeReports: DiscountCodeReports[];
    NewSubscription = new DiscountCodeReports();
    numberOfDiscountCodeReport: number;
    limit: number;
    page: number = 1;
    error;
    modalAlerts: string;
    divClass: string = '';
    topPos: string = '2000px';
    selectedRow: number = 0;
    datePipe: DatePipe = new DatePipe('en-US');
    bsValue1: Date = new Date();
    bsValue2: Date = new Date();
    selectedId: number = 0;
    modalRef: BsModalRef;
    date = new Date();
    previousdate = new Date();
    bsConfig: Partial<BsDatepickerConfig>;
    NoRecord: string;
    alerts: string;
    ExportingStart: boolean;
    buttonStatus: boolean;
    @ViewChild(PagerComponent) pagerComponent: PagerComponent;
    recordCount: number;
    pageNo: number;
    pageLength: number;

    constructor(private router: Router, private reportservice: ReportService, private fb: FormBuilder,
      private modalService: BsModalService, private global: GlobalService, 
      private _loadingService: LoadingService, private _confirmService: ConfirmService) { }

    ngOnInit(): void {
      this.ExportingStart = false;
      this.buttonStatus = false;
      this.bsConfig = Object.assign({}, { containerClass: 'theme-dark-blue' });
      this.NoRecord = this.global.NoRecords;
        let DiscountCodeReport = new DiscountCodeReports();
        this.previousdate.setDate(this.previousdate.getDate());
        this.DiscountCodeReportForm = this.fb.group({
            sFromDate: [this.previousdate],
            sToDate: [this.date],
            iDiscountCode: [0],
            iAccountNumber: []
        });
        this.alerts = '';
        this._loadingService.ShowLoading("global-loading");
        this.GetDiscountCode();
        this._loadingService.HideLoading("global-loading");
        //this.GetSubscriptionReport();
        $('#fixed_hdr2').fxdHdrCol({
            fixedCols: 0,
            width: "100%",
            height: 330,
            colModal: [
                { width: 180, align: 'center' },
                { width: 230, align: 'center' },
                { width: 150, align: 'Center' },
                { width: 150, align: 'Center' },
                { width: 350, align: 'Center' },
                { width: 500, align: 'Center' },
                { width: 130, align: 'Center' },
                { width: 120, align: 'center' },
                { width: 280, align: 'Center' },
                { width: 180, align: 'center' },
                { width: 200, align: 'center' },
                { width: 170, align: 'center' },
                { width: 80, align: 'center' },
                { width: 150, align: 'center' },
                { width: 150, align: 'center' },
                { width: 180, align: 'Center' },
                //{ width: 400, align: 'Center' },
                //{ width: 150, align: 'center' },
                //{ width: 110, align: 'center' },
            ],
            sort: true
        });
        this.pageNo = 1;
        this.pageLength = 10;
        this.NoRecord = this.global.NoRecords;
        this.recordCount = 0;
        this.pagerComponent = new PagerComponent();
            
        if(document.getElementById("fixed_table_rc") != null){
            document.getElementById("fixed_table_rc").remove();
            var testScript = document.createElement("script");
            testScript.setAttribute("id", "fixed_table_rc");
            testScript.setAttribute("src", "../assets/scripts/fixed_table_rc.js");
            testScript.setAttribute("type", "text/javascript");
            document.body.appendChild(testScript);
        }
        
    }

    GetDiscountCode() {
        this.reportservice.GetDiscountCodes().subscribe(st => { this.lstDiscountCode = st; }, error => this.error = <any>error);
    }

    GetDiscountReport(evt: any) {
      this._loadingService.ShowLoading("global-loading");
      var tempArr = evt.split(',');
      this.pageNo = parseInt(tempArr[0]);
      this.pageLength = parseInt(tempArr[1]);
      this.alerts = '';
      this.global.compareTwoDates(this.DiscountCodeReportForm.controls['sToDate'].value, this.DiscountCodeReportForm.controls['sFromDate'].value);
      if (this.global.ValidationMsg != '') {
        this.alerts = this.global.ValidationMsg
      }
      if (this.alerts == '') {
        this._loadingService.ShowLoading("global-loading");
        this.NewSubscription = this.DiscountCodeReportForm.value;
        var obj = this.NewSubscription;
        if (this.ExportingStart) {          
          this.reportservice.GetDiscountReport(obj, this.pageNo, this.pageLength).subscribe((DiscountCodeReports: DiscountCodeReports[]) => { this.ExportService(DiscountCodeReports); }, error => this.error = <any>error);
        }
        else {
          this.reportservice.GetDiscountReport(obj, this.pageNo, this.pageLength).subscribe((DiscountCodeReports: DiscountCodeReports[]) => { this.BindFormFields(DiscountCodeReports); }, error => this.error = <any>error);
        }
      }
    }

    SearchRecords() {
      this.GetDiscountReport('1, ' + this.pageLength);
    }

    BindFormFields(data) {
      this.recordCount = data.RecordCount;
      this.lstDiscountCodeReport = data.DiscountReportList;
      this.limit = this.lstDiscountCodeReport.length;
      if (this.lstDiscountCodeReport.length > 0) {
        this.NoRecord = '';
        this.buttonStatus = true;
        this._loadingService.HideLoading("global-loading");
      }
      if (this.lstDiscountCodeReport.length == 0) {
        this.NoRecord = this.global.NoRecords;
        this._loadingService.HideLoading("global-loading");
        this.buttonStatus = false;
      }
    }

    ExportEvent() {
      if (this.buttonStatus) {
        this.ExportingStart = true;
        this.GetDiscountReport('1, ' + this.recordCount);
      }
    }

    ExportService(data: any) {
      this._loadingService.HideLoading("global-loading");
      this.global.ExportToCSV(data.DiscountReportList, "DiscountReport-" + this.date.getDay() + "-" + this.date.getMonth() + "-" + this.date.getSeconds())
     
    }
}