{% extends 'affiliates_app/base_affiliate.html' %}
{% load static %}

{% block title %}{{ meta.label }}{% endblock %}

{% block content %}
<style>
    .referral-list-section {
        background:
            radial-gradient(circle at top left, rgba(25, 135, 84, 0.12), transparent 28%),
            radial-gradient(circle at bottom right, rgba(13, 110, 253, 0.08), transparent 30%),
            linear-gradient(135deg, #f8fafc 0%, #eefaf4 100%);
        min-height: 100vh;
    }

    .referral-list-card {
        border-radius: 28px;
        overflow: hidden;
        background: #ffffff;
        box-shadow: 0 20px 55px rgba(15, 23, 42, 0.10);
    }

    .referral-list-header {
        background: linear-gradient(135deg, #064e3b 0%, #047857 58%, #0f766e 100%);
        padding: 2rem 2.2rem;
        color: #ffffff;
        position: relative;
        overflow: hidden;
    }

    .referral-list-header::after {
        content: "";
        position: absolute;
        width: 210px;
        height: 210px;
        border-radius: 50%;
        right: -80px;
        top: -80px;
        background: rgba(255, 255, 255, 0.12);
    }

    .referral-list-header-content {
        position: relative;
        z-index: 2;
    }

    .referral-list-kicker {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        background: rgba(255, 255, 255, 0.15);
        border: 1px solid rgba(255, 255, 255, 0.18);
        color: rgba(255, 255, 255, 0.92);
        padding: 7px 13px;
        border-radius: 50rem;
        font-size: 0.82rem;
        font-weight: 700;
        margin-bottom: 0.9rem;
    }

    .referral-list-header h2 {
        font-weight: 800;
        letter-spacing: -0.03em;
        margin-bottom: 0.45rem;
    }

    .referral-list-header p {
        margin-bottom: 0;
        color: rgba(255, 255, 255, 0.82);
    }

    .referral-list-body {
        padding: 2.2rem;
    }

    /* Referral grid card */
    .referral-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        gap: 1.25rem;
    }

    .referral-person-card {
        background: #ffffff;
        border: 1px solid #e7f2ec;
        border-radius: 22px;
        padding: 1.4rem;
        display: flex;
        flex-direction: column;
        gap: 0.9rem;
        transition: box-shadow 0.2s ease, transform 0.2s ease;
    }

    .referral-person-card:hover {
        box-shadow: 0 12px 32px rgba(15, 23, 42, 0.10);
        transform: translateY(-2px);
    }

    .referral-person-top {
        display: flex;
        align-items: center;
        gap: 0.9rem;
    }

    .referral-avatar {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid #e7f2ec;
        flex-shrink: 0;
    }

    .referral-avatar-placeholder {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: linear-gradient(135deg, #dcfce7, #bbf7d0);
        border: 2px solid #e7f2ec;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        font-weight: 800;
        font-size: 1.1rem;
        color: #047857;
    }

    .referral-person-name {
        font-weight: 800;
        color: #0f172a;
        font-size: 1rem;
        margin-bottom: 0.1rem;
        line-height: 1.3;
    }

    .referral-person-country {
        font-size: 0.82rem;
        color: #64748b;
    }

    .referral-contact-row {
        display: flex;
        flex-direction: column;
        gap: 0.4rem;
    }

    .referral-contact-item {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.875rem;
        color: #334155;
    }

    .referral-contact-item i {
        color: #047857;
        font-size: 0.9rem;
        width: 16px;
        flex-shrink: 0;
    }

    .referral-contact-item a {
        color: #047857;
        text-decoration: none;
        font-weight: 600;
    }

    .referral-contact-item a:hover {
        text-decoration: underline;
    }

    .referral-divider {
        border: none;
        border-top: 1px solid #e7f2ec;
        margin: 0;
    }

    .referral-meta-row {
        display: flex;
        flex-direction: column;
        gap: 0.35rem;
    }

    .referral-meta-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 0.82rem;
    }

    .referral-meta-label {
        color: #64748b;
    }

    .referral-meta-value {
        font-weight: 700;
        color: #0f172a;
    }

    .referral-status-badge {
        display: inline-flex;
        align-items: center;
        padding: 4px 12px;
        border-radius: 50rem;
        font-size: 0.75rem;
        font-weight: 700;
    }

    .referral-joined {
        font-size: 0.78rem;
        color: #94a3b8;
        text-align: right;
    }

    /* Empty state */
    .referral-empty {
        text-align: center;
        padding: 4rem 2rem;
        color: #64748b;
    }

    .referral-empty i {
        font-size: 3rem;
        color: #cbd5e1;
        margin-bottom: 1rem;
    }

    .referral-empty h5 {
        font-weight: 800;
        color: #334155;
        margin-bottom: 0.5rem;
    }

    /* Tab filter strip */
    .referral-filter-strip {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
        margin-bottom: 1.5rem;
    }

    .referral-filter-btn {
        padding: 6px 16px;
        border-radius: 50rem;
        font-size: 0.82rem;
        font-weight: 700;
        border: 1px solid #dbe7e1;
        background: #ffffff;
        color: #475569;
        text-decoration: none;
        transition: all 0.15s ease;
    }

    .referral-filter-btn:hover {
        background: #f0fdf4;
        border-color: #047857;
        color: #047857;
    }

    .referral-filter-btn.active {
        background: #047857;
        border-color: #047857;
        color: #ffffff;
    }

    @media (max-width: 767.98px) {
        .referral-list-header { padding: 1.5rem; }
        .referral-list-body { padding: 1.1rem; }
        .referral-grid { grid-template-columns: 1fr; }
    }
</style>

<section class="referral-list-section py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-12 col-lg-11">

                <div class="referral-list-card">

                    <div class="referral-list-header">
                        <div class="referral-list-header-content">
                            <span class="referral-list-kicker">
                                <i class="bi bi-people-fill"></i>
                                My Referrals
                            </span>
                            <h2>{{ meta.label }}</h2>
                            <p>{{ meta.description }}</p>
                        </div>
                    </div>

                    <div class="referral-list-body">

                        <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">

                            <div class="referral-filter-strip">
                                <a href="?status=signed_up"
                                   class="referral-filter-btn {% if status_filter == 'signed_up' %}active{% endif %}">
                                    <i class="bi bi-hourglass-split me-1"></i> Pending
                                </a>
                                <a href="?status=confirmed"
                                   class="referral-filter-btn {% if status_filter == 'confirmed' %}active{% endif %}">
                                    <i class="bi bi-patch-check-fill me-1"></i> Confirmed
                                </a>
                                <a href="?status=paid_completed"
                                   class="referral-filter-btn {% if status_filter == 'paid_completed' %}active{% endif %}">
                                    <i class="bi bi-credit-card-fill me-1"></i> Completed
                                </a>
                            </div>

                            <a href="{% url 'affiliates_app:affiliate_dashboard' %}" class="btn btn-outline-secondary btn-sm rounded-pill">
                                <i class="bi bi-arrow-left me-1"></i> Back to Dashboard
                            </a>

                        </div>

                        {% if referrals %}
                            <div class="referral-grid">
                                {% for referral in referrals %}
                                    {% with profile=referral.foreigner_profile guest=referral.guest_user %}
                                    <div class="referral-person-card">

                                        <div class="referral-person-top">
                                            {% if profile and profile.profile_image %}
                                                <img src="{{ profile.profile_image.url }}"
                                                     alt="{{ referral.guest_name }}"
                                                     class="referral-avatar">
                                            {% else %}
                                                <div class="referral-avatar-placeholder">
                                                    {{ referral.guest_name|slice:":1"|upper }}
                                                </div>
                                            {% endif %}

                                            <div>
                                                <div class="referral-person-name">
                                                    {{ referral.guest_name|default:"Unknown" }}
                                                </div>
                                                {% if referral.guest_country %}
                                                    <div class="referral-person-country">
                                                        <i class="bi bi-geo-alt-fill me-1"></i>{{ referral.guest_country }}
                                                    </div>
                                                {% endif %}
                                            </div>
                                        </div>

                                        <hr class="referral-divider">

                                        <div class="referral-contact-row">
                                            {% if referral.guest_email %}
                                                <div class="referral-contact-item">
                                                    <i class="bi bi-envelope-fill"></i>
                                                    <a href="mailto:{{ referral.guest_email }}">{{ referral.guest_email }}</a>
                                                </div>
                                            {% endif %}
                                            {% if referral.guest_phone %}
                                                <div class="referral-contact-item">
                                                    <i class="bi bi-whatsapp"></i>
                                                    <a href="https://wa.me/{{ referral.guest_phone|cut:'+' }}" target="_blank">
                                                        {{ referral.guest_phone }}
                                                    </a>
                                                </div>
                                            {% endif %}
                                        </div>

                                        <hr class="referral-divider">

                                        <div class="referral-meta-row">
                                            {% if referral.selected_package_name %}
                                                <div class="referral-meta-item">
                                                    <span class="referral-meta-label"><i class="bi bi-box-seam me-1"></i>Package</span>
                                                    <span class="referral-meta-value">{{ referral.selected_package_name }}</span>
                                                </div>
                                            {% endif %}

                                            <div class="referral-meta-item">
                                                <span class="referral-meta-label"><i class="bi bi-piggy-bank me-1"></i>Possible Commission</span>
                                                <span class="referral-meta-value text-success">${{ referral.total_possible_commission_usd }}</span>
                                            </div>

                                            {% if referral.total_earned_commission_usd > 0 %}
                                                <div class="referral-meta-item">
                                                    <span class="referral-meta-label"><i class="bi bi-check-circle me-1"></i>Earned</span>
                                                    <span class="referral-meta-value text-success">${{ referral.total_earned_commission_usd }}</span>
                                                </div>
                                            {% endif %}

                                            <div class="referral-meta-item">
                                                <span class="referral-meta-label">Status</span>
                                                <span class="referral-status-badge {{ meta.badge_class }}">
                                                    {{ referral.get_status_display }}
                                                </span>
                                            </div>
                                        </div>

                                        <div class="referral-joined">
                                            <i class="bi bi-calendar3 me-1"></i>
                                            Referred {{ referral.created_at|date:"M d, Y" }}
                                        </div>

                                    </div>
                                    {% endwith %}
                                {% endfor %}
                            </div>

                        {% else %}
                            <div class="referral-empty">
                                <i class="bi bi-people"></i>
                                <h5>No referrals found</h5>
                                <p class="mb-0">You don't have any referrals in this category yet.</p>
                            </div>
                        {% endif %}

                    </div>
                </div>

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