// icon-upload.js
jQuery(document).ready(function($) {
    $('#rzcps-icon-upload').on('click', function() {
        $('#rzcps-icon-file').click();
    });

    $('#rzcps-icon-file').on('change', function() {
        var file_data = $(this).prop('files')[0];
        if (!file_data) return;

        var form_data = new FormData();
        form_data.append('file', file_data);
        form_data.append('action', 'rzcps_upload_custom_icon');
        form_data.append('_wpnonce', rzcpIconData.nonce);

        $.ajax({
            url: ajaxurl,
            type: 'POST',
            contentType: false,
            processData: false,
            data: form_data,
            success: function(response) {
                if (response.success) {
                    $('#rzcps-icon-grid').append(response.data.html);
                } else {
                    alert(response.data.message);
                }
            }
        });
    });

    $(document).on('click', '.rzcps-remove-icon', function() {
        var $item = $(this).closest('.rzcps-icon-item');
        var id = $(this).data('id');
        if (!confirm('Remove this icon?')) return;

        $.post(ajaxurl, {
            action: 'rzcps_delete_custom_icon',
            attach_id: id,
            _wpnonce: rzcpIconData.nonce
        }, function(response) {
            if (response.success) {
                $item.remove();
            } else {
                alert('Failed to remove icon.');
            }
        });
    });
});
