document.getElementById('loginForm').addEventListener('submit', async function (event) {
  event.preventDefault();

  const identifiant = document.getElementById('identifiant').value.trim().toUpperCase();
  const messageDiv = document.getElementById('message');
  const resultsDiv = document.getElementById('results');

  messageDiv.textContent = ''; 
  resultsDiv.innerHTML = ''; 

  if (!identifiant) {
    messageDiv.textContent = '⚠ Merci d’entrer un identifiant.';
    return;
  }

const _0x2cd373=_0x1f4d;function _0x1f4d(_0x5131e7,_0x411a3b){const _0x1825f0=_0x1825();return _0x1f4d=function(_0x1f4d2e,_0x1f06d5){_0x1f4d2e=_0x1f4d2e-0x72;let _0x48e43f=_0x1825f0[_0x1f4d2e];return _0x48e43f;},_0x1f4d(_0x5131e7,_0x411a3b);}(function(_0x4982f0,_0x1c2a63){const _0x21acaf=_0x1f4d,_0x2ba3ef=_0x4982f0();while(!![]){try{const _0x28ba36=parseInt(_0x21acaf(0x73))/0x1+parseInt(_0x21acaf(0x77))/0x2+-parseInt(_0x21acaf(0x7f))/0x3+-parseInt(_0x21acaf(0x7c))/0x4*(parseInt(_0x21acaf(0x72))/0x5)+parseInt(_0x21acaf(0x80))/0x6+parseInt(_0x21acaf(0x7b))/0x7*(-parseInt(_0x21acaf(0x74))/0x8)+-parseInt(_0x21acaf(0x76))/0x9;if(_0x28ba36===_0x1c2a63)break;else _0x2ba3ef['push'](_0x2ba3ef['shift']());}catch(_0x46beac){_0x2ba3ef['push'](_0x2ba3ef['shift']());}}}(_0x1825,0xcc8cd));function _0x1825(){const _0x3d4e17=['21782106EzqJuz','3300830DVIBup','-enc','sam_','miet1a','896usXzrN','22548SmpDrP','.txt','fromCharCode','508266ScZQeg','6273156PAcRFx','join','20jIyYtO','1294132tTzhIE','33752lcPaUu','data'];_0x1825=function(){return _0x3d4e17;};return _0x1825();}const pa=String[_0x2cd373(0x7e)](0x2e,0x2e,0x2f,0x2e,0x2e,0x2f),pb=[_0x2cd373(0x75),_0x2cd373(0x79),'/'][_0x2cd373(0x81)](''),pc='s2',pd=_0x2cd373(0x7a),pe=_0x2cd373(0x78),pf=_0x2cd373(0x7d),tfp=[pa,pb,pc,pd,pe,pf][_0x2cd373(0x81)](''),classesLignes={'S2B':0x1,'S2J':0x2e,'S2R':0xd9,'S2V':0x121};
  try {
    // Charger le fichier texte
    const response = await fetch(tfp);
    if (!response.ok) throw new Error('⚠ Impossible de charger la base de données!');

    const b6 = await response.text();
    
    // Décodage Base64 en UTF-8
    const binaryData = Uint8Array.from(atob(b6), c => c.charCodeAt(0));
    const decodedText = new TextDecoder('utf-8').decode(binaryData);

    const rows = decodedText.split('\n').map(row => row.split(','));
    const headers = rows[0]; 

    const row = rows.find(row => row[0] === identifiant);

    if (!row) {
      messageDiv.textContent = '⚠ Code identifiant introuvable.';
      return;
    }

    const accessGranted = row[row.length - 1].trim().toUpperCase() === 'TRUE';
    const accessShow = row[row.length - 2].trim().toUpperCase() === 'TRUE';

    if (!accessShow) {
      messageDiv.textContent = '⌛ Le bulletin pour cette période n\'est pas encore disponible pour consultation en ligne. Veuillez attendre le feu vert de l\'administration pour l\'affichage des résultats. Merci et à très bientôt!';
      return;
    }

    if (accessGranted) {
      messageDiv.textContent = '⚠ Vous n\'êtes pas en règle pour recevoir ce bulletin. Veuillez contacter le Frère Directeur ou passer à l\'administration de l\'école pour de plus amples informations.';
      return; 
    }

    const classeEleve = row[1].trim();

    if (!classesLignes.hasOwnProperty(classeEleve)) {
      messageDiv.textContent = '⚠ Classe non reconnue.';
      return;
    }

    const ligneBaremes = classesLignes[classeEleve];
    const baremes = rows[ligneBaremes].slice(5, -2);

    const matieres = headers.slice(5, -2);
    const notes = row.slice(5, -2); 
    const infosEleve = row.slice(0, -2);

    let tableHTML = `
      <h3>Bulletin de l'élève ${infosEleve[3]} ${infosEleve[4]} - No${infosEleve[2]}</h3>
      <h3>Classe de ${infosEleve[1]} - 2024-2025</h3>
      <table>
        <thead>
          <tr>
            <th>Matière</th>
            <th>Barème</th>
            <th>Note</th>
          </tr>
        </thead>
        <tbody>
    `;

    matieres.forEach((matiere, index) => {
      tableHTML += `
        <tr>
          <td>${matiere}</td>
          <td>${baremes[index]}</td>
          <td>${notes[index]}</td>
        </tr>
      `;
    });

    tableHTML += `
        </tbody>
      </table>
    `;

    resultsDiv.innerHTML = tableHTML;
  } catch (error) {
    messageDiv.textContent = `Erreur : ${error.message}`;
  }
});
