{% extends 'admin/base.html' %}
{% block title %}Subscribers{% endblock %}
{% block page_title %}Subscribers Management{% endblock %}
{% block content %}

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
    <form class="search-bar" method="GET" action="/admin/subscribers" style="margin: 0; flex: 1;">
        <input type="text" name="search" placeholder="🔍 Search name, email or phone..." value="{{ search }}"/>
        
        {# NEW: Site Filter (Only shows if admin is Super Admin) #}
        {% if is_super %}
        <select name="site_id">
            <option value="">All Sites</option>
            {% for site in all_sites %}
            <option value="{{ site.id }}" {% if selected_site == site.id %}selected{% endif %}>
                {{ site.name }}
            </option>
            {% endfor %}
        </select>
        {% endif %}

        <select name="status">
            <option value="">All Statuses</option>
            <option value="active" {% if status=='active' %}selected{% endif %}>Active</option>
            <option value="expired" {% if status=='expired' %}selected{% endif %}>Expired</option>
            <option value="pending" {% if status=='pending' %}selected{% endif %}>Pending</option>
        </select>
        <button type="submit" class="btn btn-purple">Filter</button>
    </form>
    
    {% if session.admin_role == 'super_admin' %}
    <a href="{{ url_for('admin_pos_register') }}" class="btn btn-green" style="margin-left: 15px;">
        ➕ New POS Customer
    </a>
    {% endif %}
</div>

<div class="table-wrap">
    <table>
        <thead>
            <tr>
                <th>S/N</th> 
                <th>Name</th>
                <th>Site</th> {# NEW COLUMN #}
                <th>Email</th>
                <th>Status</th>
                <th>Expiry</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {# Note: Changed 'subscribers.items' to 'subscribers' based on your route logic #}
            {% for s in subscribers %}
            <tr>
                <td class="mono">{{ loop.index }}</td>
                <td><strong style="color:white;">{{ s.first_name }} {{ s.surname }}</strong></td>
                
                {# NEW: Site Label #}
                <td>
                    <span style="font-size: 10px; color: #888; border: 1px solid #444; padding: 2px 5px; border-radius: 4px; text-transform: uppercase;">
                        {{ s.site.slug }}
                    </span>
                </td>

                <td class="mono" style="font-size: 11px;">{{ s.email }}</td>
                <td><span class="badge {{ s.payment_status }}">{{ s.payment_status }}</span></td>
                <td class="mono" style="font-size: 11px;">{{ s.expiry_date.strftime('%d %b %Y') if s.expiry_date else '—' }}</td>
                <td>
                    <div style="display:flex; gap:6px; flex-wrap: wrap;">
                        
                        {% if s.payment_status == 'active' %}
                        <form method="POST" action="{{ url_for('admin_toggle_user', sub_id=s.id) }}">
                            <input type="hidden" name="action" value="disable">
                            <button type="submit" class="btn btn-amber" style="font-size: 10px; padding: 5px 8px;">🚫 Disable</button>
                        </form>
                        {% else %}
                        <form method="POST" action="{{ url_for('admin_toggle_user', sub_id=s.id) }}">
                            <input type="hidden" name="action" value="enable">
                            <button type="submit" class="btn btn-green" style="font-size: 10px; padding: 5px 8px;">✅ Enable</button>
                        </form>
                        {% endif %}

                        <form method="POST" action="{{ url_for('admin_pos_renew', sub_id=s.id) }}">
                            <button type="submit" class="btn btn-blue" style="font-size: 10px; padding: 5px 8px;" 
                                    onclick="return confirm('Initiate POS for {{ s.first_name }}?')">💳 POS</button>
                        </form>

                        {% if not s.mikrotik_added %}
                        <form method="POST" action="{{ url_for('admin_force_push', sub_id=s.id) }}">
                            <button type="submit" class="btn btn-purple" style="font-size: 10px; padding: 5px 8px;"
                                    title="Repair MikroTik Account">🚀 Push</button>
                        </form>
                        {% endif %}

                        <form id="del_{{ s.id }}" method="POST" action="/admin/subscriber/{{ s.id }}/delete"></form>
                        <button class="btn btn-red" style="padding: 5px 10px;"
                                onclick="confirmAction('del_{{ s.id }}','Delete {{ s.email }}?')">🗑</button>
                    </div>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="7" style="text-align:center; padding:32px;">No subscribers found</td></tr>
            {% endfor %}
        </tbody>
    </table>
</div>

{% endblock %}