<?php
$pageTitle = 'Email Settings';
$breadcrumb = [['label' => 'Settings', 'url' => APP_URL . '/settings'], ['label' => 'Email', 'url' => null]];
ob_start();
?>
<div class="page-header">
    <div>
        <h1 class="page-title">Email / SMTP Settings</h1>
        <p class="page-subtitle">Configure the SMTP server used to send platform emails</p>
    </div>
</div>

<div class="tabs">
    <a href="<?= APP_URL ?>/settings" class="tab-item">General</a>
    <a href="<?= APP_URL ?>/settings/security" class="tab-item">Security</a>
    <a href="<?= APP_URL ?>/settings/billing" class="tab-item">Billing</a>
    <a href="<?= APP_URL ?>/settings/email" class="tab-item active">Email/SMTP</a>
    <a href="<?= APP_URL ?>/admin/system" class="tab-item">System</a>
    <a href="<?= APP_URL ?>/settings/audit" class="tab-item">Audit Log</a>
</div>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">SMTP Configuration</h3>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= APP_URL ?>/settings/email">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">

            <div class="form-group">
                <label class="form-label">SMTP Host</label>
                <input type="text" name="smtp_host" class="form-control" value="<?= htmlspecialchars($settings['smtp_host'] ?? SMTP_HOST) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Port</label>
                <input type="number" name="smtp_port" class="form-control" value="<?= htmlspecialchars($settings['smtp_port'] ?? SMTP_PORT) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Username</label>
                <input type="text" name="smtp_username" class="form-control" value="<?= htmlspecialchars($settings['smtp_username'] ?? '') ?>">
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Password</label>
                <input type="password" name="smtp_password" class="form-control" placeholder="Leave blank to keep current password">
            </div>
            <div class="form-group">
                <label class="form-label">Encryption</label>
                <select name="smtp_encryption" class="form-control form-select">
                    <option value="tls" <?= ($settings['smtp_encryption'] ?? 'tls') === 'tls' ? 'selected' : '' ?>>TLS</option>
                    <option value="ssl" <?= ($settings['smtp_encryption'] ?? '') === 'ssl' ? 'selected' : '' ?>>SSL</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">From Email</label>
                <input type="email" name="smtp_from_email" class="form-control" value="<?= htmlspecialchars($settings['smtp_from_email'] ?? SMTP_FROM_EMAIL) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">From Name</label>
                <input type="text" name="smtp_from_name" class="form-control" value="<?= htmlspecialchars($settings['smtp_from_name'] ?? SMTP_FROM_NAME) ?>">
            </div>

            <button type="submit" class="btn btn-primary">Save Settings</button>
        </form>
    </div>
</div>

<div class="card" style="margin-top:24px;">
    <div class="card-header">
        <h3 class="card-title">Send Test Email</h3>
    </div>
    <div class="card-body">
        <form id="testEmailForm" style="display:flex;gap:12px;align-items:flex-end;">
            <div class="form-group" style="flex:1;margin-bottom:0;">
                <label class="form-label">Recipient Email</label>
                <input type="email" id="test_email" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-secondary">Send Test</button>
        </form>
        <p id="testEmailResult" style="margin-top:12px;font-size:0.85rem;"></p>
    </div>
</div>

<script>
document.getElementById('testEmailForm').addEventListener('submit', function (e) {
    e.preventDefault();
    const email = document.getElementById('test_email').value;
    const result = document.getElementById('testEmailResult');
    result.textContent = 'Sending...';
    fetch('<?= APP_URL ?>/settings/email/test', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'test_email=' + encodeURIComponent(email) + '&csrf_token=' + encodeURIComponent('<?= $_SESSION[CSRF_TOKEN_NAME] ?? '' ?>')
    }).then(r => r.json()).then(data => {
        result.textContent = data.message;
        result.style.color = data.success ? 'var(--success)' : 'var(--danger)';
    });
});
</script>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
