import { Component, OnInit } from '@angular/core';
import { ContactService } from '../../contact.service';
declare var $: any

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.scss']
})
export class MainComponent implements OnInit {
   token_identifier: string;
  emailid: any;
  useridd: any;
  show:boolean=false
  showLine:boolean=false;
  isCC:boolean=true;
  supAdmin: any;
  constructor(private contactService:ContactService) { 
        this.token_identifier = localStorage.getItem('referrer_token=');

  }

  ngOnInit() {
     this.emailid = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).email;
    // this.or = JSON.parse(window.atob(window.localStorage.token.split(".")[1]))._orgName;
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
     var cc= JSON.parse(window.atob(window.localStorage.token.split('.')[1])).customer_role;
     this.supAdmin=JSON.parse(window.atob(window.localStorage.token.split('.')[1])).supAdmin;
    if(cc=="cc"){
       this.isCC=false;
     }
     this.contactService.get('/users/shareProfileImage?uid='+this.supAdmin).subscribe((res:any)=>{
      // console.log(res);
      
    if(res.imageDoc.length != 0){
        var str =  res.imageDoc[0]
        var splitStr = str.split('/');
        var concatStr ='';
       for(var i=1 ; i< splitStr.length ; i++){      
         if(i > 0){ 
          var tt = splitStr[i];   
          concatStr += '/'+tt ; 
         }
       }
       var logo1='https://www.oneqlik.in' + concatStr
       localStorage.setItem("logo1",logo1)
      
      }
      
    })
    this.getDevices();
    this.getGroups()
  }
  getGroups(){
   this.contactService.getGroup(this.useridd).subscribe(      
    res => {
      console.log(res);     
    //  this.groups = res["group_details"];
      localStorage.setItem("groupData",JSON.stringify(res["group_details"]))
      // console.log(this.grpName);  
    })
}

  getDevices(){
    var user
    if(this.isCC){
      user=this.useridd
    }else{
       user=this.supAdmin
    }
    this.contactService.getDevice(this.emailid,user).subscribe(
        data => {
          localStorage.setItem("vehicaleData",JSON.stringify(data.devices))
          if(localStorage.getItem("vehicaleData"))
          this.show=true;
      },err=>{

  })
}
openNav() {
  if(this.showLine){
    document.getElementById("mySidebar").style.width = "295px";
    // document.getElementById("main").style.marginLeft = "295px";
      document.getElementById("line").style.marginLeft = "295px";
      document.getElementById("line1").style.marginLeft = "295px";
      document.getElementById("line").style.background="none"
      

      this.showLine=false;
  }else{
    this.closeNav()
  }


}

closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft= "0";
  document.getElementById("line").style.marginLeft= "0";
  document.getElementById("line").style.background="#426E86"
  document.getElementById("line").style.width="20px"
  document.getElementById("line1").style.marginLeft= "0";

    this.showLine=true


}

}
