{% extends 'admin/layout.twig' %}
{% block title %}Settings — Admin{% endblock %}
{% block content %}
    <div class="admin-page-head">
        <h1>Settings</h1>
        <p class="muted">Customise branding and connect your inbound/outbound email.</p>
    </div>

    <form method="post" action="{{ app_url('/admin/settings') }}" enctype="multipart/form-data" class="form">
        {{ csrf_field()|raw }}

        <div class="card">
            <h2 class="section-title">Branding</h2>
            <div class="form-row two">
                <div>
                    <label>Brand name</label>
                    <input type="text" name="brand_name" value="{{ settings.brand_name }}" required>
                </div>
                <div>
                    <label>Tagline</label>
                    <input type="text" name="brand_tagline" value="{{ settings.brand_tagline }}">
                </div>
            </div>
            <div class="form-row two">
                <div>
                    <label>Primary color</label>
                    <input type="color" name="brand_primary_color" value="{{ settings.brand_primary_color }}">
                </div>
                <div>
                    <label>Support email (Reply-To)</label>
                    <input type="email" name="brand_support_email" value="{{ settings.brand_support_email }}">
                </div>
            </div>
            <div class="form-row two">
                <div>
                    <label>Logo URL or upload</label>
                    <input type="text" name="brand_logo" value="{{ settings.brand_logo }}" placeholder="/uploads/logo.png">
                    <input type="file" name="logo_file" accept="image/*">
                </div>
                <div>
                    <label>Footer HTML</label>
                    <input type="text" name="brand_footer" value="{{ settings.brand_footer }}">
                </div>
            </div>
            <div class="form-row two">
                <div>
                    <label>Reply subject prefix</label>
                    <input type="text" name="reply_subject_prefix" value="{{ settings.reply_subject_prefix }}" placeholder="[Ticket #{number}]">
                    <small class="muted">Use <code>{number}</code> as placeholder.</small>
                </div>
            </div>
        </div>

        <div class="card">
            <h2 class="section-title">Behavior</h2>
            <label class="checkbox"><input type="checkbox" name="kb_enabled" value="1" {{ settings.kb_enabled == '1' ? 'checked' : '' }}> Enable Knowledge Base</label>
            <label class="checkbox"><input type="checkbox" name="allow_guest_tickets" value="1" {{ settings.allow_guest_tickets == '1' ? 'checked' : '' }}> Allow guest ticket submission</label>
            <label class="checkbox"><input type="checkbox" name="allow_registration" value="1" {{ settings.allow_registration == '1' ? 'checked' : '' }}> Allow customer self-registration</label>
        </div>

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

    <div class="card">
        <h2 class="section-title">Departments</h2>
        <p class="muted">Each department can have its own incoming email address. Inbound mail addressed to that address is routed to the department.</p>
        <table class="data-table">
            <thead><tr><th>Name</th><th>Email</th><th></th></tr></thead>
            <tbody>
                {% for d in departments %}
                    <tr>
                        <td>{{ d.name }}</td>
                        <td>{{ d.email ?: '—' }}</td>
                        <td>
                            <form method="post" action="{{ app_url('/admin/settings/departments/' ~ d.id ~ '/delete') }}" class="inline-form" onsubmit="return confirm('Delete department?');">
                                {{ csrf_field()|raw }}
                                <button type="submit" class="btn btn-danger small">Delete</button>
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
        <form method="post" action="{{ app_url('/admin/settings/departments') }}" class="form inline-add">
            {{ csrf_field()|raw }}
            <input type="text" name="name" placeholder="Name (e.g. Billing)" required>
            <input type="email" name="email" placeholder="Email (e.g. billing@yours.com)">
            <button type="submit" class="btn btn-primary">Add</button>
        </form>
    </div>

    <div class="card">
        <h2 class="section-title">Email tools</h2>
        <p class="muted">SMTP and IMAP credentials live in <code>.env</code> for security. Use these tools to verify the connection.</p>
        <form method="post" action="{{ app_url('/admin/settings/test-email') }}" class="form inline-add">
            {{ csrf_field()|raw }}
            <input type="email" name="to" placeholder="Test recipient" required>
            <button type="submit" class="btn btn-light">Send test SMTP</button>
        </form>
        <form method="post" action="{{ app_url('/admin/settings/fetch-mail') }}" class="form" style="margin-top: 10px;">
            {{ csrf_field()|raw }}
            <button type="submit" class="btn btn-light">Fetch incoming mail now</button>
            <span class="muted small"> — or schedule <code>php bin/fetch-mail.php</code> as a cron every minute.</span>
        </form>
    </div>
{% endblock %}
