import { Component, OnInit , Inject } from '@angular/core';
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
import { DatepickerModule as YourAlias } from 'angular2-material-datepicker'
import * as moment from 'moment-timezone';
import {ContactService} from '../contact.service';
import {Schedule} from '../shedule';
import { FlashMessagesService } from 'angular2-flash-messages';

@Component({
  selector: 'app-edit-sche',
  templateUrl: './edit-sche.component.html',
  styleUrls: ['./edit-sche.component.scss'],
  providers: [ContactService]
})

export class EditScheComponent implements OnInit {
  onn_time: Date ;
  offf_time: Date;
  timme:any; 
  schedules: Schedule[];
  schedule: Schedule;
  shdata:any;
  data2:any;
  tim :any;
  did:any;
  ontime:any;
  on_uid:any;
  offtime:any;
  mess:any;
  mess2:any;
  constructor( private _flashMessagesService: FlashMessagesService,public dialogRef: MdDialogRef<EditScheComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) {
      // 4:38 PM
      this.did = data.Device_name;
      this.on_uid= data.on_id;
      // console.log(data);
      this.timme = moment(data.on_time, 'HH:mm a');
      this.tim = moment(data.off_time, 'HH:mm a');
      this.onn_time= new Date( this.timme ); 
      this.offf_time=new Date( this.tim );
      this.data2 = data.on_id;
     }
    onNoClick(): void {
      this.dialogRef.close();
    }
    editsche(on,off){
      // console.log(on);
      this.ontime = on;
      this.offtime = off;
    
      const newshe ={
        did : this.did,
        ontime: this.ontime,
        on_uid:this.on_uid,
        offtime: this.offtime,
        wday: [this.dayselM,this.dayselT,this.dayselW,this.dayselTH,this.dayselF,this.dayselSa,this.dayselSu]   
     }
  // console.log(newshe)
  this.contactService.editshe(newshe)
  .subscribe(schedule => {
    this.onNoClick()   
  this.schedules.push(schedule);
  
  
  } , (err: any) => {
    //  console.log(err.status); 
    // console.log(err);
  if(err.status ==   500)
  {
    this.mess = err._body.split(":")[1] ;
    this.mess2 = this.mess.split("}")[0];
    
    this._flashMessagesService.show("Error: "+this.mess2, { cssClass: 'alert-danger',timeout: 2000 });

  }     
}
); 


    }
    dayselM:any;
    dayselT:any;
    dayselW:any ;
    dayselTH:any;
    dayselF:any;
    dayselSa:any;
    dayselSu:any;
  ngOnInit() {
   // mytime: 

  // console.log(this.data2);
   this.contactService.getsh( this.data2 ).subscribe(
    
    data => {
      this.shdata = data           
    //  console.log(this.shdata[0].daily_sched.weekdays);
      
     this.dayselM = this.shdata[0].daily_sched.weekdays[0];
     this.dayselT = this.shdata[0].daily_sched.weekdays[1];
     this.dayselW = this.shdata[0].daily_sched.weekdays[2];
     this.dayselTH = this.shdata[0].daily_sched.weekdays[3];
     this.dayselF = this.shdata[0].daily_sched.weekdays[4];
     this.dayselSa = this.shdata[0].daily_sched.weekdays[5];
     this.dayselSu = this.shdata[0].daily_sched.weekdays[6];
    });
  }

  }


