<?php
require_once '../config/database.php';
secureSessionStart();
requireLogin();

$conn = getConnection();
$user_id = isset($_GET['user_id']) ? mysqli_real_escape_string($conn, $_GET['user_id']) : 0;

if (!$user_id) {
    exit('No user ID provided');
}

// Get user details
$query = "SELECT * FROM users WHERE user_id = '$user_id'";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

if (!$user) {
    exit('User not found');
}
?>

<!-- Info Tab Content -->
<div class="user-profile text-center mb-4">
    <div class="user-avatar-large">
        <img src="../uploads/profile/<?php echo $user['image'] ?: 'noprofil.jpg'; ?>" 
             onerror="this.src='../uploads/profiles/noprofil.jpg'">
    </div>
    <h5 class="user-name-large"><?php echo htmlspecialchars($user['name'] ?: 'N/A'); ?></h5>
    <div class="user-id-large">ID: <?php echo $user['user_id']; ?></div>
</div>

<div class="info-section">
    <h6 class="info-title"><i class="fas fa-user me-2"></i>Personal Information</h6>
    <div class="info-grid">
        <div class="info-item">
            <div class="info-label">Full Name</div>
            <div class="info-value"><?php echo htmlspecialchars($user['name'] ?: 'N/A'); ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Mobile</div>
            <div class="info-value"><?php echo $user['mobile'] ?: 'N/A'; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Email</div>
            <div class="info-value"><?php echo htmlspecialchars($user['email'] ?: 'N/A'); ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Date of Birth</div>
            <div class="info-value"><?php echo $user['dob'] ?: 'N/A'; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Gender</div>
            <div class="info-value">
                <?php 
                if ($user['gender'] == 0) echo 'Male';
                elseif ($user['gender'] == 1) echo 'Female';
                else echo 'Other';
                ?>
            </div>
        </div>
        <div class="info-item">
            <div class="info-label">PAN Card</div>
            <div class="info-value"><?php echo $user['panno'] ?: 'N/A'; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Aadhaar</div>
            <div class="info-value"><?php echo $user['aadharno'] ?: 'N/A'; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Joined</div>
            <div class="info-value"><?php echo date('d M Y', strtotime($user['joining_date'] ?? $user['created_at'] ?? 'now')); ?></div>
        </div>
    </div>
</div>

<div class="info-section">
    <h6 class="info-title"><i class="fas fa-sitemap me-2"></i>Binary Information</h6>
    <div class="info-grid">
        <div class="info-item">
            <div class="info-label">Left Count</div>
            <div class="info-value"><?php echo $user['left_count'] ?? 0; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Right Count</div>
            <div class="info-value"><?php echo $user['right_count'] ?? 0; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Sponsor ID</div>
            <div class="info-value"><?php echo $user['sponsor_id'] ?: 'N/A'; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Placement</div>
            <div class="info-value"><?php echo $user['placement_id'] ?: 'N/A'; ?></div>
        </div>
    </div>
</div>

<div class="info-section">
    <h6 class="info-title"><i class="fas fa-wallet me-2"></i>Financial Information</h6>
    <div class="info-grid">
        <div class="info-item">
            <div class="info-label">Wallet Balance</div>
            <div class="info-value text-success">₹<?php echo number_format($user['wallet'] ?? 0, 2); ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Total Withdrawal</div>
            <div class="info-value">₹<?php echo number_format($user['withdrawl_inc'] ?? 0, 2); ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Rank</div>
            <div class="info-value"><?php echo $user['rank'] ?: 'No Rank'; ?></div>
        </div>
        <div class="info-item">
            <div class="info-label">Package</div>
            <div class="info-value">
                <?php 
                if (isset($user['package_id'])) {
                    if ($user['package_id'] == 2500) echo 'Basic (₹2500)';
                    elseif ($user['package_id'] == 3500) echo 'Premium (₹3500)';
                    else echo 'No Package';
                } else {
                    echo 'No Package';
                }
                ?>
            </div>
        </div>
    </div>
</div>