document.getElementById('files').addEventListener('change', function(event) {
    const files = event.target.files;
    const previewContainer = document.getElementById('previewContainer');
    previewContainer.innerHTML = ''; // Limpiar el contenedor de previsualización
    
    Array.from(files).forEach((file, index) => {
      const reader = new FileReader();
      const previewItem = document.createElement('div');
      previewItem.classList.add('preview-item');
      previewItem.dataset.index = index;

      const removeBtn = document.createElement('button');
      removeBtn.textContent = '×';
      removeBtn.classList.add('remove-btn');
      removeBtn.onclick = function() {
        previewItem.remove(); // Eliminar el item de la vista previa
        const dataTransfer = new DataTransfer();
        Array.from(document.getElementById('files').files).forEach((file, fileIndex) => {
          if (fileIndex !== index) {
            dataTransfer.items.add(file);
          }
        });
        document.getElementById('files').files = dataTransfer.files;
      };
      previewItem.appendChild(removeBtn);

      reader.onload = function(e) {
        if (file.type.startsWith('image/')) {
          const img = document.createElement('img');
          img.src = e.target.result;
          previewItem.appendChild(img);
        } else if (file.type === 'application/pdf') {
          const iframe = document.createElement('iframe');
          iframe.src = e.target.result;
          previewItem.appendChild(iframe);
        }
        previewContainer.appendChild(previewItem);
      };

      reader.readAsDataURL(file);
    });
  });

  
  
  