<?php

include('con.php');

/* ================= SESSION CHECK ================= */
if (!isset($_SESSION['phone'])) {
    header("Location: login.php");
    exit;
}

$mobile = $_SESSION['phone'];
$customerName = "Unknown User";
$balance = 0.00;
$commission_balance = 0.00;
$show_activate = false;

/* ================= USER INFO ================= */
$query = mysqli_query($ahk_conn, "SELECT name, balance, parent FROM users WHERE phone='$mobile'");

if ($query && mysqli_num_rows($query) > 0) {
    $user = mysqli_fetch_assoc($query);
    $customerName = $user['name'];
    $balance = number_format($user['balance'], 2);
    $parent_uid = $user['parent'];
} else {
    $parent_uid = '';
}

/* ================= ACTIVATE COMMISSION WALLET ================= */
if (isset($_GET['activate_commission'])) {

    $checkAgain = mysqli_query($ahk_conn, "SELECT id FROM commissionuser WHERE username='$mobile'");

    if (mysqli_num_rows($checkAgain) == 0) {
        mysqli_query($ahk_conn, "
            INSERT INTO commissionuser (username, balance, status, refusername) 
            VALUES ('$mobile', '0', '1', '$parent_uid')
        ");
    }

    header("Location: dashboard.php");
    exit;
}

/* ================= CHECK COMMISSION WALLET ================= */
$checkComm = mysqli_query($ahk_conn, "SELECT balance FROM commissionuser WHERE username='$mobile'");

if ($checkComm && mysqli_num_rows($checkComm) > 0) {
    $comm = mysqli_fetch_assoc($checkComm);
    $commission_balance = number_format($comm['balance'], 2);
} else {
    $show_activate = true;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        .card-hover {
            cursor: pointer;
            transition: transform 0.2s ease-in-out;
        }
        .card-hover:hover {
            transform: scale(1.03);
        }
        .card-img-top {
            height: 100px;
            object-fit: contain;
            padding: 10px;
        }
    </style>
</head>

<body>

<!-- ================= NAVBAR ================= -->
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm mb-4">
    <div class="container">

        <a class="navbar-brand" href="dashboard.php">HarGharRojgar</a>
        <a class="navbar-text me-3" href="/members/dashboard.php">Portal 1.0</a>

        <div class="d-flex ms-auto align-items-center">

            <span class="navbar-text me-3">
                Welcome, <strong><?php echo htmlspecialchars($customerName); ?></strong>
            </span>

            <span class="navbar-text me-3 text-success">
                Main Balance: ₹<?php echo $balance; ?>
            </span>

            <!-- ================= COMMISSION SECTION ================= -->
            <?php if ($show_activate): ?>
                <a href="?activate_commission=1" class="btn btn-warning btn-sm me-3">
                    Click Here To Activate Commission Wallet
                </a>
            <?php else: ?>
    <a href="withdraw.php" class="btn btn-outline-primary btn-sm me-3">
        Commission: ₹<?php echo $commission_balance; ?>
    </a>
<?php endif; ?>
            <a href="logout.php" class="btn btn-outline-danger btn-sm">Logout</a>
        </div>
    </div>
</nav>


</body>
</html>