tinymce.create("tinymce.plugins.proqyz_email_templates_tinymce", {
    init: function(e, url) {
        
        e.addButton("proqyz_email_templates_tinymce", {
            title: "Email Templates",
            icon: "icon dashicons-email",
            onclick: function() {
                const modalHTML = `
                <div id="proqyz_email-modal" class="proqyz_email-modal fade show" tabindex="-1" aria-proqyz_email-modal="true" role="dialog" style="display: block;">
                    <div class="proqyz_email-modal-dialog">
                        <div class="proqyz_email-modal-content">
                            <div class="proqyz_email-modal-header">
                                <h3 class="proqyz_email-modal-title">proqyz_email-modal title</h3>
                                
                                
                                <button type="button" class="proqyz_modal-x-btn">
                                    <span class="dashicons dashicons-no"></span>                           
                                </button>

                            </div>

                            <div class="proqyz_email-modal-body">
                                
                            </div>

                            <div class="proqyz_email-modal-footer">
                                
                            </div>
                        </div>
                    </div>
                </div>
                `;

                document.body.insertAdjacentHTML('beforeend', modalHTML);

                jQuery(document).on('click','.proqyz_modal-x-btn', function(){
                    jQuery('#proqyz_email-modal').remove();
                });
                

                jQuery.ajax({
                    url: proqyz_tinymce_email_templates.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'proqyz_get_email_templates'
                    },
                    success: function(res) {
                    
                        
                        if(res.has_html){
                            jQuery('#proqyz_email-modal .proqyz_email-modal-body').html(res.html);
                        }

                        // Handle template insertion
                        /*
                        editor.insertContent(selectedTemplate);
                        editor.windowManager.close(); 
                        */
                        
                    },
                    error: function() {
                        jQuery('#template-list').html('Failed to load templates.');
                    }
                });
            }
        })
    },
    createControl: function(n, e) {
        return null
    }
}),
tinymce.PluginManager.add("proqyz_email_templates_tinymce", tinymce.plugins.proqyz_email_templates_tinymce);

