  const path = require('path');
  const fs = require('fs');
  const xml2js = require('xml2js');
 
   
  document.addEventListener('DOMContentLoaded', async () => {

  function getFilesInFolder(folderName) {
          
   const xmlFilePath = path.join(__dirname, folderName);
   try { 

    const fileNames = fs.readdirSync(xmlFilePath);
    const xmlFiles = fileNames.filter((file) => file.endsWith('.xml'));
    return xmlFiles;
    
  } catch (error) {
    console.error('Error reading folder:', error);
    return [];
  }
}
require('dotenv').config();
//XmlPath get from .env
const xmlPath=process.env.XML_FILE_PATH;

const folderName = `/${xmlPath}`; 
const filePaths = [];
const files = getFilesInFolder(folderName);
files.forEach((file) => {
        const filePath = file; 
        filePaths.push(filePath); 
      });



         // Function to load XML data and populate the list view
          function loadList(xmlFilePath) {
              return fetch(`${xmlPath}`+xmlFilePath)
                  .then(response => response.text())
                  .then(xmlContent => {
                      const parser = new DOMParser();
                      const xmlDoc = parser.parseFromString(xmlContent, 'text/xml');
                      var titleText = $(xmlDoc).find('Title').text();
                      return titleText;
                  })
                  .catch(error => {
                      console.error("Error loading XML:", error);
                      throw error;
                  });
          }


// Function to load and display XML files in a loop
async function loadMultipleXMLs() {
  for (const xmlFile of filePaths) {
    console.log(xmlFile)
      try {
          const titleText = await loadList(xmlFile); // Initially load in list view

          const dynamicId = `download-button-${Date.now()}`;

          // Handle list view
          const html_for_list = `
          <div class="landing-icons">
          <a class="icons" href="QuestionPreview.html?filename=${xmlFile}" title="Preview">
          <i><img src="assets/images/view.svg" alt=""></i>
          </a>
          <a class="icons" href="addform.html?filename=${xmlFile}" title="Edit">
            <i><img src="assets/images/edit.svg" alt=""></i>
          </a>
          <a class="icons" href="#" id="${dynamicId}" data-filename="${xmlFile}" title="Download">
            <i><img src="assets/images/download.svg" alt=""></i>
          </a>
         </div>
      <div class="resent-post-single-box">
          <div class="resent-thunb">
              <i class="fa fa-file-text-o" style="font-size: 25px; color:#457B9D; margin-left: 5px"></i>
          </div>
          <div class="xmlFileName">
              <a style="font-size: 16px; font-weight: 300">${xmlFile}</a>
          </div>
      </div>`;
          $('#list').append(html_for_list);

          // Add event listener for download buttons
        $('#'+dynamicId).on('click',function (event) {
            event.preventDefault();
            const filename =$(this).data('filename');
            downloadFile(filename);
        });

      } catch (error) {
          console.error("Error loading XML:", error);
      }

  }
}



// Call the function to load and display XML files
loadMultipleXMLs();



 });


			



      








        
      

