<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="pbuaiv-wrap">
    <div class="pbuaiv-page-header">
        <h1 class="pbuaiv-page-title">🇦🇪 <?php esc_html_e('Invoice Templates','pb-invoicing-system'); ?></h1>
        <p class="pbuaiv-page-subtitle"><?php esc_html_e('Choose and preview your document templates.','pb-invoicing-system'); ?></p>
    </div>

    <div class="pbuaiv-templates-layout">
        <!-- Left: template picker -->
        <div class="pbuaiv-templates-sidebar">
            <div class="pbuaiv-card">
                <div class="pbuaiv-card__head"><h2><?php esc_html_e('Document Type','pb-invoicing-system'); ?></h2></div>
                <div class="pbuaiv-card__body">
                    <select class="pbuaiv-select" id="pbuaiv-preview-doc-type">
                        <option value="invoice"><?php esc_html_e('Tax Invoice','pb-invoicing-system'); ?></option>
                        <option value="receipt"><?php esc_html_e('Receipt','pb-invoicing-system'); ?></option>
                        <option value="credit_note"><?php esc_html_e('Credit Note','pb-invoicing-system'); ?></option>
                        <option value="proforma"><?php esc_html_e('Proforma','pb-invoicing-system'); ?></option>
                    </select>
                </div>
            </div>

            <div class="pbuaiv-card">
                <div class="pbuaiv-card__head"><h2><?php esc_html_e('Template','pb-invoicing-system'); ?></h2></div>
                <div class="pbuaiv-card__body">
                    <?php $pbuaiv_current_template = get_option('pbuaiv_default_template','modern'); ?>
                    <div class="pbuaiv-template-picker-compact">
                        <?php foreach(['modern'=>'Modern','classic'=>'Classic','minimal'=>'Minimal','corporate'=>'Corporate'] as $pbuaiv_template_key => $pbuaiv_template_label) : ?>
                            <label class="pbuaiv-template-opt <?php echo $pbuaiv_current_template===$v ? 'pbuaiv-template-opt--active':'' ?>">
                                <input type="radio" name="pbuaiv_default_template" value="<?php echo esc_attr( $pbuaiv_template_key ); ?>" <?php checked($pbuaiv_current_template,$pbuaiv_template_key); ?> />
                                <div class="pbuaiv-template-mini pbuaiv-template-mini--<?php echo esc_attr( $pbuaiv_template_key ); ?>"></div>
                                <span><?php echo esc_html( $pbuaiv_template_label ); ?></span>
                            </label>
                        <?php endforeach; ?>
                    </div>
                    <button type="button" class="pbuaiv-btn pbuaiv-btn--primary pbuaiv-btn--wide" id="pbuaiv-preview-btn" style="margin-top:14px;">
                        <?php esc_html_e('Preview','pb-invoicing-system'); ?>
                    </button>
                    <button type="button" class="pbuaiv-btn pbuaiv-btn--secondary pbuaiv-btn--wide" id="pbuaiv-save-template" style="margin-top:8px;">
                        <?php esc_html_e('Set as Default','pb-invoicing-system'); ?>
                    </button>
                </div>
            </div>

            <div class="pbuaiv-card">
                <div class="pbuaiv-card__head"><h2><?php esc_html_e('Custom PHP Override','pb-invoicing-system'); ?></h2></div>
                <div class="pbuaiv-card__body">
                    <p class="pbuaiv-help"><?php esc_html_e('Copy any template to your theme\'s folder to override it:','pb-invoicing-system'); ?></p>
                    <code style="display:block;padding:10px;background:#f8fafc;border-radius:6px;font-size:11px;word-break:break-all;"><?php echo esc_html(get_template_directory()); ?>/pbuaiv-templates/invoice/modern.php</code>
                </div>
            </div>
        </div>

        <!-- Right: preview iframe -->
        <div class="pbuaiv-templates-preview">
            <div class="pbuaiv-preview-bar">
                <span class="pbuaiv-preview-bar__title"><?php esc_html_e('Template Preview','pb-invoicing-system'); ?></span>
                <span class="pbuaiv-preview-bar__hint"><?php esc_html_e('Based on your most recent order','pb-invoicing-system'); ?></span>
            </div>
            <div class="pbuaiv-preview-frame-wrap">
                <div id="pbuaiv-preview-loading" style="display:none;padding:40px;text-align:center;color:#94a3b8;">
                    <span class="dashicons dashicons-update pbuaiv-spin"></span> <?php esc_html_e('Generating preview...','pb-invoicing-system'); ?>
                </div>
                <div id="pbuaiv-preview-placeholder" style="padding:60px 20px;text-align:center;color:#94a3b8;">
                    <span style="font-size:40px;display:block;margin-bottom:12px;">🧾</span>
                    <p style="font-size:13px;margin:0;"><?php esc_html_e('Select a template and click Preview','pb-invoicing-system'); ?></p>
                </div>
                <iframe id="pbuaiv-preview-iframe" class="pbuaiv-preview-iframe" sandbox="allow-same-origin allow-scripts" style="display:none;"></iframe>
            </div>
        </div>
    </div>
</div>
