import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import { NgxSpinnerService } from 'ngx-spinner'; import Swal from 'sweetalert2'; import { Oferente } from 'src/app/models/oferente.model'; import { OferenteService } from 'src/app/services/oferente/oferente.service'; import { Cuenta } from 'src/app/models/cuenta.model'; import { CuentaService } from 'src/app/services/oferente/cuenta.service'; import { Email } from 'src/app/models/email.model'; import { Especialidad } from 'src/app/models/especialidad.model' @Component({ selector: 'app-oferente', templateUrl: './oferente.component.html' }) export class OferenteComponent implements OnInit { oferenteId: number = 1; entidad: string = 'Oferente'; oferente: Oferente | null = null; frmEntidad: FormGroup; cuenta: Cuenta | null = null; emails: Email[] = []; especialidades: Especialidad[] = []; constructor(private oferenteService: OferenteService, private spinner: NgxSpinnerService, private cuentaService: CuentaService) { this.frmEntidad = new FormGroup({ 'id': new FormControl('', Validators.required), 'nombre': new FormControl('', Validators.required), 'rut': new FormControl('', Validators.required), }); } ngOnInit(): void { this.frmEntidad = new FormGroup({ 'id': new FormControl('', Validators.required), 'nombre': new FormControl('', Validators.required), 'rut': new FormControl('', Validators.required), 'fecha_inicio_actividad': new FormControl('', Validators.required), }); this.oferenteService.getByIdAlpha(this.oferenteId).subscribe((response: any) => { this.oferente = response['data']; if (!this.oferente) { return; } this.frmEntidad.patchValue({ 'id': this.oferente.id, 'nombre': this.oferente.nombre, 'rut': this.oferente.rut, 'fecha_inicio_actividad': this.oferente.fecha_inicio_actividad, }); this.cuentaService.getCuentaByOferenteId(this.oferente.id).subscribe((response: any) => { this.cuenta = response['data']; }); if (this.oferente && this.oferenteId) { this.oferenteService.getEmailsByOferenteId(this.oferenteId).subscribe((emails) => { this.emails = emails; }); } }); } editarEntidad(): void { if (this.frmEntidad.valid) { this.spinner.show(); const entidadEditada = this.frmEntidad.value; this.oferenteService.update(entidadEditada.id, entidadEditada).subscribe((response) => { this.spinner.hide(); Swal.fire({ title: `${this.entidad} Actualizada`, text: `El ${this.entidad.toLowerCase()} ha sido actualizado satisfactoriamente.`, icon: 'success', confirmButtonText: 'OK' }); }, (error) => { this.spinner.hide(); Swal.fire({ title: 'Error', text: 'Ha ocurrido un error al intentar actualizar el oferente.', icon: 'error', confirmButtonText: 'OK' }); }); } } }