|
|
@@ -2,7 +2,8 @@ import { FormApiService, FormConfiguration, FormListItem } from './../../service
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { EnviarInfoService } from '../../services/enviar-info.service';
|
|
|
import { FormService } from '../../services/FormService.service';
|
|
|
-
|
|
|
+import { ActivatedRoute } from '@angular/router';
|
|
|
+import { FormArray } from '@angular/forms';
|
|
|
interface GridCell {
|
|
|
row: number;
|
|
|
col: number;
|
|
|
@@ -73,17 +74,27 @@ export class AdminFormComponent implements OnInit {
|
|
|
{ type: 'checkbox', label: 'Casilla', required: false }
|
|
|
];
|
|
|
formData: any;
|
|
|
+ form: any;
|
|
|
+ fb: any;
|
|
|
|
|
|
constructor(
|
|
|
private _enviarInfo: EnviarInfoService,
|
|
|
private formApiService: FormApiService,
|
|
|
- private formService: FormService
|
|
|
+ private formService: FormService,
|
|
|
+ private route: ActivatedRoute
|
|
|
) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
this._enviarInfo.currentTextColor.subscribe(color => this.textColor = color);
|
|
|
this._enviarInfo.currentColor.subscribe(color => this.color = color);
|
|
|
this.addTab();
|
|
|
+ this.route.queryParams.subscribe(params => {
|
|
|
+ const id = params['id'];
|
|
|
+ if (id) {
|
|
|
+ this.cargarFormulario(+id);
|
|
|
+ console.log('ID recibido:', id);
|
|
|
+ }
|
|
|
+ });
|
|
|
// this.loadSavedForms();
|
|
|
}
|
|
|
|
|
|
@@ -111,6 +122,7 @@ export class AdminFormComponent implements OnInit {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
const tabConError = this.tabs.find(tab => tab.rows < 1 || tab.columns < 1);
|
|
|
if (tabConError) {
|
|
|
alert(` La sección "${tabConError.name}" tiene un número inválido de filas o columnas.\n\n
|
|
|
@@ -482,13 +494,67 @@ export class AdminFormComponent implements OnInit {
|
|
|
this.updateCurrentTabGrid();
|
|
|
}
|
|
|
}
|
|
|
- cargarFormulario(id: number): void {
|
|
|
- this.formService.getFormById(id).subscribe(data => {
|
|
|
- this.formData = data;
|
|
|
- // Aquí ya puedes inyectar los datos en tu formulario dinámico
|
|
|
- console.log('Form a editar:', this.formData);
|
|
|
- // ejemplo: this.form.patchValue(this.formData)
|
|
|
- });
|
|
|
+ cargarFormulario(id: number): void {
|
|
|
+ this.formService.getFormById(id).subscribe(data => {
|
|
|
+ this.formData = data;
|
|
|
+ console.log('Form a editar:', this.formData);
|
|
|
+
|
|
|
+ // Llama al método que reconstruye el formulario visual
|
|
|
+ this.loadFormConfiguration(this.formData.form.configuration);
|
|
|
+ this.formTitle = this.formData.form.title;
|
|
|
+ this.currentFormId = this.formData.form.id;
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ updateForm() {
|
|
|
+ if (!this.formTitle.trim()) {
|
|
|
+ alert('Por favor, ingresa un título para el formulario');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!this.currentFormId) {
|
|
|
+ alert('No se ha cargado ningún formulario para editar');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const tabConError = this.tabs.find(tab => tab.rows < 1 || tab.columns < 1);
|
|
|
+ if (tabConError) {
|
|
|
+ alert(`La sección "${tabConError.name}" tiene dimensiones inválidas (mínimo 1 fila y 1 columna).`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.currentTab) {
|
|
|
+ this.currentTab.grid = [...this.grid];
|
|
|
+ }
|
|
|
+
|
|
|
+ const config = this.buildFormConfiguration();
|
|
|
+ this.isLoading = true;
|
|
|
+ this.saveStatus = 'Actualizando formulario...';
|
|
|
+
|
|
|
+ this.formService.updateForm(this.currentFormId, config).subscribe({
|
|
|
+ next: (response) => {
|
|
|
+ console.log('Formulario actualizado:', response);
|
|
|
+ this.saveStatus = 'Formulario actualizado correctamente';
|
|
|
+ this.isLoading = false;
|
|
|
+ },
|
|
|
+ error: (error) => {
|
|
|
+ console.error('Error al actualizar formulario:', error);
|
|
|
+ this.saveStatus = 'Error al actualizar el formulario';
|
|
|
+ this.isLoading = false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+saveOrUpdateForm() {
|
|
|
+ if (this.currentFormId) {
|
|
|
+ this.updateForm();
|
|
|
+ } else {
|
|
|
+ this.saveForm();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|