<!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");
$user_id1 = "";
$show_date = date("d-m-Y");
$show_date1 = date("d-m-Y");
$cal_date = date("Y-m-d");
$cal_date1 = date("Y-m-d");

if ($_GET) {
    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 = $_SESSION['admin_user_id'] ?? '';
        }
    }
    if (!empty($_GET['from_date'])) {
        $show_date = $_GET['from_date'];
        $cal_date = date("Y-m-d", strtotime($show_date));
    }
    if (!empty($_GET['to_date'])) {
        $show_date1 = $_GET['to_date'];
        $cal_date1 = date("Y-m-d", strtotime($show_date1));
    }
}

// ── Helper functions ─────────────────────────────────────────────────────────
function getName(PDO $pdo, $member_id)
{
    $stmt = $pdo->prepare("SELECT walletAddress FROM meddolic_user_details WHERE member_id=?");
    $stmt->execute([$member_id]);
    return $stmt->fetchColumn() ?? '';
}

function getUserId(PDO $pdo, $member_id)
{
    $stmt = $pdo->prepare("SELECT user_id FROM meddolic_user_details WHERE member_id=?");
    $stmt->execute([$member_id]);
    return $stmt->fetchColumn() ?? '';
}

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;
}
function getActivationType(PDO $pdo, $member_id)
{
    $stmt = $pdo->prepare("SELECT buyType FROM meddolic_user_activation_details WHERE member_id=? ORDER BY date_time DESC LIMIT 1");
    $stmt->execute([$member_id]);
    $type = $stmt->fetchColumn() ?? 0;

    switch ($type) {
        case 1:
            return '<span style="color: green; font-weight: bold;">Register</span>';
        case 2:
            return '<span style="color: orange; font-weight: bold;">Upgrade</span>';
        case 3:
            return '<span style="color: blue; font-weight: bold;">Matrix</span>';
        default:
            return '<span style="color: gray;">N/A</span>';
    }
}
$whereClause = "";
$params = [];

if ($user_id1 != "") {
    $whereClause .= " AND user_id=?";
    $params[] = $user_id1;
}

// ── Fetch investment history ────────────────────────────────────────────────
$sql = "SELECT member_id, user_id, walletAddress, sponser_id, date_time, activation_date 
        FROM meddolic_user_details 
        WHERE member_id != 1 AND topup_flag=1 AND user_type=2 
        AND CAST(activation_date AS DATE) BETWEEN ? AND ? 
        $whereClause 
        ORDER BY activation_date DESC";

$params = array_merge([$cal_date, $cal_date1], $params);
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="main-content">
    <div class="container-fluid">
        <!-- Header + Form -->
        <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>
                        Investment History
                    </h2>
                    <ul class="breadcrumb">
                        <li class="breadcrumb-item"><a href="dashboard"><i class="icon-home"></i></a></li>
                        <li class="breadcrumb-item">Member</li>
                        <li class="breadcrumb-item active">Investment History</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 mb-3">
                                    <input data-provide="datepicker" data-date-autoclose="true" class="form-control"
                                        name="from_date" id="from_date" placeholder="From Date" required
                                        value="<?= $show_date ?>" readonly>
                                </div>
                                <div class="col-lg-3 col-md-12 mb-3">
                                    <input data-provide="datepicker" data-date-autoclose="true" class="form-control"
                                        name="to_date" id="to_date" placeholder="To Date" required
                                        value="<?= $show_date1 ?>" readonly>
                                </div>
                                <div class="col-lg-3 col-md-12 mb-3">
                                    <input class="btn btn-primary" type="submit" value="Search">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- Investment Table -->
        <div class="row clearfix">
            <div class="col-lg-12 col-md-12 col-sm-12">
                <div class="card">
                    <div class="header">
                        <h2>Investment History</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>Wallet Address</th>
                                        <th>Sponsor</th>
                                        <th>Total Investment</th>
                                        <th>Joining Date</th>
                                        <th>Active Date</th>
                                        <th>Activation Type</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $count = 0;
                                    foreach ($results as $val) {
                                        $count++;
                                        ?>
                                        <tr>
                                            <td><?= $count ?></td>
                                            <td><?= htmlspecialchars($val['user_id']) ?></td>
                                            <td><?= htmlspecialchars($val['walletAddress']) ?></td>
                                            <td><?= htmlspecialchars(getName($pdo, $val['sponser_id']) . " (User ID:" . getUserId($pdo, $val['sponser_id']) . ")") ?>
                                            </td>
                                            <td>$ <?= number_format(investAmount($pdo, $val['member_id']), 2) ?></td>
                                            <td><?= htmlspecialchars($val['date_time']) ?></td>
                                            <td><?= htmlspecialchars($val['activation_date']) ?></td>
                                            <td><?= getActivationType($pdo, $val['member_id']) ?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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

</html>