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

@Component({
  selector: 'app-load-management',
  templateUrl: './load-management.component.html',
  styleUrls: ['./load-management.component.scss']
})
export class LoadManagementComponent implements OnInit {
  loadManagement=[];
  useridd: any;
  reportData: {};
  loadManagement_response: any;
  errorMsg: string;
  data_descip: any;

  constructor(public _contactService :ContactService,private _flashMessagesService: FlashMessagesService) { }

  ngOnInit() {

    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    this.getLoadManagementData();
  }


  getLoadManagementData(){
    
    var uId = this.useridd;
    this._contactService.getLoad_managementData(uId)
      .subscribe(res=>{
        // console.log(res);
        this.loadManagement_response=res;

        if(this.loadManagement_response.length==0){
          this._flashMessagesService.show('No data Available', { cssClass: 'alert-danger', timeout: 3000 });
        }else{
          for(var i=0;i<this.loadManagement_response.length;i++){
            this.reportData = {
              deviceName: this.loadManagement_response[i].devName,
              groupName : (this.loadManagement_response[i].devGrp.length!=0)?this.loadManagement_response[i].devGrp[0].name:"", 
              geofenceName:(this.loadManagement_response[i].gfs.length!=0)?this.loadManagement_response[i].gfs[0].geoname:"",
              Distance:(this.loadManagement_response[i].gfs.length!=0)?parseFloat((this.loadManagement_response[i].gfs[0].dist.calculated)).toFixed(2):""
            } 
            this.loadManagement.push(this.reportData);
  
          }
          // console.log("dataToShow",this.loadManagement);
        }
    
      },err=>{
        // console.log(err.status);
        if(err.status==500){
          this._flashMessagesService.show('Internal Server error, Please try after sometime', { cssClass: 'alert-danger', timeout: 3000 });
        }
        
      })
     
  }


  tost1(divid){
    if(divid == "Isr"){
      this.data_descip = "Internal server error";
      launch_toast();
      
    }
   else if(divid == "emptyArr"){
    this.data_descip = "No data available";
    launch_toast();
    
    // this.onNoClick("succ");
   }
      function launch_toast() {
       // console.log(divid);
        var x = document.getElementById("toast")
        //console.log(x);
        x.className = "show";
        setTimeout(function(){ x.className = x.className.replace("show", "this.data_descip"); }, 1500);
    }
     }



}
