import { Component, Inject, NgZone, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
import { ContactService } from '../contact.service';

@Component({
  selector: 'app-comment-window',
  templateUrl: './comment-window.component.html',
  styleUrls: ['./comment-window.component.scss']
})
export class CommentWindowComponent implements OnInit {
  useridd: any;
  commentMsg:any;
  assetsData:any
  showAssets:boolean=false;
  showEmergencyContact:boolean=false;
  emergencyContactData:any;
  userDetail: any={
    first_name :"",
    last_name:"",
    phone:"",
    email:"",
   
  };
  selectedTab=1;

  constructor(public dialogRef: MdDialogRef<CommentWindowComponent>,private errorDialog: MdDialog,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService, private ngZone: NgZone) { }

  ngOnInit() {
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    console.log('this.data',this.data);
    console.log('commentMsg',this.commentMsg);
    this.contactService.getUserObj(this.data.deviceData.user).subscribe(res=>{
      this.userDetail = res;
      if(res.emergency_contact){
        this.emergencyContactData=res.emergency_contact;
        this.showEmergencyContact=true;
      }
      // console.log(this.emergencyContactData)
    })
    this.getDeviceData()

  
  }
  //https://socket.oneqlik.in/devices/getDevicebyId?deviceId=868003033811113
  getDeviceData(){
 this.contactService.getdevById(this.data.deviceData.did).subscribe((res:any) => {
  // this.assetsData=res;
   this.assetsData = JSON.parse(res['_body']);
  // console.log("-------",this.assetsData);
  this.showAssets=true
  
 },err=>{
   console.log(err);
   
 })
  }
  templatemessage:any;
  templatemessage1:any;
  comment(){
    var that  =this;
    console.log('Comment Here',this.data);
    console.log('commentMsg',this.commentMsg);
    if((this.commentMsg=='')||(this.commentMsg == undefined)){
        console.log("Please Type Error Message");
        this.templatemessage = "Please Type Comment First" ;
        var error = setTimeout(()=>{
          that.templatemessage = '';
          clearTimeout(error);
        },5000)
    }else{
      var payload={
        notification_id : this.data.deviceData._id,
        commentSaved : true,
        comment : this.commentMsg
      };
      this.contactService.commentSave(payload).subscribe(res=>{
        console.log(res);
        this.templatemessage1 = "Comment Saved";
        var error = setTimeout(()=>{
          that.templatemessage1 = '';
          this.dialogRef.close(this.data.deviceData._id);
          clearTimeout(error);
        },3000)
      },err=>{
        console.log(err);
      })
    }
  }
  close(){
    console.log("close",this.dialogRef)
    this.errorDialog.closeAll()
    this.dialogRef.close()
   
  }
  selectTab(value){
    console.log(value);
    
    this.selectedTab=value;
  }

}
