<?php
session_start();
require 'config/db.php';
require_once 'includes/functions.php'; // Currency
include 'includes/header.php';

if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); }

$user_id = $_SESSION['user_id'];

// Fetch User Data
$stmt = $pdo->prepare("SELECT referral_code, wallet_balance FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

// Fetch History
$hist = $pdo->prepare("
    SELECT r.*, u.name as invited_name 
    FROM referrals r 
    JOIN users u ON r.referred_user_id = u.id 
    WHERE r.referrer_id = ? 
    ORDER BY r.created_at DESC
");
$hist->execute([$user_id]);
$referrals = $hist->fetchAll();

$ref_link = "https://prosubscriptionoffers.com/register.php?ref=" . $user['referral_code'];
?>

<div class="container" style="padding-top: 40px; padding-bottom: 60px;">
    
    <div style="text-align: center; margin-bottom: 40px;">
        <h1 style="color: #2c3e50;">Invite & Earn</h1>
        <p style="color: #666;">Share your link. When your friends buy, you earn <strong>₹50</strong> in wallet credit!</p>
    </div>

    <div style="display: flex; gap: 30px; flex-wrap: wrap;">
        
        <div style="flex: 1; min-width: 300px; background: linear-gradient(135deg, #667eea, #764ba2); color: white; padding: 30px; border-radius: 15px; text-align: center;">
            <h3 style="margin-top:0;">My Wallet Balance</h3>
            <div style="font-size: 40px; font-weight: bold; margin: 15px 0;">
                ₹<?php echo number_format($user['wallet_balance'], 2); ?>
            </div>
            <p style="opacity: 0.8; font-size: 14px;">Use this balance automatically at checkout.</p>
        </div>

        <div style="flex: 2; min-width: 300px; background: white; padding: 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.05); border: 1px solid #eee;">
            <h3 style="margin-top:0; color: #2c3e50;">Your Unique Link</h3>
            
            <div style="display: flex; gap: 10px; margin-top: 20px;">
                <input type="text" value="<?php echo $ref_link; ?>" id="refLink" readonly 
                       style="flex: 1; padding: 12px; border: 1px solid #ddd; border-radius: 5px; background: #f9f9f9;">
                <button onclick="copyRef()" class="btn btn-primary" style="width: auto; padding: 0 25px;">Copy</button>
            </div>

            <div style="margin-top: 20px;">
                <a href="https://wa.me/?text=Check+out+Pro+Offers+for+cheap+software+keys%21+Use+my+link%3A+<?php echo urlencode($ref_link); ?>" 
                   target="_blank" class="btn" style="background: #25D366; color: white; display: inline-block; width: auto; padding: 10px 20px;">
                   <i class="fab fa-whatsapp"></i> Share on WhatsApp
                </a>
            </div>
        </div>

    </div>

    <h3 style="margin-top: 50px;">Referral History</h3>
    <table style="width: 100%; background: white; border-collapse: collapse; box-shadow: 0 2px 10px rgba(0,0,0,0.05); border-radius: 8px; overflow: hidden;">
        <thead>
            <tr style="background: #f8f9fa; text-align: left;">
                <th style="padding: 15px;">Invited Friend</th>
                <th>Status</th>
                <th>Reward</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php if(count($referrals) > 0): ?>
                <?php foreach($referrals as $r): ?>
                <tr style="border-bottom: 1px solid #eee;">
                    <td style="padding: 15px;"><strong><?php echo htmlspecialchars($r['invited_name']); ?></strong></td>
                    <td>
                        <?php if($r['status'] == 'verified'): ?>
                            <span style="color: green; font-weight: bold;">Verified (Purchased)</span>
                        <?php else: ?>
                            <span style="color: orange; font-weight: bold;">Pending (Registered)</span>
                        <?php endif; ?>
                    </td>
                    <td>₹<?php echo number_format($r['reward_amount']); ?></td>
                    <td><?php echo date('d M Y', strtotime($r['created_at'])); ?></td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="4" style="padding: 20px; text-align: center; color: #777;">No referrals yet. Share your link!</td></tr>
            <?php endif; ?>
        </tbody>
    </table>

</div>

<script>
function copyRef() {
    var copyText = document.getElementById("refLink");
    copyText.select();
    navigator.clipboard.writeText(copyText.value);
    alert("Link Copied!");
}
</script>

<?php include 'includes/footer.php'; ?>