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

// Function that search student names and displays in a list
function searchStudentName(name) {

    // ajax call starts
    if (name.length>2){
        $.ajax({

            type: "POST",
            url: './modules/teacher/coursesoptions/json_course_inscriptions.php',
            data: {"name": name,
                    "search" : 1},
            success: searchStudentNameSuccess,
            dataType: 'json'
        })
    }else{
        document.getElementById('found_box').style.display = 'block';
        $('#found_box_results').empty();
        $('#found_box_results').append("<p> Must put at least 3 letters.</p>");

    }
}

function searchStudentNameSuccess(ajaxResponse) {

    document.getElementById('found_box').style.display = 'block';
    $('#found_box_results').empty();


    // If no results
    if (ajaxResponse["name"].length < 1) {
        $('#found_box_results').append("<p> No Students found</p>");

        // If results
    } else {
        // Add inscription buttons
        for (i = 0; i < ajaxResponse["name"].length; i++) {
            $('#found_box_results').append('<div class="row" id="student_inscription_' + ajaxResponse["id_user"][i] + '"  ><div class="small-12 large-12 columns"><div style="width: 15px;" class="columns" onclick="addStudentInscription(' + ajaxResponse["id_user"][i] + ', \'' + ajaxResponse["name"][i] + '\')"> <i class="fi-plus size-18"></i></div><div class="columns" style="width:500px;float:left;"><p style="padding-top: 1px;">' + ajaxResponse["name"][i] + '</p></div></div></div>');
        }
    }
}


// Add student inscription button event
function addStudentInscription(id_student, name) {


    insertStudentInscription(id_student, $('#course_id').html());



}

function refreshCourseInscriptionList(){

    // ajax call starts
    $.ajax({

        type: "POST",
        url: './modules/teacher/coursesoptions/json_course_inscriptions.php',
        data: { "get_list" : 1,
                "id_course": $('#course_id').html()
        },
        success: printList,
        dataType: 'json'
    })


}


function insertStudentInscription(id_student, id_course) {

    // ajax call starts
    $.ajax({

        type: "POST",
        url: './modules/teacher/coursesoptions/json_course_inscriptions.php',
        data: { "insert" : 1,
                "id_user" : id_student,
                "id_course" : id_course},
        success: insertSuccess,
        dataType: 'json'
    })

}

function insertSuccess(ajaxResponse){
    refreshCourseInscriptionList();
}


function printList(ajaxResponse){

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

    // If no results
    if (ajaxResponse["name"].length < 1) {
        $('#course_inscription_list').append('<div class="row" >No students with inscription.</div>');
    }else {
        for (i=0; i<ajaxResponse["name"].length; i++) {
            $('#course_inscription_list').append('<div class="row" ><div class="small-12 large-12 columns"><div class="columns" style=" width: 15px;" onclick="removeStudentInscription('+  ajaxResponse["id_user"][i]+')"><i class="fi-x size-18"></i></div><div class="columns" style="width:500px;float:left;"><p style="padding-top: 1px;">' + ajaxResponse["name"][i] + '</p></div></div></div>');
        }

    }
}

function removeStudentInscription(id_user){
    // ajax call starts
    $.ajax({

        type: "POST",
        url: './modules/teacher/coursesoptions/json_course_inscriptions.php',
        data: { "remove" : 1,
            "id_user" : id_user,
            "id_course" : $('#course_id').html()},
        success: insertSuccess,
        dataType: 'json'
    });

    refreshCourseInscriptionList();

}