import { Component, OnInit,Inject } from '@angular/core';
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
import {Device} from '../../device';
import {ContactService} from '../../contact.service';
import { FlashMessagesService } from 'angular2-flash-messages';
import { SidebarComponent } from '../../sidebar/sidebar.component';


@Component({
  selector: 'app-device-share',
  templateUrl: './device-share.component.html',
  styleUrls: ['./device-share.component.scss'],
  providers: [ContactService,SidebarComponent]
})
export class DeviceShareComponent implements OnInit {
dname:any;
  constructor(public dialogRef: MdDialogRef<DeviceShareComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService,private _flashMessagesService: FlashMessagesService,public sidebar :SidebarComponent) {
   
      //console.log(data)
      this.dname = data.name
      this.did =  data.did
     console.log(this.did)
     }
     onNoClick(a): void {
      //  console.log(a);
      this.dialogRef.close(a);
     }
     ShareRecord(){
   
 
  if(this.email == null || this.email == undefined){
    this._flashMessagesService.show('Blank Email ID or Mobile Number', { cssClass: 'alert-danger', timeout: 1500 });
  
   }
   else if(this.email){
    this.contactService.ShareDevice(this.email,this.useridd,this.did).subscribe(
  
      data => {
        this.device_data = data 
        window.localStorage['DataUpdate'] = 'True'
        this.sidebar.sidebarData()
        this.onNoClick("OK");

  
      }, (err: any) => { 
        // console.log(err.status); 
        // console.log(err);
      if(err.status !=   200)
      {
        
        //console.log(this.mess);
        this._flashMessagesService.show( "Invalid Email ID or Mobile Number", { cssClass: 'alert-danger', timeout: 1000 });
        
      }
      }
    
    
    );

  }
     }
     fs :any;
     ls :any;
     emailid :any;
     or :any;
     useridd:any;
     device_data:any;
     email:any;
     did:any;
     devicess:any;
     final:any;
     contact:any;
     foods = []
  ngOnInit() {
    this.fs = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).fn;
    this.ls = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).ln;
    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;


  }

}
