<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<?php
$user_activation_status = $_GET['activation_status'] ?? '';

if ($user_activation_status == '1') {
    $Title = "List Of Active Members";
} else if ($user_activation_status == '0') {
    $Title = "List of UnVerified Members";
} else if ($user_activation_status == '101') {
    $Title = "List of UnActive Members";
} else {
    $Title = "List Of Members";
}
?>

<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<div id="main-wrapper">
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>

<div class="page-wrapper">
<div class="container-fluid">
<br><br>

<div class="row">
<div class="col-12">
    <div class="card">
        <div class="card-body">
            <h4 class="card-title"><?php echo $Title; ?></h4>
            <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6>

            <div class="table-responsive m-t-40">
                <form method="post" action="../Controller/Database_sql.php" enctype="multipart/form-data">
                <table id="example23"
                    class="display nowrap table table-hover table-striped table-bordered"
                    cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>Action</th>
                            <th>Sponsor ID</th>
                            <th>Member Info</th>
                            <th>Verification Status</th>
                            <th>Registered Date</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php

                    // Build query (JOIN sponsor user as "s")
                    if ($level == '1') {

                        if ($user_activation_status == '0') {
                            // Verified account but verification_status = 0
                            $sql_user = "
                                SELECT
                                    u.*,
                                    s.ref_code AS sponsor_ref_code,
                                    s.name AS sponsor_name,
                                    s.nick_name AS sponsor_nick_name
                                FROM user u
                                LEFT JOIN user s
                                    ON u.sponsor_code = s.ref_code
                                WHERE u.level='2'
                                  AND u.status='1'
                                  AND u.activation_status='1'
                                  AND u.verification_status='0'
                                ORDER BY u.id DESC
                            ";
                        }
                        else if ($user_activation_status == '1') {
                            $sql_user = "
                                SELECT
                                    u.*,
                                    s.ref_code AS sponsor_ref_code,
                                    s.name AS sponsor_name,
                                    s.nick_name AS sponsor_nick_name
                                FROM user u
                                LEFT JOIN user s
                                    ON u.sponsor_code = s.ref_code
                                WHERE u.level='2'
                                  AND u.status='1'
                                  AND u.activation_status='1'
                                ORDER BY u.id DESC
                            ";
                        }
                        else if ($user_activation_status == '101') {
                            // UnActive members (status=0, activation=0)
                            $sql_user = "
                                SELECT
                                    u.*,
                                    s.ref_code AS sponsor_ref_code,
                                    s.name AS sponsor_name,
                                    s.nick_name AS sponsor_nick_name
                                FROM user u
                                LEFT JOIN user s
                                    ON u.sponsor_code = s.ref_code
                                WHERE u.level='2'
                                  AND u.status='0'
                                  AND u.activation_status='0'
                                ORDER BY u.id DESC
                            ";
                        }
                        else {
                            $sql_user = "
                                SELECT
                                    u.*,
                                    s.ref_code AS sponsor_ref_code,
                                    s.name AS sponsor_name,
                                    s.nick_name AS sponsor_nick_name
                                FROM user u
                                LEFT JOIN user s
                                    ON u.sponsor_code = s.ref_code
                                WHERE u.level='2'
                                ORDER BY u.id DESC
                            ";
                        }

                    } else {
                        // Non-admin: show only downlines
                        if ($user_activation_status == '0') {
                            $sql_user = "
                                SELECT
                                    u.*,
                                    s.ref_code AS sponsor_ref_code,
                                    s.name AS sponsor_name,
                                    s.nick_name AS sponsor_nick_name
                                FROM user u
                                LEFT JOIN user s
                                    ON u.sponsor_code = s.ref_code
                                WHERE u.sponsor_code='$ref_code'
                                  AND u.level='2'
                                  AND u.status='1'
                                  AND u.activation_status='0'
                                ORDER BY u.id DESC
                            ";
                        } else {
                            $sql_user = "
                                SELECT
                                    u.*,
                                    s.ref_code AS sponsor_ref_code,
                                    s.name AS sponsor_name,
                                    s.nick_name AS sponsor_nick_name
                                FROM user u
                                LEFT JOIN user s
                                    ON u.sponsor_code = s.ref_code
                                WHERE u.sponsor_code='$ref_code'
                                  AND u.level='2'
                                  AND u.status='1'
                                  AND u.activation_status='$user_activation_status'
                                ORDER BY u.id DESC
                            ";
                        }
                    }

                    $result_user = $mysqli->query($sql_user);
                    if ($result_user && $result_user->num_rows > 0) {
                        while($record = $result_user->fetch_assoc()) {

                            $id = $record['id'];
                            $member_ref_code = $record['ref_code'];
                            $member_nick_name = $record['nick_name'];
                            $member_trans_code = $record['trans_code'];
                            $member_sponsor_code = $record['sponsor_code'];

                            $member_name = $record['name'];
                            $member_email = $record['email'];
                            $member_phone = $record['phone_number'];

                            $member_activation_status = $record['activation_status'];
                            $member_verification_status = $record['verification_status'];

                            $created_date_raw = $record['created_date'];
                            $created_date_disp = '';
                            if (!empty($created_date_raw)) {
                                $created_date_disp = date('d-m-Y', strtotime($created_date_raw));
                            }

                            $member_verification_status_display = ($member_verification_status == '1') ? "Verified" : "Unverified";

                            // sponsor display
                            $sponsor_ref_code = $record['sponsor_ref_code'] ?? $member_sponsor_code;
                            $sponsor_name = $record['sponsor_name'] ?? '';
                            $sponsor_nick = $record['sponsor_nick_name'] ?? '';

                            // Optional: get latest purchase data (your existing logic)
                            $sql_package_purchase = "SELECT *
                                                     FROM woocommerce_order_info
                                                     WHERE new_ref_code = '$member_ref_code'
                                                       AND status = '1'
                                                     ORDER BY id DESC
                                                     LIMIT 1";
                            $result_package_purchase = $mysqli->query($sql_package_purchase);
                            if ($result_package_purchase && $result_package_purchase->num_rows > 0) {
                                $row_package_purchase = $result_package_purchase->fetch_assoc();
                                $order_items  = $row_package_purchase['order_items'] ?? '';
                                $order_items = str_replace("|Free shipping", "", $order_items);
                            }

                    ?>
                        <tr>
                            <td>
                                <?php if ($level == '1') { ?>
                                    <?php if ($user_activation_status == "101") { ?>
                                        <a href="Form-Verification.php?ref_code=<?php echo $member_ref_code; ?>&from_page=list_of_user.php&activation_status=101"
                                           class="btn btn-success">
                                           <i class="fa fa-podcast"></i>&nbsp; Activate Member
                                        </a>

                                        <a href="../Controller/Database_sql.php?form_name_Delete_User=Delete_User&id=<?php echo $id; ?>&ref_code=<?php echo $member_ref_code; ?>&activation_status=<?php echo $user_activation_status; ?>"
                                           onClick="javascript: return confirm('Do you want to proceed with the deletion ?');"
                                           class="btn btn-danger">
                                           <i class="fa fa-trash"></i>&nbsp; Delete
                                        </a>
                                    <?php } else { ?>
                                        <a href="user_detail.php?user_ref_code=<?php echo $member_ref_code; ?>" class="btn btn-success">
                                            <i class="fa fa-podcast"></i>&nbsp; View
                                        </a>
                                        <a href="../Controller/Database_sql.php?form_name_Delete_Active_User=Delete_Active_User&id=<?php echo $id; ?>&ref_code=<?php echo $member_ref_code; ?>&trans_code=<?php echo $member_trans_code; ?>&activation_status=<?php echo $user_activation_status; ?>"
                                           onClick="javascript: return confirm('Do you want to proceed with the deletion ?');"
                                           class="btn btn-danger">
                                           <i class="fa fa-trash"></i>&nbsp; Delete
                                        </a>
                                    <?php } ?>
                                <?php } ?>
                            </td>

                            <td>
                                <?php echo htmlspecialchars($sponsor_ref_code, ENT_QUOTES); ?><br>
                                <?php echo htmlspecialchars($sponsor_name, ENT_QUOTES); ?><br>
                                <?php echo htmlspecialchars($sponsor_nick, ENT_QUOTES); ?>
                            </td>

                            <td>
                                <?php echo htmlspecialchars($member_ref_code, ENT_QUOTES); ?><br>
                                <?php echo htmlspecialchars($member_nick_name, ENT_QUOTES); ?><br>
                                <?php echo htmlspecialchars($member_name, ENT_QUOTES); ?><br>
                                <?php echo htmlspecialchars($member_email, ENT_QUOTES); ?><br>
                                <?php echo htmlspecialchars($member_phone, ENT_QUOTES); ?>
                            </td>

                            <td><?php echo $member_verification_status_display; ?></td>
                            <td><?php echo $created_date_disp; ?></td>
                        </tr>
                    <?php
                        }
                    }
                    ?>
                    </tbody>
                </table>
                </form>
            </div>

        </div>
    </div>
</div>
</div>

</div>
</div>

<?php include ("../Footer/footer.php"); ?>

</div>

<?php include ("../Header/all_jquery.php"); ?>

</body>
</html>