{% extends 'main.html' %}
{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-lg-9 mx-auto">
            <div class="card shadow-sm">
                <div class="card-header">
                    <h4 class="my-0">Species Name Consistency &mdash; Enforce Single Quote Usage</h4>
                </div>
                <div class="card-body px-4 py-3">
                    <p class="text-muted">
                        Scans all species <code>name</code> and <code>alt_name</code> fields and
                        replaces every curly, smart, or double-quote variant with a standard
                        straight single quote&nbsp;(<code>'</code>).
                        Only records that actually change are saved. Click <strong>Run</strong> to apply.
                    </p>
                    <form method="POST">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-warning mt-1">Run</button>
                        <a href="{% url 'tools2' %}" class="btn btn-secondary mt-1 ms-2">Cancel</a>
                    </form>
                </div>
            </div>

            {% if request.method == 'POST' %}
            <div class="card shadow-sm mt-4">
                <div class="card-header">
                    <h5 class="my-0">
                        Results &mdash;
                        {% if changes %}
                            {{ changes|length }} record{{ changes|length|pluralize }} updated
                        {% else %}
                            No changes needed
                        {% endif %}
                    </h5>
                </div>
                <div class="card-body px-4 py-3">
                    {% if changes %}
                    <table class="table table-striped table-sm">
                        <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>Original Name</th>
                                <th>Updated Name</th>
                                <th>Fields Changed</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for c in changes %}
                            <tr>
                                <td>{{ c.id }}</td>
                                <td><code>{{ c.original_name }}</code></td>
                                <td><code>{{ c.new_name }}</code></td>
                                <td>{{ c.fields }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                    {% else %}
                    <p class="text-success mb-0">&#10003; All species names are already using standard single quotes — nothing to update.</p>
                    {% endif %}
                </div>
            </div>
            {% endif %}

        </div>
    </div>
</div>
{% endblock content %}