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

@Component({
  selector: 'app-cmd-ui',
  templateUrl: './cmd-ui.component.html',
  styleUrls: ['./cmd-ui.component.scss']
})
export class CmdUIComponent implements OnInit {
  useridd: any;
  errFlag:boolean=false;
  errmsg:any='';
  command:any;
  action:any;
  device_type: any;


    constructor(public dialogRef: MdDialogRef<CmdUIComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) {
      

     }
     commands=[];
  ngOnInit() {
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    var d_model = this.data.deviceObject.device_model;
    this.contactService.getDeviceModelbyID(d_model).subscribe(res=>{
      console.log('deviceModel=>',res);
      this.device_type = res[0].device_type;
    })


  }

  triggerCommand(){
    console.log(this.data);
    var imei = this.data.deviceObject.Device_ID; 
        console.log('this.device_type',this.device_type);

    var pld = {
      command:this.command,
      protocol_type:this.device_type, 
      action:this.action,
      imei:imei,
      user : this.useridd
    }
        console.log("command triggered",pld);
        this.contactService.triggerCommand(pld).subscribe(res=>{
          console.log(res);
          this.dialogRef.close('success');
        })
  }


  close(){
    console.log('closed');
    this.dialogRef.close('cancel');
  }
}
