import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BaseComponent } from '../base/base.component';
import { QuotesService } from '../_services';
import { AuthenticationService } from '../_services/authentication.service';
import { Quote } from '../_models';
import * as _ from 'lodash';
import * as UAParser from 'ua-parser-js';
declare var jQuery: any;

@Component({
  selector: 'app-prices',
  templateUrl: './prices.component.html',
  styleUrls: ['./prices.component.css']
})
export class PricesComponent extends BaseComponent implements OnInit {
  public loading = false;
	public quotes: Quote;

  showdata: any = false;
  public searchId: any = "";
  public searchEmail: any = "";
  public status: string = "";
  public searchFrom: string = "";
  public searchTo: string = "";

  /* pagination */
  public itemsPerPage = 10;
  public offset = 0;
  public currentPage = 0;
  public paginator = true;
  public userAgent;
  public total: number = 0;

	constructor(
        private router: Router,
        private auth: AuthenticationService,
        private quotesService: QuotesService
	) {
		super();
  }

	ngOnInit() {
    jQuery("#dateDesde").datepicker();
    jQuery("#dateHasta").datepicker();
    var e = {
      "first": 0
    }
    this.loadLazy(e);
	}

  loadLazy(e: any) {
      let current = (e.first / this.CONSTANTS.users.perPage) + 1; 
      const data = {
          email: this.searchEmail,
          id: this.searchId,
          status: this.status,
          searchTo: this.searchTo,
          searchFrom: this.searchFrom,
          perPage: this.itemsPerPage,
          page: current
      };
      this.loading = true;

      this.quotesService.getFilteredQuotes(data).subscribe((quotes: any) => {
        this.loading = false;
        this.showdata = true;
        this.total = quotes.total;
        this.quotes = quotes.data;
      }, err => this.handleError(err));
  }

  updateData(){
    this.searchTo = jQuery("#dateHasta").val();
    this.searchFrom = jQuery("#dateDesde").val();
    let data = {
      email: this.searchEmail,
      id: this.searchId,
      status: this.status,
      searchTo: this.searchTo,
      searchFrom: this.searchFrom,
      perPage: this.itemsPerPage,
      page: this.currentPage
    };
    var e = {
      "first": 0
    }
    this.loadLazy(e);
  }

  showUserAgent(quote: any){
    this.userAgent =  quote.userAgent.replace(new RegExp("\\\\", "g"), "");
    var parser = new UAParser();
    parser.setUA(this.userAgent);
    var result = parser.getResult();
    return result.os.name
  }


  toDetails(id: number){
    this.router.navigate(['/admin/quote/',id]);
  }

	private handleError(error: any): Promise<any> {
  	console.error(this.constructor.name + ': An error occurred', error);
      if (error.error.msg === this.CONSTANTS.servicesMsg.NOT_AUTHORIZED) {
          this.auth.logout();
          this.router.navigate(['/login']);
      }
      return Promise.reject(error.message || error);
 	}

}
