{% extends "admin/base.html" %}
{% block title %}Mail Settings{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-envelope-fill me-2"></i>Mail Settings</h2>
</div>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card border-secondary">
            <div class="card-header">SMTP Configuration</div>
            <div class="card-body">
                {% if error %}
                <div class="alert alert-danger">{{ error }}</div>
                {% endif %}

                <form method="post">
                    <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                    <div class="row g-3">
                        <div class="col-md-8">
                            <label class="form-label">SMTP Host</label>
                            <input type="text" name="host" class="form-control" required value="{{ settings.host if settings else '' }}">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Port</label>
                            <input type="number" name="port" class="form-control" required value="{{ settings.port if settings else '587' }}">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">SMTP Username</label>
                            <input type="text" name="username" class="form-control" required value="{{ settings.username if settings else '' }}">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">SMTP Password</label>
                            <input type="password" name="password" class="form-control" placeholder="{{ '(unchanged)' if settings else '' }}">
                            {% if settings %}
                            <div class="form-text">Leave blank to keep the stored password.</div>
                            {% endif %}
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">From Email</label>
                            <input type="email" name="from_email" class="form-control" required value="{{ settings.from_email if settings else '' }}">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">From Name</label>
                            <input type="text" name="from_name" class="form-control" value="{{ settings.from_name if settings else '' }}" placeholder="MCLogger">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Test Recipient</label>
                            <input type="email" name="test_recipient" class="form-control" placeholder="Optional, defaults to From Email">
                        </div>
                        <div class="col-md-6 d-flex align-items-end">
                            <div class="form-check form-switch mb-2">
                                <input class="form-check-input" type="checkbox" role="switch" name="use_tls" id="use_tls" value="1" {{ 'checked' if not settings or settings.use_tls }}>
                                <label class="form-check-label" for="use_tls">Use STARTTLS</label>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-2 mt-4">
                        <button type="submit" name="action" value="save" class="btn btn-success">
                            <i class="bi bi-check2-circle me-1"></i>Save and Verify
                        </button>
                        <button type="submit" name="action" value="test" class="btn btn-outline-info">
                            <i class="bi bi-send-check me-1"></i>Send Test Email
                        </button>
                    </div>
                </form>

                {% if settings %}
                <form method="post" action="{{ url_for('site_admin.mail_settings_delete') }}" class="mt-3">
                    <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-outline-danger" onclick="return confirm('Delete stored mail settings?')">
                        <i class="bi bi-trash3 me-1"></i>Delete Stored Settings
                    </button>
                </form>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="col-lg-5">
        <div class="card border-secondary">
            <div class="card-header">How to use your no-reply mailbox</div>
            <div class="card-body">
                <p class="text-muted small mb-2">Use your SMTP server details here. IMAP4 and POP3 are not needed for sending mail.</p>
                <ul class="small text-muted mb-0">
                    <li>SMTP Host: your provider's outgoing mail server</li>
                    <li>Port: usually 587 for STARTTLS</li>
                    <li>SMTP Username: often the full mailbox address</li>
                    <li>From Email: your no-reply address, for example noreply@yourdomain.com</li>
                    <li>Use STARTTLS: keep enabled for your setup</li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}
