import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { UserInput } from 'src/app/models/userInput';
import * as moment from 'moment';
import { AutoAansturingService } from './auto-aansturing.service';
@Component({
  selector: 'app-auto-aansturing',
  templateUrl: './auto-aansturing.component.html',
  styleUrls: ['./auto-aansturing.component.css']
})
export class AutoAansturingComponent implements OnInit {
  successmessage : string = '';
  userInput: UserInput = {
    depth: 0,
    period: '',
    timestamp: '',
    wharfID: 0
  };
  errorMessage: string = 'default message';
  depth: number=0;
  wharfId = this.route.snapshot.paramMap.get('wharIid');
  endDate: Date= new Date;
  postUserInput$: Subscription = new Subscription();
  constructor(private autoService: AutoAansturingService, private router: Router,private route: ActivatedRoute) { }

  ngOnInit(): void {
  }
onSubmit(){
  let date = moment(this.endDate).hour(8).minutes(0).toDate();
  this.userInput = {
    period: date.toISOString(),
    depth: this.depth,
    timestamp: (new Date).toISOString(),
    wharfID: Number(this.wharfId)
  }
  console.log(this.userInput)

 this.autoService.postUserInput(this.userInput).subscribe({
    next: () => this.successmessage = "Input opgeslagen",
    error: (e) => (this.errorMessage = e.message),
  });
}
}
