import { Component, OnInit } from '@angular/core';
import { LocalStorage } from "../../core/services/local_storage.service"
import { HyperService } from '../../core/services/http.service';
import { Router,ActivatedRoute } from "@angular/router";
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
import * as moment from 'moment';
@Component({
  selector: 'app-launch-brochure',
  templateUrl: './launch-brochure.component.html',
  styleUrls: ['./launch-brochure.component.scss']
})
export class LaunchBrochureComponent implements OnInit {
  sub: any;
  model:any;
  varient:any;
  token:any;
  image:string='';
  brochure_pdf:any;
  loader:boolean=false;
  constructor(private route: ActivatedRoute,private server: HyperService, private router: Router, private sanitizer: DomSanitizer) {
    this.sub = this.route.params.subscribe(params => {
      this.model = params['model'];
      this.varient = params['varient'];
      this.token = params['token'];

      // console.log(this.token,"this.token")
  });
  LocalStorage.setValue('brochure_token',this.token)
  // console.log(this.model,this.varient,this.token)
   }

  ngOnInit() {
    this.getComparison()
  }
  getComparison() {
    this.loader=true;
    this.server.getBrochure("explore/model")
        .then((data) => {
            if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
              this.loader=false;

                data.result.data.forEach(element => {
                  if(this.model==element.model_id){
                    this.image=element.model_banner_image_url;
                    this.brochure_pdf=element.model_brochure;
                  }
                });
                // setTimeout(() => this.brochure_pdf = "", 1000);
                // this.excelExport();
              } else if (data.result.status == 401 && data.result.response == 'failed') {
                this.loader=false;
             } else {
              this.loader=false;
              alert(data.result.message)
             }
        });
  }
  clickExcelDownloadpdf(brochure_pdf){
    // this.brochure_pdf=this.brochure_pdf;
    // setTimeout(() => this.brochure_pdf = "", 1000);
    // this.excelExport();
    window.open(brochure_pdf, '_blank')
  }
  excelExport() {
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.brochure_pdf);
  }
}
