{% extends "admin/base.html" %}
{% block title %}{{ 'Edit User' if user else 'New User' }}{% endblock %}
{% block content %}
<div class="d-flex align-items-center gap-2 mb-4">
    <a href="{{ url_for('site_admin.users') }}" class="btn btn-sm btn-outline-secondary">
        <i class="bi bi-arrow-left"></i>
    </a>
    <h2 class="mb-0">{{ 'Edit User: ' ~ user.username if user else 'New User' }}</h2>
</div>

<div class="row">
    <div class="col-md-6">
        <div class="card border-secondary">
            <div class="card-body">
                <form method="post">
                    <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                    <div class="mb-3">
                        <label class="form-label">Username *</label>
                        <input type="text" name="username" class="form-control" required
                               value="{{ user.username if user else request.form.get('username', '') }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Email *</label>
                        <input type="email" name="email" class="form-control" required
                               value="{{ user.email if user else request.form.get('email', '') }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ 'New Password (leave blank = unchanged)' if user else 'Password *' }}</label>
                        <input type="password" name="{{ 'new_password' if user else 'password' }}" class="form-control"
                               {{ '' if user else 'required' }}>
                        {% if not user %}
                        <div class="form-text">Minimum 8 characters recommended.</div>
                        {% endif %}
                    </div>
                    <div class="mb-4">
                        <div class="form-check">
                            <input type="checkbox" name="is_site_admin" id="is_site_admin" class="form-check-input"
                                   value="1" {{ 'checked' if user and user.is_site_admin }}>
                            <label class="form-check-label" for="is_site_admin">
                                <span class="text-danger fw-semibold"><i class="bi bi-shield-fill me-1"></i>Site Admin</span>
                                <small class="text-muted d-block">Full access to all groups and settings</small>
                            </label>
                        </div>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-success">
                            <i class="bi bi-check-lg me-1"></i>{{ 'Save' if user else 'Create' }}
                        </button>
                        <a href="{{ url_for('site_admin.users') }}" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
