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

@Component({
  selector: 'app-edit-device-model',
  templateUrl: './edit-device-model.component.html',
  styleUrls: ['./edit-device-model.component.scss']
})
export class EditDeviceModelComponent implements OnInit {
  imobliser_type:any;
  devType:any;
  cnfigCMD:any;
  locCmd:any;
  immobilizerCmd:any;
  resumeCmd:any;
  resetCmd:any;
  modelID:any;
  showError:boolean = false;
  Manufacturer:any;
  selectedImmobilizer: any;
  deviceport: any;
  sms_ip;
  sms_apn;
  sms_timezone;

  constructor(public dialogRef: MdDialogRef<EditDeviceModelComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService, public _flashMessagesService : FlashMessagesService) { 
      console.log('DATA=>',data); 
      this.cnfigCMD = data.configuration_command;
      this.devType = data.device_type;
      this.immobilizerCmd = data.immoblizer_command;
      this.imobliser_type = data.imobliser_type;
      this.locCmd = data.location_command;
      this.resetCmd = data.reset_command;
      this.resumeCmd = data.resume_command;
      this.modelID = data._id;
      this.deviceport = data.deviceport;
      this.Manufacturer = data.Manufacturer;
      this.sms_apn=data.sms_apn;
      this.sms_ip=data.sms_ip;
      this.sms_timezone=data.sms_timezone
      console.log(this.imobliser_type);
      if(this.imobliser_type == 1){
        this.selectedImmobilizer = 'srvrcmd'
      }
      if(this.imobliser_type == 0){
        this.selectedImmobilizer = 'mblcmd'
      }
    }

  ngOnInit() {
  }
    errMsg  = "Internal Server Error" ;
  editDeviceModel(){
    var outerthis = this;
    var devModelObject  = {
            _id :this.modelID,
      device_type:this.devType,
        config_cmd:this.cnfigCMD,
           loc_cmd:this.locCmd,
          imob_cmd:this.immobilizerCmd,
         reset_cmd:this.resetCmd,
        resume_cmd:this.resumeCmd,
    }
    console.log(this.selectedImmobilizer);
    if(this.selectedImmobilizer){
      if(this.selectedImmobilizer == 'srvrcmd') this.serverCmd = '1.0';
      if(this.selectedImmobilizer == 'mblcmd')   this.serverCmd = '0.0';
      devModelObject['imobliser_type'] = this.serverCmd;
    }

    if(this.Manufacturer){
      devModelObject['Manufacturer'] = this.Manufacturer;
    }

    if(this.deviceport){
      devModelObject['deviceport'] = this.deviceport;
    }

     if(this.sms_apn){
      devModelObject['sms_apn'] = this.sms_apn;
    }
     if(this.sms_ip){
      devModelObject['sms_ip'] = this.sms_ip;
    }
     if(this.sms_timezone){
      devModelObject['sms_timezone'] = this.sms_timezone;
    }



    this.contactService.editDevModel(devModelObject)
      .subscribe(res=>{
          console.log(res);
          this.dialogRef.close('success');
      },err=>{
        console.log(err);
        this.errMsg = err._body;
        this._flashMessagesService.show(this.errMsg, { cssClass: 'alert-danger', timeout: 2000 });  
      })
  }
  cancel(a){
  console.log(a);
  this.dialogRef.close(a);
  }
  serverCmd:any;
  getImmobilizerType(ev:any){
    console.log(this.selectedImmobilizer);
  }

}
