<?php
include('include/head.php');
include("loginCheck.php");
include('include/header.php');
include('include/menu.php');
// requireLogin();

$db = getDB();

$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");

/* FILTERS */
if (!empty($_GET['user_id'])) {
    $user_id1 = trim($_GET['user_id']);

    $st = $db->prepare("SELECT COUNT(*) FROM meddolic_user_details WHERE user_id=?");
    $st->execute([$user_id1]);
    $exists = $st->fetchColumn();

    if ($exists == 0) {
        echo "<script>alert('Invalid User Id')</script>";
        $user_id1 = "";
    }
}

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));
}

/* BUILD QUERY */
$sql = "
SELECT 
a.name,
a.user_id,
a.member_id,
a.walletAddress,
a.password,
a.account_status,
a.topup_flag,
a.date_time,
b.user_id AS sponserId,
b.walletAddress AS sponserName
FROM meddolic_user_details a
JOIN meddolic_user_details b ON a.sponser_id=b.member_id
WHERE DATE(a.date_time) BETWEEN ? AND ?
AND a.member_id!=1
AND a.user_type=2
AND a.account_status=1
";

$params = [$cal_date, $cal_date1];

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

$sql .= " ORDER BY a.date_time DESC";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$users = $stmt->fetchAll();
?>
<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> View
                        Member</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"> View Member</li>
                    </ul>
                </div>
            </div>
        </div>
        <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="<?= $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 data-provide="datepicker" data-date-autoclose="true" class="form-control"
                                            name="from_date" id="from_date" placeholder="e.g. From Date" required
                                            value="<?= $show_date; ?>" readonly>
                                    </div>
                                </div>
                                <div class="col-lg-3 col-md-12">
                                    <div class="input-group mb-3">
                                        <input name="to_date" id="to_date" data-provide="datepicker"
                                            data-date-autoclose="true" class="form-control" placeholder="e.g. To Date"
                                            required="" value="<?= $show_date1; ?>" readonly>
                                    </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>
        <div class="row clearfix">
            <div class="col-lg-12 col-md-12 col-sm-12">
                <div class="card">
                    <div class="header">
                        <h2>View Member</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>UserID</th>
                                        <th>wallet Address</th>
                                        <th>Sponser</th>
                                        <th>Status</th>
                                        <th>Joinig Date</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $count = 0;
                                    foreach ($users as $valUser) {
                                        $count++;
                                        ?>
                                        <tr>
                                            <td><?= $count ?></td>

                                            <td><?= e($valUser['user_id']) ?></td>

                                            <td><?= e($valUser['walletAddress']) ?></td>

                                            <td>
                                                <?= "(SponserId: " . $valUser['sponserId'] . " [ " . $valUser['sponserName'] . " ])" ?>
                                            </td>

                                            <td>
                                                <?= $valUser['topup_flag'] == 1 ? "Active" : "Not-Active" ?>
                                            </td>

                                            <td>
                                                <?= date("d-m-Y H:i:s", strtotime($valUser['date_time'])) ?>
                                            </td>

                                            <td><a href="viewMemberDetails.php?user_id=<?= $valUser['user_id'] ?>"
                                                    class="btn btn-success btn-sm">More</a> </td>

                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include('include/footer.php'); ?>
<script>
    function memberDashboard(userId, password, memberId) {
        var url = '../User/setSessionAdvice.php?userId=' + userId + '&mID=' + password + '&codeGenerate=' + memberId;
        window.open(url, '_blank');
    };

    function blockUser(memberId, blockStatus) {
        if (memberId != "") {
            if (confirm('Are you sure to Block this Member?')) {
                $.ajax({
                    type: "POST",
                    url: 'ajaxCalls/blockUnBlockUserAjax',
                    data: {
                        memberId: memberId,
                        blockStatus: blockStatus
                    },
                    cache: false,
                    success: function (data) {
                        alert(data);
                        if (data) {
                            alert('User Block Successfully');
                            location.reload();
                        }
                    }
                });
            }
        }
    }

    function unBlockUser(memberId, blockStatus) {
        if (memberId != "") {
            if (confirm('Are you sure to Un-Block this Member?')) {
                $.ajax({
                    type: "POST",
                    url: 'ajaxCalls/blockUnBlockUserAjax',
                    data: {
                        memberId: memberId,
                        blockStatus: blockStatus
                    },
                    cache: false,
                    success: function (data) {
                        // alert(data);
                        if (data) {
                            alert('User Un-Block Successfully');
                            location.reload();
                        }
                    }
                });
            }
        }
    }
    var d = document.getElementById("Members");
    d.className += " active";
    var d = document.getElementById("viewMember");
    d.className += " active";
</script>
</body>

</html>