<!DOCTYPE html>
<html lang="en">
<?php
include('include/head.php');
include("loginCheck.php");
include('include/header.php');
include('include/menu.php');

$pdo = getDB();

date_default_timezone_set("Asia/Kolkata");

$mainId = $_SESSION['admin_user_id'] ?? 0;
$user_id1 = $mainId;

// ── Handle GET request ───────────────────────────────────────────────────────
if (!empty($_GET['user_id'])) {
  $user_id1 = $_GET['user_id'];

  $stmt = $pdo->prepare("SELECT COUNT(*) FROM meddolic_user_details WHERE user_id=?");
  $stmt->execute([$user_id1]);
  if ($stmt->fetchColumn() == 0) {
    echo "<script>alert('Invalid User Id');</script>";
    $user_id1 = $mainId;
  }
}
function investAmount(PDO $pdo, $member_id)
{
  $stmt = $pdo->prepare("SELECT SUM(investPrice) FROM meddolic_user_activation_details WHERE member_id=?");
  $stmt->execute([$member_id]);
  return $stmt->fetchColumn() ?? 0;
}
// ── Get member_id for the searched user ──────────────────────────────────────
$stmt = $pdo->prepare("SELECT member_id FROM meddolic_user_details WHERE user_id=?");
$stmt->execute([$user_id1]);
$member_id = $stmt->fetchColumn() ?? 0;

// ── Fetch direct referrals ───────────────────────────────────────────────────
$stmtDirect = $pdo->prepare("SELECT * FROM meddolic_user_details WHERE sponser_id=? ORDER BY date_time ASC");
$stmtDirect->execute([$member_id]);
$directResults = $stmtDirect->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="main-content">
  <div class="container-fluid">
    <div class="block-header">
      <div class="row">
        <div class="col-lg-5 col-md-8 col-sm-12">
          <h2>
            <a href="javascript:void(0);" onclick="window.history.back();"
              class="btn btn-xs btn-link btn-toggle-fullwidth">
              <i class="fa fa-arrow-left"></i>
            </a>
            My Direct
          </h2>
          <ul class="breadcrumb">
            <li class="breadcrumb-item"><a href="dashboard"><i class="icon-home"></i></a></li>
            <li class="breadcrumb-item"> Team </li>
            <li class="breadcrumb-item active"> My Direct</li>
          </ul>
        </div>
      </div>
    </div>

    <!-- Search Form -->
    <div class="row clearfix">
      <div class="col-md-12">
        <div class="card">
          <div class="body">
            <form>
              <div class="row">
                <div class="col-lg-3 col-md-12">
                  <div class="input-group date">
                    <input class="form-control" type="text" placeholder="Enter User ID" name="user_id"
                      value="<?= htmlspecialchars($user_id1) ?>">
                    <div class="input-group-append">
                      <button class="btn btn-outline-secondary" type="button"><i class="fa fa-user"></i></button>
                    </div>
                  </div>
                </div>
                <div class="col-lg-3 col-md-12">
                  <div class="input-group mb-3">
                    <input class="btn btn-primary" type="submit" value="Search">
                  </div>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

    <!-- Direct Referrals Table -->
    <div class="row clearfix">
      <div class="col-lg-12 col-md-12 col-sm-12">
        <div class="card">
          <div class="header">
            <h2>My Direct</h2>
          </div>
          <div class="body">
            <div class="table-responsive">
              <table class="table table-hover js-basic-example dataTable table-custom mb-0">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>User Id</th>
                    <th>walletAddress</th>
                    <th>Invest Amount</th>
                    <th>Joining Date</th>
                    <th>Active Status</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $count = 0;
                  foreach ($directResults as $valDirect) {
                    $count++;
                    ?>
                    <tr>
                      <td><?= $count ?></td>
                      <td><?= htmlspecialchars($valDirect['user_id']) ?></td>
                      <td><?= htmlspecialchars($valDirect['walletAddress']) ?></td>
                      <td>$ <?= number_format(investAmount($pdo, $valDirect['member_id']), 2) ?></td>
                      <td><i class="fa fa-clock-o"></i> <?= date("d-m-Y H:i:s", strtotime($valDirect['date_time'])) ?>
                      </td>
                      <td><?= ($valDirect['topup_flag'] == 1) ? 'Active' : 'In-Active' ?></td>
                      <td>
                        <a href="myDirect.php?user_id=<?= htmlspecialchars($valDirect['user_id']) ?>">
                          <span class="badge badge-success"><i class="fa fa-users"></i> Team</span>
                        </a>
                      </td>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

<?php include('include/footer.php'); ?>
<script>
  document.getElementById("Team").classList.add("active");
  document.getElementById("myDirect").classList.add("active");
</script>
</body>

</html>