import { Component, Input } from '@angular/core';
import { Hotel } from '../HotelItem/hotel';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { NgFor, NgIf } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Router, RouterLink } from '@angular/router';

@Component({
  selector: 'app-hotel-form',
  standalone: true,
  imports: [ReactiveFormsModule, NgIf, NgFor, RouterLink],
  templateUrl: './hotel-form.component.html',
  styleUrl: './hotel-form.component.css'
})
export class HotelFormComponent {
  @Input() public hotel!: Hotel;
  public hotelForm!: FormGroup

  constructor(public http: HttpClient, public router: Router) {

  }

  ngOnInit(): void {
    this.hotelForm = new FormGroup({
      name: new FormControl(this.hotel.hotelName, Validators.required),
      description: new FormControl(this.hotel.description, Validators.required),
      price: new FormControl(this.hotel.price, Validators.required),
    })
  }

  submit(): void {
    this.hotel.hotelName = this.hotelForm.get("name")?.value;
    this.hotel.description = this.hotelForm.get("description")?.value;
    this.hotel.price = this.hotelForm.get("price")?.value;
    console.log(this.hotelForm.value);
    this.http.put("/api/hotels/" + this.hotel.id, this.hotel).subscribe();
    this.router.navigate(["/"]);
  }
}
