add-documentos.component.ts 3.97 KB
Newer Older
1
import { Component, OnInit, Inject } from '@angular/core';
2
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3
import { MatDialogRef, MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog';
4 5 6 7 8 9
import { docompar } from 'src/app/modules/main/Models/docompar';
import { DatosEspeService } from 'src/app/modules/main/services/APIs Externas/Datos espe/datos-espe.service';
import { saveAs } from 'file-saver';
import { parametrosdet } from 'src/app/modules/main/Models/parametrosdet';
import { ParametrosdetService } from 'src/app/modules/main/services/parametrosdet/parametrosdet.service';
import { Actualiza_datosService } from 'src/app/modules/main/services/actualiza_datos/actualiza_datos.service';
10
import { ErrorTextoComponent } from '../../../errores/error-texto/error-texto.component';
11 12 13 14 15 16
@Component({
  selector: 'vex-add-documentos',
  templateUrl: './add-documentos.component.html',
  styleUrls: ['./add-documentos.component.css']
})
export class AddDocumentosComponent implements OnInit {
17 18 19 20 21 22
  myForm: FormGroup;
  selectedFile: File | null = null;
  file: File;
  enviarSolicitud = false;
  formularioEnviado: boolean;
  docompar: docompar;
23 24
  constructor(
    private dialogRef: MatDialogRef<AddDocumentosComponent>,
25 26 27 28
    @Inject(MAT_DIALOG_DATA) public data: { idConf: number },
    private formBuilder: FormBuilder,
    private datosEspeService: DatosEspeService,
    private pdfService: ParametrosdetService,
29 30
    private datosCompartidos: Actualiza_datosService,
    private dialog: MatDialog
31 32 33
  ) {
    this.docompar = new docompar();
  }
34 35

  ngOnInit(): void {
36
    this.myForm = this.formBuilder.group({
37
      archivo: ['', Validators.required]
38
    });
39 40 41
    console.log('ID recibido:', this.data.idConf);
  }

42 43 44 45
  onFileSelected(event: any) {
    this.selectedFile = event.target.files[0];
  }

46
  archivoEsPDFValido: boolean = false;
47 48 49 50 51
  onFileChange(event: any) {
    // Obtener el archivo seleccionado
    const fileList: FileList = event.target.files;
    if (fileList.length > 0) {
      this.file = fileList[0];
52 53 54 55 56
      if (!this.validarArchivo(this.file)) {
        this.openModalError('Seleccione Unicamente Archivos PDF');
      } else {
        this.archivoEsPDFValido = true;
      }
57 58 59
    }
  }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  openModalError(texto: string) {
    const dialogRef = this.dialog.open(ErrorTextoComponent, {
      disableClose: true,
      data: { mensaje: texto }
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('La ventana modal se ha cerrado');
      this.formularioEnviado = true;
    });
  }

  validarArchivo(file: File): boolean {
    const fileName = file.name;
    return fileName.toLocaleLowerCase().endsWith('.pdf');
  }

77 78 79
  async onSubmit() {
    if (this.myForm.valid) {

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      try {
        const pdfData = await this.enviarPDF();
        if (pdfData.idPDF === null || pdfData.idPDF === undefined) {

        } else {
          this.docompar.uzytavinstproy_id = this.data.idConf;

          this.docompar.uzytavdocompar_nombre_url = pdfData.idPDF;
          this.docompar.uzytavdocompar_nombre_doc = pdfData.filename;
          this.pdfService.guardarPDF(this.docompar).subscribe(
            response => {
              this.datosCompartidos.actualizarDatos(this.docompar);
              this.submitForm();
              console.log('pdf enviado')
            },
            error => {
              console.log(error)
            }
          );
99
        }
100 101
      } catch (error) {
      }
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }
  }

  async enviarPDF() {
    const formData = new FormData();
    formData.append('files', this.file);
    formData.append('sistema', 'vinculacion');

    try {
      const response = await this.datosEspeService.saveDocument(formData);
      return {
        idPDF: response.uuid,
        filename: response.fileName
      };
    } catch (error) {
117 118 119 120 121 122
      this.openModalError('Este PDF ya se ha subido');
      this.archivoEsPDFValido = false;
      return {
        idPDF: null,
        filename: ''
      }
123 124 125 126
    }
  }


127
  submitForm() {
128 129
    this.dialogRef.close();
  }
130
  cerrarModal() {
131

132
    this.dialogRef.close();
133 134 135

  }
}