<?php ob_start(); ?>
<?php
/**
 * Admin View: Mail Template Editor (Phase 4 – v3.3.0)
 */
?>
<link rel="stylesheet" href="/admin/assets/editor/jodit.min.css">

<?php admin_content_header(
    e($template['name']),
    'mail',
    [],
    __('admin.mail.template_body')
) ?>

<div style="margin-bottom: 1rem;">
    <a href="/admin/settings/mail?tab=templates" class="btn btn-secondary btn-sm">
        <?= icon('arrow-left', 14) ?> <?= __('admin.mail.template_back') ?>
    </a>
</div>

<form method="POST" action="/admin/settings/mail/template/<?= (int)$template['id'] ?>/save" id="template-edit-form">
    <?= csrf_field() ?>

    <div class="mail-editor-layout">
        <!-- Main editor -->
        <div class="mail-editor-main">
            <div class="form-card">
                <h4><?= __('admin.mail.template_subject') ?></h4>
                <div class="form-group">
                    <input type="text" name="subject" id="template-subject" value="<?= e($template['subject']) ?>" required>
                </div>
            </div>

            <div class="form-card">
                <h4><?= __('admin.mail.template_body') ?></h4>
                <div class="form-group">
                    <textarea name="html_body" id="template-body" rows="20" style="min-height: 400px;"><?= e($template['html_body']) ?></textarea>
                </div>
            </div>

            <div class="mail-action-bar">
                <button type="submit" class="btn btn-primary">
                    <?= icon('save', 16) ?> <?= __('admin.mail.template_save') ?>
                </button>
                <a href="/admin/settings/mail?tab=templates" class="btn btn-secondary">
                    <?= __('admin.mail.template_back') ?>
                </a>
            </div>
        </div>

        <!-- Variables sidebar -->
        <div class="mail-editor-sidebar">
            <div class="form-card">
                <h4><?= __('admin.mail.template_variables') ?></h4>
                <p class="form-hint" style="margin-bottom: 0.75rem;"><?= __('admin.mail.template_var_hint') ?></p>
                <div class="mail-var-list">
                    <?php foreach ($variables as $varKey => $varLabel): ?>
                    <button type="button" class="mail-var-tag" data-var="{{<?= e($varKey) ?>}}" title="<?= e($varLabel) ?>">
                        <code>{{<?= e($varKey) ?>}}</code>
                        <span class="mail-var-label"><?= e($varLabel) ?></span>
                    </button>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</form>

<script src="/admin/assets/editor/jodit.min.js"></script>
<script>
(function () {
    var textarea = document.getElementById('template-body');
    if (!textarea || typeof Jodit === 'undefined') return;

    var editor = Jodit.make(textarea, {
        height: 500,
        language: '<?= current_locale() === 'de' ? 'de' : 'en' ?>',
        toolbarAdaptive: false,
        showCharsCounter: false,
        showWordsCounter: false,
        showXPathInStatusbar: false,
        askBeforePasteHTML: false,
        askBeforePasteFromWord: false,
        defaultActionOnPaste: 'insert_only_text',
        buttons: 'bold,italic,underline,strikethrough,|,ul,ol,|,fontsize,paragraph,|,link,table,|,align,|,source,fullsize,undo,redo',
        iframe: true,
        iframeStyle: 'body { font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #334155; line-height: 1.6; padding: 16px; }'
    });

    // Variable tag click → copy to clipboard
    document.querySelectorAll('.mail-var-tag').forEach(function (btn) {
        btn.addEventListener('click', function () {
            var varText = btn.getAttribute('data-var');
            if (navigator.clipboard) {
                navigator.clipboard.writeText(varText).then(function () {
                    if (typeof iwToast === 'function') {
                        iwToast(varText + ' kopiert', 'success', '', 2000);
                    }
                });
            }
        });
    });
})();
</script>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
