import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { HitAWodService } from 'src/app/services/hit-a-wod.service';
import { Question } from 'src/app/models/question.model';
import { SubscriptionService } from 'src/app/services/subscription.service';
import { UserService } from 'src/app/services/user.service';
import { User } from 'src/app/models/user.model';

@Component({
  selector: 'app-contacto',
  templateUrl: './contacto.component.html',
  styleUrls: ['./contacto.component.css']
})
export class ContactoComponent implements OnInit {
  @Output() close:EventEmitter<boolean> = new EventEmitter();
  private user_id:string;
  public question:Question = new Question();
  public subjects = [
    "Proponer ejercicio nuevo",
    "Incidencia técnica",
    "Tengo una duda con el funcionamiento",
    "Otro"
  ];

  constructor(private api:HitAWodService,
              private subscriptionService: SubscriptionService,
              private userService:UserService) { }

  ngOnInit(): void {
    this.subscriptionService.currentSubscription.subscribe( s => {
      if (s) {
        if (s.box) {
          this.question.company_id = s.box.company_id;
          this.question.box_id = s.box.id;
        } else {
          this.question.company_id = s.company.id;
          this.question.box_id = null;
        }
      }
    });

    const user:User = this.userService.currentUserValue;
    this.question.name = user.name + ' ' + user.surnames;
    this.question.mobile = user.phone;
    this.question.mail = user.email;
  }

  isDisabled() {
    if(this.question.name == null) return true;
    if(this.question.mobile == null) return true;
    if(this.question.mail == null) return true;
    if(this.question.sub == null && this.question.sub2 == null) return true;
    if(this.question.msg == null) return true;

    return false;
  }

  enviar() {
    this.question.subject = this.question.sub + " " + this.question.sub2;
    this.api.sendQuestion(this.question).subscribe( s => {  });
    this.closeDialog();
  }

  closeDialog() {
    this.close.emit(true)
  }

}
