{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_list %}

{% block title %}Email Users{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
    &rsaquo; Email Users
</div>
{% endblock %}

{% block content %}
<div class="module filtered">
    <h2>Filter Users for Email</h2>

    <form method="post" action="{% url 'admin:email_users' %}">
        {% csrf_token %}

        <div class="form-row">
            <div class="field-box">
                <label for="user_origin">User Origin:</label>
                <select name="user_origin" id="user_origin" class="vTextField">
                    <option value="">All Origins</option>
                    {% for choice in user_origin_choices %}
                        <option value="{{ choice.0 }}" {% if choice.0 == selected_origin %}selected{% endif %}>
                            {{ choice.1 }}
                        </option>
                    {% endfor %}
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="field-box">
                <label for="has_purchase">
                    <input type="checkbox" name="has_purchase" id="has_purchase" value="1"
                           {% if has_purchase %}checked{% endif %}>
                    User has made a purchase
                </label>
            </div>
        </div>

        <div class="form-row">
            <input type="submit" value="Filter Users" class="default" name="_filter">
        </div>
    </form>

    {% if filtered_users %}
    <div class="results">
        <h3>Filtered Users ({{ filtered_users|length }} found)</h3>
        <div class="module">
            <table cellspacing="0">
                <thead>
                    <tr>
                        <th>Email</th>
                        <th>Origin</th>
                        <th>Has Purchase</th>
                        <th>Date Joined</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in filtered_users %}
                    <tr class="{% cycle 'row1' 'row2' %}">
                        <td>{{ user.email }}</td>
                        <td>{{ user.get_origin_display }}</td>
                        <td>{{ user.has_purchase|yesno:"Yes,No" }}</td>
                        <td>{{ user.date_joined|date:"M d, Y" }}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="4">No users found matching criteria.</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
