/**
 * Puredash Cleaner JS
 */
jQuery(document).ready(function($) {
    var currentBlockId = null;

    // --- LABEL EDITING ---
    $('.cleanview-edit-label-link').on('click', function(e) {
        e.preventDefault();
        var row = $(this).closest('tr');
        currentBlockId = row.data('id');
        var currentLabel = row.find('.cleanview-label-text').text();

        $('#cleanview-new-label-input').val(currentLabel);
        $('#cleanview-label-popup').fadeIn(200);
        $('#cleanview-new-label-input').focus();
    });

    $('#cleanview-label-cancel').on('click', function() {
        $('#cleanview-label-popup').fadeOut(200);
        currentBlockId = null;
    });

    $('#cleanview-label-save').on('click', function() {
        var newLabel = $('#cleanview-new-label-input').val().trim();
        if (!newLabel || !currentBlockId) return;

        var btn = $(this);
        btn.prop('disabled', true).text(puredashSettingsI18n.saving);

        $.post(ajaxurl, {
            action: 'puredash_update_block_label',
            security: puredashSettingsI18n.nonce,
            id: currentBlockId,
            label: newLabel
        }, function(response) {
            btn.prop('disabled', false).text(puredashSettingsI18n.save);
            if (response.success) {
                $('tr[data-id="' + currentBlockId + '"] .cleanview-label-text').text(newLabel);
                $('#cleanview-label-popup').fadeOut(200);
                if (window.CleanViewToast) {
                    window.CleanViewToast.success(response.data.message);
                }
                currentBlockId = null;
            } else {
                if (window.CleanViewToast) {
                    window.CleanViewToast.error(response.data.message);
                }
            }
        });
    });

    // Delete handler
    $('.cleanview-delete-btn').on('click', function(e) {
        e.preventDefault();
        
        var btn = $(this);
        var id = btn.data('id');

        if (window.CleanViewModal) {
            window.CleanViewModal.open({
                type: 'confirm',
                title: puredashSettingsI18n.deleteConfirmTitle,
                message: puredashSettingsI18n.deleteConfirmMsg,
                i18n: {
                    cancel: puredashSettingsI18n.cancel,
                    confirm: puredashSettingsI18n.deleteConfirmBtn
                },
                onConfirm: function() {
                    $.post(ajaxurl, {
                        action: 'puredash_delete_block',
                        security: puredashSettingsI18n.nonce,
                        id: id
                    }, function(response) {
                        if (response.success) {
                            btn.closest('tr').fadeOut();
                            if (window.CleanViewToast) {
                                window.CleanViewToast.success(puredashSettingsI18n.blockDeleted);
                            }
                            setTimeout(function() {
                                window.location.reload();
                            }, 1000);
                        } else {
                            if (window.CleanViewToast) {
                                window.CleanViewToast.error(response.data.message);
                            }
                        }
                    });
                }
            });
        }
    });

    // Close popup on escape
    $(document).on('keydown', function(e) {
        if (e.key === 'Escape') {
            if ($('#cleanview-label-popup').is(':visible')) {
                $('#cleanview-label-popup').fadeOut(200);
                currentBlockId = null;
            }
        }
    });

    // Documentation nav
    if ( $('.cleanview-doc-nav').length ) {
        $('.cleanview-doc-nav a').on('click', function(e) {
            $('.cleanview-doc-nav a').removeClass('active');
            $(this).addClass('active');
        });
    }
});
