/**
 * Created by David Camacho on 21/04/2015.
 */


// Add question button event
function saveQuestion(id_test) {

    insertQuestion(id_test, $('#points_input').val(),$('#question_input').val() );
}

function refreshQuestionList(){

    // ajax call starts
    $.ajax({

        type: "POST",
        url: './modules/teacher/coursesoptions/json_store_question.php',
        data: { "get_list" : 1,
                "id_test": $('#test_id').html()
        },
        success: printQuestionList,
        dataType: 'json'
    })


}


function insertQuestion(id_test, points, question) {


    // ajax call starts
    $.ajax({

        type: "POST",
        url: './modules/teacher/coursesoptions/json_store_question.php',
        data: { "insert" : 1,
                "id_test" : id_test,
                "points" : points,
                "question" : question},
        success: insertQuestionSuccess,
        dataType: 'json'
    })

}

function insertQuestionSuccess(ajaxResponse){

    if (ajaxResponse == false) {
        $('#question_form_alerts').empty();
        $('#question_form_alerts').css("display", "block");
        $('#question_form_alerts').append("<div class='small-12 large-12 alert-box alert' >Question not inserted. Check fields</div>");

    }

    refreshQuestionList();
}


function printQuestionList(ajaxResponse){

    $('#questions_list').empty();

    // If no results
    if (ajaxResponse["id_question"].length < 1) {
        $('#questions_list').append('<div class="row" >No questions</div>');
    }else {
        for (i=0; i<ajaxResponse["id_question"].length; i++) {
            $('#questions_list').append('<div class="row panel" >' +
            '<div class="small-12 large-12 columns">' +
            '<div class="columns" style=" width: 15px;" onclick="removeQuestion('+  ajaxResponse["id_question"][i]+')">' +
            '<i class="fi-x size-18"></i></div><div class="columns" style="width:600px;float:left;">' +
            '<p style="padding-top: 1px;  float: left; width: 406px; font-weight:bolder;">(' + ajaxResponse["points"][i] + ' points)' + ajaxResponse["question"][i] + '</p>' +
            '<form action="main.php" method="post">'+
            '<input type="hidden" name="id_test" value="' + $('#test_id').html() +'">' +
            '<input type="hidden" name="id_question" value="' + ajaxResponse["id_question"][i] + '">' +
            '<input type="hidden" name="test_option" value="1">' +
            '<input type="hidden" name="step" value="'+ $('#step').html() +'">' +
            '<div class="button" onclick="$(this).closest(\'form\').submit()" style="height: 40px; padding: 11px;" >' +
            '<i class="fi-page-edit size-18"></i>' +

            'Edit answers' +

            '</div>' +
            '</form>' +
            '</div>' +
            '</div>' +
            '</div>');
        }

    }
}

function removeQuestion(id_question){
    // ajax call starts
    $.ajax({

        type: "POST",
        url: './modules/teacher/coursesoptions/json_store_question.php',
        data: { "remove" : 1,
            "id_question" : id_question
        },
        success: removeQuestionSuccess,
        dataType: 'json'
    });




}

function removeQuestionSuccess(ajaxResponse){

    refreshQuestionList();

}
