diff --git a/assets/js/chatbot.js b/assets/js/chatbot.js index 0de44a2..0903f17 100644 --- a/assets/js/chatbot.js +++ b/assets/js/chatbot.js @@ -48,18 +48,71 @@ class ChatbotIA { } ouvrirChat() { - // Récupérer l'ID de la question actuelle - this.currentQuestionId = this.getCurrentQuestionId(); - - if (!this.currentQuestionId) { - alert('Erreur : impossible de déterminer la question actuelle'); - return; - } - - this.modalChat.classList.add('show'); - this.chatInput.focus(); + // Récupérer l'ID de la question ACTUELLE dynamiquement + this.currentQuestionId = this.detectCurrentQuestion(); + + if (!this.currentQuestionId) { + alert('Erreur : impossible de déterminer la question actuelle'); + return; } + console.log('💬 Ouverture chat - Question:', this.currentQuestionId); + + // Charger l'aide restante pour cette question + this.loadHelpRemaining(); + + this.modalChat.classList.add('show'); + this.chatInput.focus(); +} + +// NOUVELLE MÉTHODE : Détecter la question visible +detectCurrentQuestion() { + // Méthode 1 : Chercher l'élément de question visible/actif + const activeQuestion = document.querySelector('.question.active, .question-container.active, [data-question-active="true"]'); + if (activeQuestion && activeQuestion.dataset.questionId) { + return parseInt(activeQuestion.dataset.questionId); + } + + // Méthode 2 : Chercher dans les inputs visibles + const visibleInputs = document.querySelectorAll('input[name^="reponse_"]:not([style*="display: none"])'); + if (visibleInputs.length > 0) { + const match = visibleInputs[0].name.match(/reponse_(\d+)/); + if (match) { + return parseInt(match[1]); + } + } + + // Méthode 3 : Index de question actuelle (si ton système utilise un index) + if (typeof currentQuestionIndex !== 'undefined' && questionsIds[currentQuestionIndex]) { + return questionsIds[currentQuestionIndex]; + } + + // Fallback : première question + return questionsIds[0]; +} + +// NOUVELLE MÉTHODE : Charger l'aide restante via AJAX +async loadHelpRemaining() { + try { + const response = await fetch('/mathematiques/api/chatbot_check_limit.php', { + method: 'POST', + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({ + id_tentative: this.tentativeId, + id_question: this.currentQuestionId + }) + }); + + const data = await response.json(); + if (data.success) { + this.helpRemaining = data.remaining; + this.updateHelpBadge(); + } + } catch (error) { + console.error('Erreur chargement limite aide:', error); + } +} + fermerChat() { this.modalChat.classList.remove('show'); }