{% extends 'base.html' %}

{% block title %}Dashboard - Referral Rewards{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="row gy-4">
        <!-- Profile Header -->
        <div class="col-12">
            <div class="card border-0 shadow-sm p-4 rounded-4 bg-white">
                <div class="d-flex align-items-center">
                    <div class="rounded-circle bg-primary bg-opacity-10 text-primary d-flex align-items-center justify-content-center me-4" style="width: 64px; height: 64px;">
                        <span class="h3 mb-0">{{ user.username|first|upper }}</span>
                    </div>
                    <div>
                        <h2 class="mb-1">Hello, {{ user.username }}!</h2>
                        <p class="text-muted mb-0">Track your progress and share your rewards.</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Stats Overview -->
        <div class="col-lg-4">
            <div class="card h-100 border-0 shadow-sm p-4 rounded-4 bg-white text-center">
                <p class="text-muted small text-uppercase fw-bold mb-2">Total Points</p>
                <h3 class="display-5 fw-bold text-primary mb-0">{{ profile.points }}</h3>
                <p class="text-muted mt-2 mb-0">Total successful referrals: {{ referrals.count }}</p>
                <div class="progress mt-3" style="height: 8px;">
                    <div class="progress-bar bg-primary" style="width: {{ profile.points }}%;"></div>
                </div>
            </div>
        </div>

        <!-- Referral Widget -->
        <div class="col-lg-8">
            <div class="card h-100 border-0 shadow-sm p-4 rounded-4 bg-white">
                <h4 class="mb-4">Your Referral Link</h4>
                <p class="text-muted mb-4">Share this link with your friends and earn points when they sign up and activate their account!</p>
                
                <div class="input-group mb-3">
                    <input type="text" id="referralLink" class="form-control form-control-lg bg-light border-0" value="{{ referral_link }}" readonly>
                    <button class="btn btn-primary px-4" type="button" onclick="copyLink()">Copy Link</button>
                </div>
                
                <div class="d-flex gap-3 mt-4">
                    <a href="https://twitter.com/intent/tweet?text=Join%20me%20on%20Referral%20Rewards%21%20{{ referral_link }}" target="_blank" class="btn btn-outline-primary flex-fill py-2">Share on Twitter</a>
                    <a href="https://wa.me/?text=Join%20me%20on%20Referral%20Rewards%21%20{{ referral_link }}" target="_blank" class="btn btn-outline-primary flex-fill py-2">Share on WhatsApp</a>
                </div>
            </div>
        </div>

        <!-- Recent Referrals -->
        <div class="col-12 mt-4">
            <h4 class="mb-4">Successful Referrals</h4>
            <div class="card border-0 shadow-sm rounded-4 overflow-hidden bg-white">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="bg-light">
                            <tr>
                                <th class="px-4 py-3 border-0">Username</th>
                                <th class="px-4 py-3 border-0 text-center">Date Joined</th>
                                <th class="px-4 py-3 border-0 text-center">Status</th>
                                <th class="px-4 py-3 border-0 text-end">Reward</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for referral in referrals %}
                            <tr>
                                <td class="px-4 py-3 border-0">{{ referral.referred_user.username }}</td>
                                <td class="px-4 py-3 border-0 text-center text-muted">{{ referral.completed_at|date:"M d, Y" }}</td>
                                <td class="px-4 py-3 border-0 text-center">
                                    <span class="badge bg-success rounded-pill px-3">{{ referral.get_status_display }}</span>
                                </td>
                                <td class="px-4 py-3 border-0 text-end fw-bold text-primary">+10 Points</td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="4" class="px-4 py-5 text-center text-muted">
                                    No referrals yet. Share your link to start earning rewards!
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- Marketing Collateral Preview -->
        <div class="col-12 mt-4">
            <div class="d-flex justify-content-between align-items-end mb-4">
                <div>
                    <h4 class="mb-1">Marketing Collateral</h4>
                    <p class="text-muted mb-0">Assets to help you promote your referral code.</p>
                </div>
                <button class="btn btn-sm btn-outline-primary">View All</button>
            </div>
            <div class="row g-4">
                <div class="col-md-4">
                    <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
                        <div class="bg-light d-flex align-items-center justify-content-center" style="height: 160px;">
                            <span class="text-muted opacity-50">Social Media Banner 1</span>
                        </div>
                        <div class="card-body p-3 text-center">
                            <button class="btn btn-link text-decoration-none">Download Asset</button>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
                        <div class="bg-light d-flex align-items-center justify-content-center" style="height: 160px;">
                            <span class="text-muted opacity-50">Email Signature</span>
                        </div>
                        <div class="card-body p-3 text-center">
                            <button class="btn btn-link text-decoration-none">Download Asset</button>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
                        <div class="bg-light d-flex align-items-center justify-content-center" style="height: 160px;">
                            <span class="text-muted opacity-50">Instagram Story Template</span>
                        </div>
                        <div class="card-body p-3 text-center">
                            <button class="btn btn-link text-decoration-none">Download Asset</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function copyLink() {
        var copyText = document.getElementById("referralLink");
        copyText.select();
        copyText.setSelectionRange(0, 99999);
        navigator.clipboard.writeText(copyText.value);
        
        const btn = event.target;
        const originalText = btn.innerText;
        btn.innerText = "Copied!";
        btn.classList.remove('btn-primary');
        btn.classList.add('btn-success');
        
        setTimeout(() => {
            btn.innerText = originalText;
            btn.classList.remove('btn-success');
            btn.classList.add('btn-primary');
        }, 2000);
    }
</script>
{% endblock %}