var jq_admin_appdbrt = jQuery.noConflict();
jq_admin_appdbrt(function () {
    jq_admin_appdbrt(document).on('click', '.change_add_button', function () {
        jq_admin_appdbrt(this).closest('.add_language_div').find('.details_enter_div').toggleClass('hide_it');
    });
    jq_admin_appdbrt(document).on('click', '.lang_add_button', function () {
        if (jq_admin_appdbrt('.language_select_options_new').val() != '') {
            jq_admin_appdbrt(this).closest('.add_language_div').find('.details_enter_div').toggleClass('hide_it');
        } else {
            jq_admin_appdbrt(this).closest('.add_language_div').find('.details_enter_div').addClass('hide_it');
        }
    });
    jq_admin_appdbrt(document).on('click', '.close_btn_lang', function () {
        jq_admin_appdbrt(this).closest('.add_language_div').find('.details_enter_div').toggleClass('hide_it');
    });
    jq_admin_appdbrt(document).on('click', '.close_popup_btn', function () {
        jq_admin_appdbrt('#langaugeModal').hide();
        location.reload();
    });

    jq_admin_appdbrt(document).on('click', '.save_new_translation', function () {
        if (obj.total_avaible >= obj.total_allowed) {
            alert('Not allow to add more languages');
            return '';
        }

        var btn = jq_admin_appdbrt(this);
        btn.attr('disabled', true);
        var translation_code = jq_admin_appdbrt('.add_language_div_section').find('.language_select_options').val();
        var title_translation = jq_admin_appdbrt('.add_language_div_section').find('.title_translation').val();
        var placeholder_translation = jq_admin_appdbrt('.add_language_div_section').find('.placeholder_translation').val();
        var default_message_translation = jq_admin_appdbrt('.add_language_div_section').find('.default_message_translation').val();
        var default_options_translation = jq_admin_appdbrt('.add_language_div_section').find('.default_options_translation').val();
        jq_admin_appdbrt.ajax({
            url: obj.ajaxurl,
            type: 'post',
            dataType: 'json',
            data: {
                action: 'doobert_add_new_translation_data',
                security: obj.security,
                translation_code, title_translation, placeholder_translation, default_message_translation, default_options_translation
            },
            success: function (res) {
                btn.attr('disabled', false);
                if (res.status == 1) {
                    jq_admin_appdbrt('.error_response').hide();
                    jq_admin_appdbrt('.success_response').show();
                    jq_admin_appdbrt('#langaugeModal').show();
                } else {
                    jq_admin_appdbrt('.success_response').hide();
                    jq_admin_appdbrt('.error_response').show();
                    jq_admin_appdbrt('.error_msgs_response').html(res.message);
                    jq_admin_appdbrt('#langaugeModal').show();
                }
            }
        });
    });


    jq_admin_appdbrt(document).on('click', '.edit_translation', function () {
        var btn = jq_admin_appdbrt(this);
        btn.attr('disabled', true);
        var id_to_update = jq_admin_appdbrt(this).attr('data-id');
        var parent = jq_admin_appdbrt(this).closest('.form-table');
        var title_translation = parent.find('.title_translation').val();
        var placeholder_translation = parent.find('.placeholder_translation').val();
        var default_message_translation = parent.find('.default_message_translation').val();
        var default_options_translation = parent.find('.default_options_translation').val();
        var status = parent.find('.language_status').val();
        jq_admin_appdbrt.ajax({
            url: obj.ajaxurl,
            type: 'post',
            dataType: 'json',
            data: {
                action: 'doobert_update_translation_data',
                security: obj.security,
                id_to_update, title_translation, placeholder_translation, default_message_translation, default_options_translation, status
            },
            success: function (res) {
                btn.attr('disabled', false);
                if (res.status == 1) {
                    jq_admin_appdbrt('.error_response').hide();
                    jq_admin_appdbrt('.success_response').show();
                    jq_admin_appdbrt('#langaugeModal').show();
                } else {
                    jq_admin_appdbrt('.success_response').hide();
                    jq_admin_appdbrt('.error_response').show();
                    jq_admin_appdbrt('.error_msgs_response').html(res.message);
                    jq_admin_appdbrt('#langaugeModal').show();
                }
            }
        });
    });

});

   jq_admin_appdbrt('.delete-pdf').on('click', function(e){
        e.preventDefault();
        if(!confirm('Are you sure you want to delete this PDF?')) return;
        var button = jq_admin_appdbrt(this);
        var pdfID = button.data('id');
        var $row  = jq_admin_appdbrt(this).closest('tr');
        jq_admin_appdbrt.ajax({
            url: obj.ajaxurl,
            type: 'post',
            data: {
                action: 'doobert_delete_pdf_file',
                pdf_id: pdfID,
                security: obj.security
            },
            success: function(response){
                if(response.success){
                $row.fadeOut(300, function(){ jq_admin_appdbrt(this).remove(); });
                var rowCount = jq_admin_appdbrt('#pdf-list tbody tr').length;
                if (rowCount < 11) {
                    jq_admin_appdbrt('.upload_files_btn').removeClass('disabled');
                }
                if(rowCount ==1) {
                    jq_admin_appdbrt("#pdf-list tbody").append('<tr class="no-pdf"><td colspan="2" style="text-align:center;">No PDFs found.</td></tr>');
                }
                } else {
                    alert(response.data || 'Failed to delete PDF.');
                }
            },
            error: function(){
                alert('Delete PDF request failed.');
            }
        });
    });

jq_admin_appdbrt('.upload_files_btn').click(function () {
    jq_admin_appdbrt('#doobert_pdf_upload').click();
});

jq_admin_appdbrt('#doobert_pdf_upload').change(function () {
    var input = jq_admin_appdbrt(this);
    const file = this.files[0];
    if (file) {

        if (file.type !== 'application/pdf') {
            jq_admin_appdbrt('.doobert_selected_file_error').html('Please select a PDF file.');
            input.val('');
            jq_admin_appdbrt('.doobert_selected_file').html('');
            return;
        }

        const maxSize = 20 * 1024 * 1024; // 20MB in bytes

        if (file.size > maxSize) {
            jq_admin_appdbrt('.doobert_selected_file_error').html('Selected file exceeds the maximum allowed size of 20MB.');
            input.val('');
            jq_admin_appdbrt('.doobert_selected_file').html('');
            return;
        }
        var filename = file.name;
        var filename = filename.split('.').slice(0, -1).join('.');
        jq_admin_appdbrt('.doobert_selected_file_error').html('');
        jq_admin_appdbrt('.doobert_selected_file').html(file.name + ' uploading...');
        jq_admin_appdbrt('.upload_files_btn').attr('disabled', true);
        var file_data = jq_admin_appdbrt("#doobert_pdf_upload").prop('files')[0];
        var language_for_pdf = jq_admin_appdbrt('#language_for_pdf').val();
        form_data = new FormData();
        form_data.append('file', file_data);
        form_data.append('action', 'doobert_upload_pdf_file');
        form_data.append('languages', language_for_pdf);
        form_data.append('filename', filename);
        form_data.append('security', obj.security);

        jq_admin_appdbrt.ajax({
            url: obj.ajaxurl,
            type: 'POST',
            contentType: false,
            processData: false,
            data: form_data,
            success: function (response) {
                if (response == 0) {
                    alert('some error occured during upload');
                    location.reload();
                }
                location.reload();
                jq_admin_appdbrt('.doobert_selected_file').html('Uploaded');
                jq_admin_appdbrt('.upload_files_btn').attr('disabled', false);
                var select2Dropdown = jq_admin_appdbrt('#language_for_pdf');
                select2Dropdown.val(null).trigger('change');
                setTimeout(function () {
                    jq_admin_appdbrt('.doobert_selected_file').html('');
                }, 2000);
            }
        });
    }
});