window.padAddStudent = padAddStudent;
window.padRemoveStudent = padRemoveStudent;
export function padAddStudent(uniqueId, marker, maxCount){
    let output = document.getElementById('pad-extra-student-output-' + uniqueId);
    let button = document.getElementById('pad-add-student-button-' + uniqueId);
    let template = document.getElementById('pad-template-student-form-' + uniqueId);
    let newElementContainer = document.createElement("div");

    if (!(+output.dataset.entry_count > 0)) output.dataset.entry_count = '0';
    let count = +output.dataset.entry_count;
    if (count >= +maxCount) {
        button.setAttribute('disabled', true);
        return
    }

    if (!(+output.dataset.current_index > 0)) output.dataset.current_index = '0';
    let index = +output.dataset.current_index;


    newElementContainer.innerHTML = template.innerHTML.replaceAll(marker, index.toString());
    newElementContainer.firstElementChild.style.display = "none";
    let id = newElementContainer.firstElementChild.id;

    output.dataset.entry_count = (count + 1).toString();
    output.dataset.current_index = (index + 1).toString();

    if (count + 1 >= +maxCount) {
        button.setAttribute('disabled', true);
    }

    output.append(newElementContainer.firstElementChild);

    jQuery("#" + id).show("slow");
}

export function padRemoveStudent(id, uniqueId){
    let output = document.getElementById('pad-extra-student-output-' + uniqueId);
    let button = document.getElementById('pad-add-student-button-' + uniqueId);

    if (!(+output.dataset.entry_count > 1)) output.dataset.current_index = '1';
    let index = +output.dataset.entry_count;
    output.dataset.entry_count = (index - 1).toString();

    button.removeAttribute('disabled');

    jQuery("#" + id).hide(600, function (){
        document.getElementById(id).remove();
    });
}