import { Component, OnInit } from '@angular/core';
import { FormsService } from '../_services/forms.service';
import {FormGroup, ReactiveFormsModule, FormBuilder, Validators} from "@angular/forms";

@Component({
  selector: 'request',
  standalone: true,
  imports: [ReactiveFormsModule],
  templateUrl: './request.component.html',
  styleUrl: './request.component.scss'
})
export class RequestComponent implements OnInit {
  public hidden = true;
  public feedbackForm: FormGroup;

  constructor(private formBuilder:FormBuilder, private formsService: FormsService) {
  }

  ngOnInit():void{
    this.feedbackForm = this.formBuilder.group({
      'feedback-name':['',Validators.required],
      'feedback-email':['',[Validators.required,Validators.email]],
      'feedback-number':[''],
      'feedback-add-number':[''],
      'feedback-message':['',Validators.required],
      mailto:['test@nirgroup.ru'],
      type:['feedback-form-support']
    });
  }

  open(){
    this.hidden = false;
    this.ngOnInit()
  }

  close(){
    this.feedbackForm.reset();
    this.hidden = true;
  }

  onSubmit(){
    this.feedbackForm.markAllAsTouched();
    if (this.feedbackForm.valid) this.formsService.save('model', 'feedback-form-support', null, this.feedbackForm.value).subscribe(res => {
      this.close()
    }, error => {
      console.log(error);
    });
  }
}
