import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Router,ActivatedRoute } from '@angular/router';
import { WebService } from '../../provider/web.service';
import { apiConfig } from '../../../environments/server.config';
import { ImageStorage } from '../../../environments/server.config';

@Component({
  selector: 'app-summary',
  templateUrl: './summary.component.html',
  styleUrls: ['./summary.component.scss']
})
export class SummaryComponent implements OnInit {
  loader:boolean;
  loginDetails:any;
  prdtData : any = new Array();
  product_id : any;
  userAddress:any;
  serverUrl: string;
  productDetails : any;
  imageServer: any;
  ref_id : any;

  @ViewChild("failureModel") public failureModelRef: ElementRef;

  constructor(
    private router        : Router, 
    private route         : ActivatedRoute,
    public webService     : WebService
  ) { 
    this.loader = true;
    this.imageServer = ImageStorage;
    this.serverUrl   = apiConfig;
  }

  ngOnInit() {
    this.checkUserLogin();
    this.checkProductId();
    this.getUserAddress();
    this.getProductDetails();
  }

  checkUserLogin(){
    this.loginDetails = JSON.parse(this.webService.getLocalStorageItem('userData'));
    if(!this.loginDetails){
      this.goToPage('purchaseHome','');
    }
  }

  checkProductId(){
    this.prdtData = JSON.parse(this.webService.getLocalStorageItem('productDetails'));
  
    if(this.prdtData && this.prdtData['product_id'] > 0){
      this.product_id = this.prdtData['product_id'];
    }else{
      this.goToPage('purchaseHome','');
    }
  }
  
  getUserAddress(){
    if(!this.loginDetails || !this.loginDetails.customer_id){
      return false;
    }
    this.loader = true;
    this.webService.post_data('getUserAddressById',{'customer_id':this.loginDetails.customer_id,'address_id':this.prdtData.address_id}).subscribe(response => {
      if(response.status == 'success'){
        this.userAddress = response.data;
      }else{
        this.userAddress = false;
      }
      this.loader = false;
    })
  }

  getProductDetails(){
    this.loader = true;
    this.webService.post_data('SingleProductSearch',{'product_id':this.product_id}).subscribe(response => {
      if(response.status == 'success'){
        this.productDetails = response.data;
      }else{
        this.productDetails = false;
      }
      this.loader = false;
    })
  }

  paymentButton(){
    this.loader = true;
    this.prdtData.customer_id = this.loginDetails.customer_id;
    let cart_id = (this.prdtData.cart_id)?'/'+this.prdtData.cart_id:'';
    this.webService.post_data('initOrderBooking',{'data':this.prdtData}).subscribe(response => {    
      if(response.status == 'success'){
        document.location.href = this.serverUrl+'orderPayNow/'+response.data+cart_id;
      }else{
        this.loader = false;
        this.failureModelRef.nativeElement.click();
      }
    })
  }

  goToPage(path,data=null){
    this.router.navigate([path],{queryParams:  data});
    document.body.scrollTop  =  document.documentElement.scrollTop  =  0;
  }

}
