<?php ob_start(); ?>
<?php
/**
 * Admin View: Mail Settings & Templates (Phase 4 – v3.3.0)
 * Tabbed view: Settings | Templates
 */
$transport  = $mailConfig['transport'] ?? 'mail';
$activeTab  = $activeTab ?? 'settings';
?>

<?php admin_content_header(
    __('admin.mail.title'),
    'mail',
    [],
    __('admin.mail.subtitle')
) ?>

<!-- Tabs -->
<div class="iw-tabs" role="tablist">
    <button class="iw-tab <?= $activeTab === 'settings' ? 'active' : '' ?>" data-tab="tab-settings" role="tab" aria-selected="<?= $activeTab === 'settings' ? 'true' : 'false' ?>">
        <span class="iw-tab-icon"><?= icon('settings', 16) ?></span> <?= __('admin.mail.tab_settings') ?>
    </button>
    <button class="iw-tab <?= $activeTab === 'templates' ? 'active' : '' ?>" data-tab="tab-templates" role="tab" aria-selected="<?= $activeTab === 'templates' ? 'true' : 'false' ?>">
        <span class="iw-tab-icon"><?= icon('file-text', 16) ?></span> <?= __('admin.mail.tab_templates') ?>
    </button>
</div>

<!-- ═══════════════════════════════════════════════════════════════════
     TAB 1: E-Mail Settings
     ═══════════════════════════════════════════════════════════════════ -->
<div class="iw-tab-panel <?= $activeTab === 'settings' ? 'active' : '' ?>" id="tab-settings" role="tabpanel">
    <form method="POST" action="/admin/settings/mail/save" id="mail-settings-form">
        <?= csrf_field() ?>

        <!-- Transport Selection -->
        <div class="form-card">
            <h4><?= __('admin.mail.transport_title') ?></h4>
            <p class="form-card-desc"><?= __('admin.mail.transport_desc') ?></p>
            <div class="mail-transport-cards">
                <label class="mail-transport-card <?= $transport === 'mail' ? 'selected' : '' ?>" data-transport="mail">
                    <input type="radio" name="transport" value="mail" <?= $transport === 'mail' ? 'checked' : '' ?> class="sr-only">
                    <span class="mail-transport-check"><?= icon('check', 14) ?></span>
                    <span class="mail-transport-icon"><?= icon('mail', 24) ?></span>
                    <span class="mail-transport-title"><?= __('admin.mail.transport_phpmail') ?></span>
                    <span class="mail-transport-desc"><?= __('admin.mail.transport_phpmail_desc') ?></span>
                </label>
                <label class="mail-transport-card <?= $transport === 'smtp' ? 'selected' : '' ?>" data-transport="smtp">
                    <input type="radio" name="transport" value="smtp" <?= $transport === 'smtp' ? 'checked' : '' ?> class="sr-only">
                    <span class="mail-transport-check"><?= icon('check', 14) ?></span>
                    <span class="mail-transport-icon"><?= icon('server', 24) ?></span>
                    <span class="mail-transport-title"><?= __('admin.mail.transport_smtp') ?></span>
                    <span class="mail-transport-desc"><?= __('admin.mail.transport_smtp_desc') ?></span>
                    <span class="mail-transport-badge"><?= icon('check', 12) ?> <?= __('admin.mail.transport_recommended') ?></span>
                </label>
            </div>
        </div>

        <!-- Sender Settings -->
        <div class="form-card">
            <h4><?= __('admin.mail.sender_title') ?></h4>
            <div class="form-group">
                <label for="mail-from-address"><?= __('admin.mail.from_address') ?> <span class="required">*</span></label>
                <input type="email" id="mail-from-address" name="from_address" value="<?= e($mailConfig['from_address'] ?? '') ?>" placeholder="noreply@example.com">
                <div class="form-hint"><?= __('admin.mail.from_address_hint') ?></div>
            </div>
            <?php admin_field_grid_open(); ?>
                <div class="form-group">
                    <label for="mail-from-name"><?= __('admin.mail.from_name') ?> <span class="required">*</span></label>
                    <input type="text" id="mail-from-name" name="from_name" value="<?= e($mailConfig['from_name'] ?? '') ?>" placeholder="<?= e(setting('site_name', 'ITSWEBER CMS')) ?>">
                </div>
                <div class="form-group">
                    <label for="mail-reply-to"><?= __('admin.mail.reply_to') ?></label>
                    <input type="email" id="mail-reply-to" name="reply_to" value="<?= e($mailConfig['reply_to'] ?? '') ?>" placeholder="reply@example.com">
                    <div class="form-hint"><?= __('admin.mail.reply_to_hint') ?></div>
                </div>
            <?php admin_field_grid_close(); ?>
        </div>

        <!-- SMTP Settings (animated slide-in) -->
        <div class="mail-smtp-settings <?= $transport === 'smtp' ? 'visible' : '' ?>" id="smtp-settings">
            <div class="form-card form-card--smtp">
                <div class="form-card-header-row">
                    <h4><?= __('admin.mail.smtp_title') ?></h4>
                    <span class="mail-smtp-badge"><?= __('admin.mail.smtp_active') ?></span>
                </div>
                <?php admin_field_grid_open(); ?>
                    <div class="form-group">
                        <label for="smtp-host"><?= __('admin.mail.smtp_host') ?> <span class="required">*</span></label>
                        <input type="text" id="smtp-host" name="smtp_host" value="<?= e($mailConfig['smtp_host'] ?? '') ?>" placeholder="smtp.example.com">
                    </div>
                    <div class="form-group">
                        <label for="smtp-port"><?= __('admin.mail.smtp_port') ?> <span class="required">*</span></label>
                        <input type="number" id="smtp-port" name="smtp_port" value="<?= e($mailConfig['smtp_port'] ?? '587') ?>" placeholder="587" style="max-width: 140px;">
                        <div class="form-hint"><?= __('admin.mail.smtp_port_hint') ?></div>
                    </div>
                <?php admin_field_grid_close(); ?>
                <div class="form-group">
                    <label for="smtp-encryption"><?= __('admin.mail.smtp_encryption') ?></label>
                    <select id="smtp-encryption" name="smtp_encryption">
                        <option value="tls" <?= ($mailConfig['smtp_encryption'] ?? 'tls') === 'tls' ? 'selected' : '' ?>><?= __('admin.mail.smtp_encryption_tls') ?></option>
                        <option value="ssl" <?= ($mailConfig['smtp_encryption'] ?? '') === 'ssl' ? 'selected' : '' ?>><?= __('admin.mail.smtp_encryption_ssl') ?></option>
                        <option value="none" <?= ($mailConfig['smtp_encryption'] ?? '') === 'none' ? 'selected' : '' ?>><?= __('admin.mail.smtp_encryption_none') ?></option>
                    </select>
                </div>
                <?php admin_field_grid_open(); ?>
                    <div class="form-group">
                        <label for="smtp-username"><?= __('admin.mail.smtp_username') ?> <span class="required">*</span></label>
                        <input type="text" id="smtp-username" name="smtp_username" value="<?= e($mailConfig['smtp_username'] ?? '') ?>" placeholder="user@example.com">
                    </div>
                    <div class="form-group">
                        <label for="smtp-password"><?= __('admin.mail.smtp_password') ?> <span class="required">*</span></label>
                        <div class="input-password-wrap">
                            <input type="password" id="smtp-password" name="smtp_password" value="<?= e($mailConfig['smtp_password'] ?? '') ?>" placeholder="SMTP-Passwort" autocomplete="new-password">
                            <button type="button" class="password-toggle" id="smtp-password-toggle" title="<?= e(__('admin.mail.smtp_password_toggle')) ?>" aria-label="<?= e(__('admin.mail.smtp_password_toggle')) ?>">
                                <?= icon('eye', 18) ?>
                            </button>
                        </div>
                    </div>
                <?php admin_field_grid_close(); ?>
            </div>
        </div>

        <!-- Action Bar -->
        <div class="mail-action-bar">
            <button type="submit" class="btn btn-primary">
                <?= icon('save', 16) ?> <?= __('admin.mail.save') ?>
            </button>
            <button type="button" class="btn btn-secondary" id="mail-test-btn">
                <?= icon('send', 16) ?> <?= __('admin.mail.test_mail') ?>
            </button>
            <span class="mail-action-feedback" id="mail-action-feedback"></span>
        </div>
    </form>
</div>

<!-- ═══════════════════════════════════════════════════════════════════
     TAB 2: E-Mail Templates
     ═══════════════════════════════════════════════════════════════════ -->
<div class="iw-tab-panel <?= $activeTab === 'templates' ? 'active' : '' ?>" id="tab-templates" role="tabpanel">
    <p class="mail-templates-desc"><?= __('admin.mail.templates_desc') ?></p>

    <div class="mail-template-grid">
        <?php foreach ($templates as $tpl): ?>
        <div class="mail-template-card">
            <div class="mail-template-card-header">
                <span class="mail-template-card-icon">
                    <?= icon($tpl['slug'] === 'password_reset' ? 'key' : 'mail', 20) ?>
                </span>
                <div>
                    <div class="mail-template-card-title"><?= e($tpl['name']) ?></div>
                    <code class="mail-template-card-subject"><?= e($tpl['subject']) ?></code>
                </div>
            </div>
            <div class="mail-template-card-preview">
                <div class="mail-preview-mini">
                    <div class="mail-preview-mini-bar"></div>
                    <div class="mail-preview-mini-line"></div>
                    <div class="mail-preview-mini-line medium"></div>
                    <div class="mail-preview-mini-line short"></div>
                    <?php if ($tpl['slug'] === 'password_reset'): ?>
                    <div class="mail-preview-mini-btn"></div>
                    <?php endif; ?>
                    <div class="mail-preview-mini-line short"></div>
                </div>
            </div>
            <div class="mail-template-card-meta">
                <span class="mail-template-card-date">
                    <?= __('admin.mail.template_last_edited') ?>: <?= date('d.m.Y', strtotime($tpl['updated_at'])) ?>
                </span>
                <a href="/admin/settings/mail/template/<?= (int)$tpl['id'] ?>" class="btn btn-secondary btn-sm">
                    <?= icon('pen-line', 14) ?> <?= __('admin.mail.template_edit') ?>
                </a>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>

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