import { Component, OnInit } from '@angular/core';
import { EventService, Registration} from "../../services/event.service";
import {ActivatedRoute, Router} from "@angular/router";
import {tap} from "rxjs/operators";
import {NavigationService} from "../../services/navigation.service";
import {RegistrationService} from "../../services/reservation.service";

@Component({
  selector: 'app-registration',
  templateUrl: './registration.component.html',
  styleUrls: ['./registration.component.scss']
})
export class RegistrationComponent implements OnInit {

  public registration: Registration;

  constructor(private eventService: EventService,
              private route: Router,
              private activeRoute: ActivatedRoute ,
              private navigationService: NavigationService,
              public registrationService: RegistrationService) { }

  ngOnInit() {
    this.activeRoute.params.subscribe( value => {
      console.info("map: ", this.activeRoute.snapshot.paramMap);
     let registration =  this.eventService.findRegistration(+this.activeRoute.snapshot.paramMap.get('idRegistration'));

      registration.subscribe(
        value1 => {
          this.registration = value1;
        }
      )

    });
  }

  cancel(registration: Registration) {
    this.eventService.cancelRegistration(registration.id).subscribe(
      value => {
        this.route.navigate(['registrations']);
      },
      error => {
        alert('Hiba történt!');
      }
    )
  }

  mayCancel(registration: Registration) {
    return this.registrationService.mayCancel(registration);
  }

  goBack() {
    this.navigationService.navigateRegistrations();
  }
}
