import { Component, OnInit } from '@angular/core';
import { environment } from '../../../environments/environment';
import { ContactService } from '../../contact.service';
import { Router } from '@angular/router';
import { MdDialog } from '@angular/material';
import { BillingInfoComponent } from '../billing-info/billing-info.component';

@Component({
  selector: 'app-buy-now',
  templateUrl: './buy-now.component.html',
  styleUrls: ['./buy-now.component.scss']
})
export class BuyNowComponent implements OnInit {
  showNav: boolean;
  showMenu = environment.showLandingpageMenu;
  dev_url=environment.hostUrl;
  url: string;
  userData: any;
  logo_1: string;
  organisationName: any;
  supportContact: any;
  text: any;
  backgroundImage: string;
  requestDemo : boolean = false; 
  demoString = "Demo Request"; 
  cpn_showLink: boolean =false;
  showLink: boolean;
  mainSiteurl:any; 
  saanvi_showLink: boolean = false;
  cartButton = false;
  message: any;
  cartValue: any=[];
  imageUrl: any;
  productQuantity : number =1 ; 
  totalAmount: any = 0;
  gstAmount: any = 0;
  payableAmt:any =0;

  constructor(private dataService: ContactService,private router: Router, public dialog: MdDialog) { 
    let split1 = document.URL.split('//')[1];
    let split2 = split1.split('/')[0]; 
        
    if(split2 == "www.oneqlik.in"){ 
      this.showLink = true;
      this.mainSiteurl  = 'https://www.oneqlik.in/telematics/';
      this.backgroundImage = '../../assets/image/login_bg.jpg';
      this.logo_1  = '../../assets/image/f_logo.jpg' ; 
    }else{  
      console.log("Inside showlink false");
      this.showLink = false;
    }

    if(split2 == "login.cpntrack.com"){ 

      this.cpn_showLink = true;
      this.mainSiteurl  = 'http://www.cpntrack.com/';
    
    }

    if((split2 == "saanvisecurity.in")||(split2 == "www.saanvisecurity.in")){
      this.saanvi_showLink = true;
      this.mainSiteurl  = 'http://site.saanvisecurity.in';
    }
   
  }

  ngOnInit() {
    let split1 = document.URL.split('//')[1];
    let split2 = split1.split('/')[0];
    this.url = split2;

    this.dataService.dealerInfo(this.url).subscribe(
      data => {
        this.userData = data 
      if(this.url != 'www.oneqlik.in'){    
        this.logo_1    =  this.dev_url+this.userData.logo; 
      }
      
        this.organisationName = (this.url == 'www.oneqlik.in')?'OneQlik':this.userData.organisationName  ; 
        this.supportContact = this.userData.contact_support?this.userData.contact_support : ''; 


        this.text=this.userData.text;
       
      },err=>{
        this.backgroundImage = '../../assets/image/login_bg.jpg' ;
     
      }) 

    
    for(var i = 0; i <this.showMenu.length;i++){
      if (this.url == this.showMenu[i]) {
        this.showNav = true;
        return;
      } else {
        this.showNav = false;
      }
    }

    var message_1 = localStorage.getItem('addedToCart');
    if(message_1!=undefined){
      this.message = JSON.parse(message_1); 
      console.log(this.message);

       for(var j = 0 ; j<this.message.length;j++){
      console.log(this.message[j]);
      // if(this.message[j].discountPrice != NaN){
        this.totalAmount +=  parseInt(this.message[j].discountPrice);
      // }else{
        // this.totalAmount +=  0; 
      // }
      this.gstAmount = (this.totalAmount*18)/100 ;
      this.payableAmt = this.totalAmount + this.gstAmount ; 

        }
      
      // console.log(this.cartValue);

    }
  }

  goBack(){
    this.router.navigateByUrl("product-shop/product-overview");
  }


  buyNow(){
    let dialogRef = this.dialog.open(BillingInfoComponent, {
      width:'800px',
      data: { deviceObject: '' }
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log(result)
      if(result !='cancel'){
        var modalResponse = JSON.parse(result);
        modalResponse['CartData'] = this.message ;
        console.log(modalResponse);
        this.ReceiptSend(modalResponse);

      }
      // if (result == "succ") {
   
      // }
      function launch_toast() {
        // console.log(divid);
        var x = document.getElementById("toast")
        //console.log(x);
        x.className = "show";
        setTimeout(function () { x.className = x.className.replace("show", ""); }, 1500);
      }
    });
  }


  ReceiptSend(mRes){
    this.dataService.sendReceipt(mRes).subscribe(res=>{
      console.log(res);
    })



  }

}
