$(document).ready(function(){
    var exam_module = $('#exam_module').val();
    
    $('#continueBtn-1').click(function(){
        $('.section-1').addClass('hidden');
        $('#continueBtn-2').addClass('hidden');
        $('.section-2').removeClass('hidden');
    });
    $('#previousBtn-1').click(function(){
        $('.section-1').removeClass('hidden');
        $('.section-2').addClass('hidden');
    });

    var countdown = 30 * 60; // 30 minutes in seconds
    var display1 = $('#timer-display');
    var display2 = $('#paper-display');
    var display3 = $('#paper2-display');
    var timerInterval; // Declare the timerInterval variable
    
    function startTimer(duration, display1, display2, display3) {
      var timer = duration, minutes, seconds;
      timerInterval = setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);
    
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
    
        display1.text(minutes + ":" + seconds);
        display2.text(minutes + ":" + seconds);
        display3.text(minutes + ":" + seconds);
    
        if (--timer === 0) {
          clearInterval(timerInterval); // Clear the interval when timer reaches zero
          $('.exam-timer').removeClass('hidden');
            $('.section-1').addClass('hidden');
            $('.section-2').addClass('hidden');
            $('#startButton').addClass('hidden')
            $('#timer-display').html('<b><h3 style="color:darkred">Time Up!</h3></b><b><h4>Your Responses has been submitted</h4></b>')
            var verbal_reasoning_section_1 = {
                save_script: '',
                student_id: student_id,
                exam_module: exam_module,
                key: key,
                data:collectAnswers()
            }
            $.post('code/script_code.php',verbal_reasoning_section_1,function(response){
                console.log(response)
            })  
          console.log(collectAnswers());
          return 0;
        }
      }, 1000);
    }
    
    $('#startButton').click(function () {
      $('.exam-timer').addClass('hidden');
      $('.section-1').removeClass('hidden');
      startTimer(countdown, display1, display2, display3); // Start the timer
    });
    

    $('#submitBtn1').click(function() {
        $('.exam-timer').removeClass('hidden');
        $('.section-1').addClass('hidden');
        $('.section-2').addClass('hidden');
        $('#startButton').addClass('hidden')
        $('#timer-display').html('<b><h3 style="color:darkred">Time Up!</h3></b><b><h4>Your Responses has been submitted</h4></b>')
        console.log(collectAnswers())
        clearInterval(timerInterval); 
        var verbal_reasoning_section_1 = {
            save_script: '',
            student_id: student_id,
            exam_module: exam_module,
            key: key,
            data:collectAnswers()
        }
        console.log(verbal_reasoning_section_1)
        $.post('code/script_code.php',verbal_reasoning_section_1,function(response){
            console.log(response)
        })
        return 0;
        
    });

    $('#submitBtn2').click(function() {
        $('.exam-timer').removeClass('hidden');
        $('.section-1').addClass('hidden');
        $('.section-2').addClass('hidden');
        $('#startButton').addClass('hidden')
        $('#timer-display').html('<b><h3 style="color:darkred">Time Up!</h3></b><b><h4>Your Responses has been submitted</h4></b>')
        console.log(collectAnswers())
        clearInterval(timerInterval); 
        var verbal_reasoning_section_1 = {
            save_script: '',
            student_id: student_id,
            exam_module: exam_module,
            key: key,
            data:collectAnswers()
        }
        $.post('code/script_code.php',verbal_reasoning_section_1,function(response){
            console.log(response)
        })
        return 0;
        
    });

    // Get Answers
    function collectAnswers() {
        let answers = {};
        
        // Loop through questions 3 to 17
        for (let i = 1; i <= 20; i++) {
            let questionId;
            let answer;
            if(i===4){
                 questionId = `#qn${i}-i-answer`;
                 answer1 = $(questionId).val();
                // 
                 questionId = `#qn${i}-ii-answer`;
                 answer2 = $(questionId).val();
                //  
                 answers[`qn${i}`] = answer1+','+answer2;
            }else if(i===5){
                questionId = `#qn${i}-i-answer`;
                answer1 = $(questionId).val();
                // 
                questionId = `#qn${i}-ii-answer`;
                answer2 = $(questionId).val();
                // 
                questionId = `#qn${i}-iii-answer`;
                answer3 = $(questionId).val();
                // 
                answers[`qn${i}`] = answer1+','+answer2+','+answer3;
            }else if(i===6){
                questionId = `#qn${i}-i-answer`;
                answer1 = $(questionId).val();
               // 
                questionId = `#qn${i}-ii-answer`;
                answer2 = $(questionId).val();
               //  
                answers[`qn${i}`] = answer1+','+answer2;
            }else if (i === 18 || i === 19 || i === 20) { // Handle radio button questions
                questionId = `#qn${i}-answer`;
                answer = $(questionId + ' input[type=radio]:checked').val();
                answers[`qn${i}`] = answer || ''; // Use an empty string if no answer is selected
            }else{
                questionId = `#qn${i}-answer`;
                answer = $(questionId).val();
                answers[`qn${i}`] = answer;
            }
            
        }


        return answers;
    }
    // console.log(collectAnswers())


});