import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Tab1Service } from '../tab1/tab1.service';
import { Evento } from '../evento';
import { Router } from '@angular/router';

@Component({
  selector: 'app-prompt-evento',
  templateUrl: './prompt-evento.page.html',
  styleUrls: ['./prompt-evento.page.scss'],
})
export class PromptEventoPage implements OnInit{

  submitted = false;
  editarEvento = false;
  eventoForm = new FormGroup({
      nombre: new FormControl('', Validators.required),
      fecha: new FormControl('', Validators.required),
      precio1: new FormControl('', Validators.required),
      precio2: new FormControl('', Validators.required),
      descripcion: new FormControl('', Validators.required),
  });

  eventos: Evento[];

  constructor(private tab1Service: Tab1Service, private router: Router) {

   }
  
   ngOnInit(){
    if (this.tab1Service.editarEvento==true){
      this.eventoForm = this.tab1Service.eventoForms[this.tab1Service.eventoIndex];
    }
  }
  onSubmit(){
    this.submitted = true;
    if (this.eventoForm.valid){
      let evento = new Evento();
      this.asignarEvento(evento);
      this.router.navigate(['/tabs']);
    }
  }
  asignarEvento(evento: Evento){
    evento.setNombre(this.eventoForm.get('nombre').value);
    evento.setDesc(this.eventoForm.get('descripcion').value);
    evento.setFecha(this.eventoForm.get('fecha').value);
    evento.setPrecio1(this.eventoForm.get('precio1').value);
    evento.setPrecio2(this.eventoForm.get('precio2').value);
    //evento.setDia(evento.getFecha()); el datetime de Ion devuelve un String, no se puede 
    //evento.setHora(evento.getFecha()); transformar en dia y hora
    if (!this.tab1Service.eventos){
      this.tab1Service.initEventos();}
    if(!this.tab1Service.eventoForms){
      this.tab1Service.initEventoForms();}
    if(this.tab1Service.editarEvento==true){
      this.tab1Service.eventos[this.tab1Service.eventoIndex] = evento;
      this.tab1Service.eventoForms[this.tab1Service.eventoIndex] = this.eventoForm;
    }
    else{
      this.tab1Service.eventos.push(evento);
      this.tab1Service.eventoForms.push(this.eventoForm);}
    
  }

  get nombre(){
    return this.eventoForm.get('nombre');
  }

  get fecha(){
    return this.eventoForm.get('fecha');
  }

  get hora(){
    return this.eventoForm.get('hora');
  }

  get precio1() {
    return this.eventoForm.get('precio1');
  }

  get precio2() {
    return this.eventoForm.get('precio2');
  }

  get descripcion(){
    return this.eventoForm.get('descripcion');
  }
  

}
