$(function() {
    $('select[autocomplete="off"]').each(function() {
        let selectedValue = $(this).find('option[selected]').val();
        if (selectedValue) {
            $(this).val(selectedValue);
        }
    });

    function updateMaxlengths(element) {
        const row = $(element).closest('tr');
        const passportType = row.find('.passport-type-select').val();

        let maxLength;
        switch (passportType) {
            case 'd':
                maxLength = 17;
                break;
            case 'k':
                maxLength = 25;
                break;
            default:
                maxLength = 17;
        }

        row.find('.first-name-input, .last-name-input, .location-input').attr('maxlength', maxLength);
    }

    $('.passport-type-select').each(function() {
        updateMaxlengths(this);
    });

    $(document).on('change', '.passport-type-select', function() {
        updateMaxlengths(this);
    });
});

function addApplication(){

    let data = {};
    data.district_region = $('#region_new').val();
    data.passport_type = $('#passport_type_new').val();
    data.mail = $('#mail_new').val();
    data.first_name = $('#first_name_new').val();
    data.last_name = $('#last_name_new').val();
    data.location = $('#location_new').val();
    data.notes = $('#notes_new').val();

    console.log(data);

    $.ajax({
        type: "POST",
        url: "/intern/antraege/hinzufuegen",
        data: data,
        dataType: "json",
        success:function(r){
            if(r.messageStatus === "success"){
                $('#mail_new').val("");
                $('#first_name_new').val("");
                $('#last_name_new').val("");
                $('#location_new').val("");
                $('#notes_new').val("");
                location.reload();
            } else {
                alert(r.errorMessage);
            }
        },
        error:function(r, a, e){
            alert(i18n.common.error_occurred + e);
        }
    });
}

function deleteApplication(applicationId){
    let data = {};
    data.id = applicationId;

    console.log(data);

    if(window.confirm(i18n.blog.confirm_delete)){
        $.ajax({
            type: "DELETE",
            url: "/intern/antraege/loeschen",
            data: data,
            dataType: "json",
            success:function(r){
                if(r.messageStatus === "success"){
                    $('#application_' + applicationId).fadeOut(300, function() {
                        $(this).remove();
                    });
                } else {
                    alert(r.errorMessage);
                }
            },
            error:function(r, a, e){
                alert(i18n.common.error_occurred + e);
            }
        });
    }
}

function editApplication(applicationId){
    let data = {};
    data.id = applicationId;
    data.first_name = $('#first_name_'+applicationId).val();
    data.last_name = $('#last_name_'+applicationId).val();
    data.location = $('#location_'+applicationId).val();
    data.notes = $('#notes_'+applicationId).val();
    data.mail = $('#mail_'+applicationId).val();
    data.passport_type = $('#passport_type_'+applicationId).val();

    console.log(data);

    if(window.confirm(i18n.blog.confirm_save)){
        $.ajax({
            type: "PUT",
            url: "/intern/antraege/bearbeiten",
            data: data,
            dataType: "json",
            success:function(r){
                if(r.messageStatus === "success"){
                    location.reload();
                } else {
                    alert(r.errorMessage);
                }
            },
            error:function(r, a, e){
                alert(i18n.common.error_occurred + e);
            }
        });
    }
}
