<?php
/**
 * Plugin Name: VMO HTML to PDF Converter
 * Description: A standalone professional publishing tool to convert dynamic HTML into interactive eBooks and PDFs with working hyperlinks, media support, and TOC.
 * Version: 2.0.0
 * Author: VMO Business Suite
 */

if (!defined('ABSPATH')) exit;

class VMO_HTML_To_PDF {
    public function __construct() {
        // High priority (20) ensures this runs AFTER the main menu is created by the parent plugin
        add_action('admin_menu', array($this, 'add_menu_page'), 20);
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    public function add_menu_page() {
        // Add as submenu under "VMO AI Builder"
        add_submenu_page(
            'vmo-ai-builder-main',
            'PDF Publisher',       // Page Title
            'PDF Publisher',       // Menu ID
            'read',                // Capability: Allow standard users
            'vmo-html-to-pdf',
            array($this, 'render_page')
        );
    }

    public function enqueue_scripts($hook) {
        // Check if we are on the correct page (allowing for different parent slugs)
        $screen = get_current_screen();
        if (strpos($screen->id, 'vmo-html-to-pdf') === false) return;
        
        // Plugin Directory URL
        $plugin_url = plugin_dir_url(__FILE__);

        // Enqueue html2pdf.js from local vendor bundle
        wp_enqueue_script('html2pdf', VMO_AI_BUILDER_URL . 'assets/vendor/html2pdf.bundle.min.js', array(), '0.10.1', true);
        
        // Custom Editor Assets
        wp_enqueue_style('vmo-pdf-editor', $plugin_url . 'editor.css', array(), '3.0.0');
        wp_enqueue_script('vmo-pdf-editor', $plugin_url . 'editor.js', array('html2pdf'), '3.0.0', true);
    }

    public function render_page() {
        // Default Template for "Interactive Book"
        $default_html = <<<HTML
<!-- BOOK COVER -->
<div style="height:1000px; display:flex; flex-direction:column; justify-content:center; align-items:center; background:#1e293b; color:white; text-align:center; page-break-after:always;">
    <div style="width:100px; height:100px; background:#06b6d4; border-radius:50%; margin-bottom:20px; font-size:50px; line-height:100px;">🚀</div>
    <h1 style="font-size:3em; margin:0;">The Digital Shift</h1>
    <h3 style="font-weight:300; opacity:0.8;">Interactive Interactive VMO Guide</h3>
    <br>
    <p>Generated by VMO PDF Publisher</p>
</div>

<!-- INTRODUCTION -->
<h1 id="intro">Introduction</h1>
<p style="font-size:18px; line-height:1.6;">
    Welcome to this fully interactive document. Unlike standard PDFs, this document preserves 
    <a href="https://google.com" style="color:#06b6d4; text-decoration:none;">hyperlinks</a>, 
    generates a functional Table of Contents, and transforms embedded media into accessible resources.
</p>

<!-- MEDIA & ANIMATION -->
<h2 id="media">Embedded Media & Animation</h2>
<p>The system automatically detects video and audio tags and converts them into "Smart Media Cards" for the PDF.</p>

<div style="padding:20px; border:1px solid #ddd; border-radius:8px; margin:20px 0; background:#f8fafc;">
    <strong>Example Audio Embed:</strong>
    <br><br>
    <audio controls src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.min.mp3"></audio>
</div>

<!-- PARALLAX / VISUAL LAYOUT -->
<h2 id="visuals">Visual Layouts</h2>
<div style="background: url('https://picsum.photos/800/200') no-repeat center center; background-size: cover; height: 200px; display:flex; align-items:center; justify-content:center; color:white; font-size:24px; text-shadow:0 2px 4px rgba(0,0,0,0.8); border-radius:8px;">
    Rich Backgrounds Preserved
</div>

<!-- COLUMNS -->
<h2 id="layout">Multi-Column Layout</h2>
<div style="display:flex; gap:20px;">
    <div style="flex:1; padding:15px; background:#f1f5f9; border-radius:4px;">
        <h3>Column A</h3>
        <p>Full flexbox support allows for complex magazine-style layouts.</p>
    </div>
    <div style="flex:1; padding:15px; background:#e2e8f0; border-radius:4px;">
        <h3>Column B</h3>
        <p>Images, text, and vectors render with high fidelity.</p>
    </div>
</div>
HTML;
        
        $vmo_icon = defined('VMO_PLUGIN_URL') ? VMO_PLUGIN_URL . 'assets/vmo-icon.svg' : '';
        $icon_html = $vmo_icon ? '<img src="' . esc_url($vmo_icon) . '" style="height:40px; vertical-align:middle; margin-right:10px;">' : '📚 ';
        
        ?>
        <div class="wrap">
            <h1><?php echo esc_html( $icon_html ); ?>VMO PDF Publisher</h1>
            <p>Convert HTML into professional interactive eBooks and documents.</p>
            
            <div class="vmo-pdf-wrapper">
                <!-- Toolbar -->
                <div class="vmo-pdf-toolbar">
                    <div class="vmo-tool-group">
                        <button class="vmo-btn primary" id="btn-generate" onclick="vmoGeneratePDF()">⬇️ Publish to PDF</button>
                    </div>
                    
                    <div class="vmo-tool-group">
                        <button class="vmo-btn" onclick="vmoUpdatePreview()">🔄 Refresh Preview</button>
                        <select id="vmo-doc-type" class="vmo-select" onchange="vmoUpdateConfig('docType', this.value); vmoUpdatePreviewStyles();" style="padding:6px; background:#334155; color:white; border:1px solid #475569; border-radius:4px;">
                            <option value="auto">✨ Auto-Detect (Exact Fit)</option>
                            <option value="book">📘 Book (A4 Paginated)</option>
                            <option value="webpage">🌐 Continuous Scroll</option>
                            <option value="form">📝 Form / Certificate</option>
                        </select>
                         <button class="vmo-btn" id="btn-orientation" onclick="vmoToggleOrientation()">🔄 Orientation: Portrait</button>
                    </div>

                    <div class="vmo-tool-group" style="border:none; display:flex; flex-direction:column; align-items:flex-start; gap:5px;">
                        <input type="text" placeholder="Proposal Email (for forms)" style="padding:5px; font-size:12px; width:200px; background:#334155; border:1px solid #475569; color:white;" onchange="vmoUpdateConfig('contactEmail', this.value)">
                        <input type="text" placeholder="Live Web Version URL (Optional)" style="padding:5px; font-size:12px; width:200px; background:#334155; border:1px solid #475569; color:white;" onchange="vmoUpdateConfig('liveUrl', this.value)">
                        
                        <div style="font-size:12px; color:#94a3b8; margin-top:5px;">
                         <label class="vmo-checkbox"><input type="checkbox" checked disabled> Smart Form Links</label>
                         <label class="vmo-checkbox" title="Fixes Locomotive Scroll/GSAP layout issues"><input type="checkbox" checked disabled> Flatten Anim/Parallax</label>
                        </div>
                    </div>
                </div>

                <!-- Workspace -->
                <div class="vmo-pdf-workstation">
                    <!-- Editor -->
                    <div class="vmo-editor-pane">
                        <textarea id="vmo-pdf-code" class="vmo-code-editor" spellcheck="false"><?php echo esc_textarea($default_html); ?></textarea>
                    </div>
                    
                    <!-- Preview -->
                    <div class="vmo-preview-pane">
                        <div id="vmo-pdf-preview">
                            <!-- Visual Render -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }
}

new VMO_HTML_To_PDF();

