<?php

$url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

  if(strpos($url, 'error') == true){


 echo '<script>
    setTimeout(function() {
        swal({
            title: "Error!",
            text: "Error Occured",
            type: "error"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
  </script>';
  

    }elseif (strpos($url, 'added') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "New Enrollee added successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    } elseif (strpos($url, 'depadd') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "New Dependant added successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }
    elseif (strpos($url, 'bulk_approved') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Bulk Approval Successful",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }elseif (strpos($url, 'approved') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Approved",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    } elseif (strpos($url, 'success') == true) {
            
     echo '<script>
        setTimeout(function() {
            swal({
                title: "Success!",
                text: "Bulk Upload Completed",
                type: "success"
            }, function() {
                window.location = "#0";
            });
        }, 1000);
    </script>';

    } elseif (strpos($url, 'denied') == true) {
        
 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Denied",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
</script>';

    }




?>







<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ADMIN PAGE</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    
    
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
    <style>
    /* Customize any additional styles here */
    body {
        background-image: url(../images/final_20864245705555.5b18e0a95657b9.83368454.jpg);
    }

    .pagination {
        justify-content: center;
    }

    /* Adjust search bar width and height */
    #searchInput {
        width: 250px;
        /* Reduced width */
        /*height: 25px; */
        /* Reduced height */
    }

    /* Add alternating row colors */
    tbody tr:nth-child(even) {
        background-color: #f2f2f2;
    }

    /* Filter dropdown style */
    #filterDropdown {
        width: 200px;
        /* Adjust as needed */
    }

    /* item dropdown style */
    #perPage {
        width: 200px;
        /* Adjust as needed */
    }

    #dataTable {
        table-layout: auto;
        width: 130%;
        /* Optional: Set a specific width for the table */
    }

    .container {
        width: 100%;
        max-width: 100vw;
        overflow-x: auto;
        padding: 20px;
        /* Adjust padding as needed */
        box-sizing: border-box;
        height: 100%;
    }

    table {
        width: 100%;
        border-collapse: collapse;
    }

    th,
    td {
        border: 1px solid #ddd;
        padding: 8px;
        text-align: left;
    }

    th {
        background-color: #f2f2f2;
    }

    .container {
        width: 100% !important;
        padding: 5px !important;
        margin-left: 20px !important;
        /* Remove left margin */
        margin-right: 40px !important;
        /* Remove right margin */
    }
    </style>

    <style>
    /* Modal styles */
    .modal,
    .modal2 {
        display: none;
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
    }

    .modal-content,
    .modal-content2 {
        background-color: #fefefe;
        margin: 2% auto;
        padding: 20px 50px;
        border: 1px solid #888;
        width: 70%;
        height: 90vh;
        overflow-y: auto;

    }

    .close,
    .close2 {
        color: #aaa;
        /* float: right; */
        font-size: 38px;
        font-weight: bold;
    }

    .close:hover,
    .close:focus,
    .close2:hover,
    .close2:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }



    .inputdiv {
        display: block;
    }

    .inputdiv input {
        padding: 10px 20px;
        outline: none;
        border-radius: 10px;
        border: 2px solid gray;

    }
    </style>




</head>

<body>


    
   <div style="text-align:center">
    <img alt="" class="form-image pt-3" style="border:0" src="../images/logo.png" height="150px" width=""
        data-component="image">
    <div class="container-fluid">
        <div class="mb-3 mt-4">
            
        <button id="addnew" class="btn btn-success mb-3" onclick="location.href='./add.php'">Add New</button>
        <button id="addtrans" class="btn btn-primary mb-3" onclick="location.href='./addtransaction.php'">Add Transaction</button>
        </div>
        <div class="container-fluid d-flex px-4 py-2">
            <div class="d-flex align-items-center px-4 py-2">
                    <label for="searchInput" class="text-black me-2">Search:</label>
                    <input type="text" id="searchInput" class="form-control" oninput="performSearch()" placeholder="Search">
            </div>
            <div class="d-flex align-items-center">
                <label for="perPage" class="text-black me-2">Items:</label>
                <select id="perPage" class="form-select" onchange="changePerPage(this)">
                    <option value="5">5</option>
                    <option value="10" selected>10</option>
                    <option value="20">20</option>
                    <option value="50">50</option>
                </select>
            </div>
            <div class="d-flex align-items-center px-4">
                <label for="filterDropdown" class="text-black me-2">Filter:</label>
                <select id="filterDropdown" class="form-select" onchange="applyFilter()">
                    <option value="All" <?php if ($filter == 'All') echo 'selected'; ?>>All</option>
                    <option value="Pending" <?php if ($filter == 'live') echo 'selected'; ?>>Live</option>
                    <option value="Approved" <?php if ($filter == 'suspended') echo 'selected'; ?>>Suspended</option>
                </select>
            </div>
            
        </div>
        <div class="mb-3">
            <div class="container">
                
                <table id="dataTable" class="table table-bordered">
                    <!-- Table Headings -->
                    <thead>
                        <tr>
                            <th><input type="checkbox" onclick="toggle(this);"></th>
                            <th>SN</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Phone Number</th>
                            <th>Email</th>
                            <th>Total Deposit</th>
                            <th>Bonus</th>
                            <th>Profit</th>
                            <th>Profit Percentage</th>
                            <th>Last Deposit</th>
                            <th>Last Withdrawal</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <!-- Table Body -->
                    <tbody>
                        <?php
                            // Connect to MySQL
                            include_once '../secure/dbconn.php';
                            // Pagination variables
                            $limit = isset($_GET['per_page']) ? $_GET['per_page'] : 10; // Number of records per page
                            $page = isset($_GET['page']) ? $_GET['page'] : 1;
                            $start = ($page - 1) * $limit;
                            // Get filter value
                            $filter = isset($_GET['filter']) ? $_GET['filter'] : 'All'; // Default filter value
                            // Adjust SQL query based on filter value
                            if ($filter === 'All') {
                                if (isset($_GET['search'])) {
                                    $search = $_GET['search'];
                                    $sql = "SELECT * FROM `users` WHERE `email_address` LIKE '%$search%' OR `first_name` LIKE '%$search%' ORDER BY id DESC LIMIT $start, $limit";
                                } else {
                                    $sql = "SELECT * FROM `users` ORDER BY id DESC LIMIT $start, $limit";
                                }
                            } else {
                                if (isset($_GET['search'])) {
                                    $search = $_GET['search'];
                                    $sql = "SELECT * FROM `users` WHERE status = '$filter' AND (`email_address` LIKE '%$search%' OR `first_name` LIKE '%$search%') ORDER BY id DESC LIMIT $start, $limit";
                                } else {
                                    $sql = "SELECT * FROM `users` WHERE status = '$filter' ORDER BY id DESC LIMIT $start, $limit";
                                }
                            }
                            // Fetch data with pagination and filter
                            $result = mysqli_query($conn, $sql);
                            // Count total records
                            $total_records = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `users`"));
                            // Calculate total pages
                            $total_pages = ceil($total_records / $limit);
                            if (mysqli_num_rows($result) > 0) {
                                $serial = ($page - 1) * $limit + 1;
                                while($row = mysqli_fetch_assoc($result)) {
                                    echo "<tr>";
                                    // Add checkbox column
                                    echo "<td><input type='checkbox' name='selected[]' value='" . $row['id'] . "'></td>";
                                    // Output other table cells with data from database
                                    // Adjust as per your database schema
                                    // Example: echo "<td>" . $row['column_name'] . "</td>";
                                    // Adjust other table cells accordingly
                                    echo "<td>" . $serial . "</td>";
                                    echo "<td>" . $row['first_name'] . "</td>";
                                    echo "<td>" . $row['last_name'] . "</td>";
                                    echo "<td>" . $row['phone_number'] . "</td>";
                                    echo "<td>" . $row['email_address'] . "</td>";
                                    echo "<td>" . $row['total_deposit'] . "</td>";
                                    echo "<td>" . $row['bonus'] . "</td>";
                                    echo "<td>" . $row['profit'] . "</td>";
                                    echo "<td>" . $row['profit_percentage'] . "</td>";
                                    echo "<td>" . $row['last_deposit'] . "</td>";
                                    echo "<td>" . $row['last_withdrawal'] . "</td>";
                                    echo "<td>" . $row['status'] . "</td>";
                                    echo "<td>";
                                    echo "<div class='btn-group'>";
                                    echo "<a href='edit.php?id=" . $row['id'] . "' class='btn btn-warning'><i class='fas fa-pen'></i></a>";
                                    echo "</div>";
                                    echo "</td>";
                                    echo "</tr>";
                                    $serial++;
                                }
                            } else {
                                echo "<tr><td colspan='18'>No data found</td></tr>";
                            }
                            // Close connection
                            mysqli_close($conn);
                        ?>
                    </tbody>
                </table>
            </div>
            <ul class="pagination justify-content-center">
                <?php if ($page > 1) : ?>
                <li class="page-item"><a class="page-link"
                        href="?page=<?php echo ($page - 1) . (isset($_GET['per_page']) ? '&per_page=' . $_GET['per_page'] : ''); ?>"><i
                            class="fas fa-chevron-left"></i></a></li>
                <?php endif; ?>
                <?php for ($i = 1; $i <= $total_pages; $i++) : ?>
                <li class="page-item <?php if ($i == $page) echo 'active'; ?>"><a class="page-link"
                        href="?page=<?php echo $i . (isset($_GET['per_page']) ? '&per_page=' . $_GET['per_page'] : ''); ?>"><?php echo $i; ?></a>
                </li>
                <?php endfor; ?>
                <?php if ($page < $total_pages) : ?>
                <li class="page-item"><a class="page-link"
                        href="?page=<?php echo ($page + 1) . (isset($_GET['per_page']) ? '&per_page=' . $_GET['per_page'] : ''); ?>"><i
                            class="fas fa-chevron-right"></i></a></li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</div>

<script>
    function toggle(source) {
        checkboxes = document.getElementsByName('selected[]');
        for (var i = 0, n = checkboxes.length; i < n; i++) {
            checkboxes[i].checked = source.checked;
        }
    }

    function performSearch() {
        var searchInput = document.getElementById('searchInput').value.toLowerCase();
        var tableRows = document.querySelectorAll('#dataTable tbody tr');

        tableRows.forEach(function(row) {
            var email_address = row.cells[7].innerText.toLowerCase(); // Index of HMO ID column
            var first_name = row.cells[4].innerText.toLowerCase(); // Index of Surname column

            if (email_address.includes(searchInput) || first_name.includes(searchInput)) {
                row.style.display = '';
            } else {
                row.style.display = 'none';
            }
        });
    }

    function changePerPage(select) {
        var value = select.value;
        window.location.href = '?page=1&per_page=' + value;
    }

    function applyFilter() {
        var filterValue = document.getElementById('filterDropdown').value;
        window.location.href = '?filter=' + filterValue;
    }
</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/js/all.min.js"></script>

<script>

    // JavaScript to set selected filter option after page refresh
    document.addEventListener("DOMContentLoaded", function() {
        var filterValue = '<?php echo $filter; ?>';
        var filterDropdown = document.getElementById('filterDropdown');
        for (var i = 0; i < filterDropdown.options.length; i++) {
            if (filterDropdown.options[i].value === filterValue) {
                filterDropdown.selectedIndex = i;
                break;
            }
        }
    });
</script>


</body>

</html>