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

@Component({
  selector: 'app-poi-list',
  templateUrl: './poi-list.component.html',
  styleUrls: ['./poi-list.component.scss']
})
export class PoiListComponent implements OnInit {
  routeId: any;
  useridd: any;
  route_detail:any;
  poiName:any;
  before:any;
  preDays:any=0;
  preHours:any=0;
  preMin:any=0;
  nextDays:any=0;
  nextHours:any=0;
  nextMin:any=0;
  halt:any;
  haltDays:any=0;
  haltHours:any=0;
  haltMin:any=0;
  showDetails=[];
  myInput:any;

  constructor(private contactService: ContactService,private router: Router,public dialogRef: MdDialogRef<PoiListComponent>,
    @Inject(MD_DIALOG_DATA) public data: any) { 
      // this.routeId = data.routeId;
      // console.log(this.routeId);
    }

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

  soon(){
    this.router.navigateByUrl("const?_i="+window.localStorage.token);
    
  }

  geo(){
    this.router.navigateByUrl("geofencing");
    
  }
poilist = [];
  getPOIdata(){

    this.contactService.POIDATA(this.routeId,this.useridd)
      .subscribe(res=>{
        this.route_detail =res;
     //  console.log(this.route_detail[0].poi);
       this.poilist = this.route_detail[0].poi;
      //  console.log(this.poilist);
      
       
      })  
  }

  
  updateDetails(){

    var previousDuration = this.preDays + ","  + this.preHours +"," + this.preMin ;
    var nextDuration = this.nextDays+ "," +  this.nextHours +","+  this.nextMin;
    var haltDuration = this.haltDays +"," +this.haltHours +"," + this.haltMin ;

    // var haltDUration = new Date(this.halt);
    // console.log('Working function !!!')
  
    var updatePayload= {
        "pname":this.poiName,
        "prv_dur" : previousDuration,
        "nxt_dur": nextDuration,
        "dur_halt" : haltDuration
     };


     var addPOI = JSON.stringify(updatePayload)
    
     this.dialogRef.close(addPOI);
    //  console.log(updatePayload);
    // this.contactService.updatePOIdetail(updatePayload)
    //   .subscribe(res=>{
    //     console.log(res);
      
    //     console.log(res);
    //   },err=>{
    //     console.log("error occured !!!")
    //   })

  }
}

