import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { DonationService } from './donation.service';

@Component({
  selector: 'app-donation',
  templateUrl: './donation.component.html',
  styleUrls: ['./donation.component.css']
})
export class DonationComponent implements OnInit {
  donationForm!: FormGroup;
  selectedDonationType: string;

  constructor(private donationService: DonationService) {

    this.selectedDonationType = 'Sorok zdravii';
  }



  onDonationTypeChange(event: any): void {
    this.selectedDonationType = event.target.value;
  }

  ngOnInit() {
    this.donationForm = new FormGroup({
      user_name: new FormControl(null, Validators.required),
      user_mail: new FormControl(null, [Validators.required]),
      user_phone: new FormControl(null, Validators.required),
      dontype: new FormControl( Validators.required),
      names: new FormControl(null, Validators.required),
    });
  }

  onSubmit() {
   
    const donationData = {
      name: this.donationForm.value.user_name,
      email: this.donationForm.value.user_mail,
      phone: this.donationForm.value.user_phone,
      donationType: this.donationForm.value.dontype,
      username: this.donationForm.value.names
    };
    console.log('Donation data:', donationData);

    this.donationService.createDonation(donationData).subscribe(response => {
      console.log(response);
    }, error => {
      console.error(error);
    });
  }
}