<?php 
require("top.php"); 

// Handle status change (Active/Inactive)
if (isset($_GET['type']) && $_GET['type'] != '') {
    $type = mysqli_real_escape_string($con, $_GET['type']);

    if ($type == 'status') {
        $operation = mysqli_real_escape_string($con, $_GET['operation']);
        $id = mysqli_real_escape_string($con, $_GET['id']);
        $status = ($operation == 'active') ? 1 : 0;

        // Use prepared statements for status update
        $update_status_sql = "UPDATE user_register SET status = ? WHERE id = ?";
        if ($stmt = mysqli_prepare($con, $update_status_sql)) {
            mysqli_stmt_bind_param($stmt, "ii", $status, $id);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
        }
    }

    // Handle user deletion
    if ($type == 'delete') { 
        $id = mysqli_real_escape_string($con, $_GET['id']);
        
        // Use prepared statements for deletion
        $delete_sql = "DELETE FROM user_register WHERE id = ?";
        if ($stmt = mysqli_prepare($con, $delete_sql)) {
            mysqli_stmt_bind_param($stmt, "i", $id);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
        }
    }
}

// Handle the search
$search = "";
if (isset($_POST['search'])) {
    $search = mysqli_real_escape_string($con, $_POST['search']);
}

// Pagination Logic
$limit = 10;  // Number of records per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;  // Current page
$offset = ($page - 1) * $limit;  // Calculate offset for SQL query

// Fetch users data with search functionality and pagination
$sql = "SELECT * FROM user_register 
        WHERE username LIKE '%$search%' OR email LIKE '%$search%' 
        ORDER BY id DESC LIMIT $limit OFFSET $offset";
$res = mysqli_query($con, $sql);

// Fetch total records for pagination
$total_sql = "SELECT COUNT(*) as total FROM user_register WHERE username LIKE '%$search%' OR email LIKE '%$search%'";
$total_res = mysqli_query($con, $total_sql);
$total_row = mysqli_fetch_assoc($total_res);
$total_records = $total_row['total'];
$total_pages = ceil($total_records / $limit);

?>

<div class="col-sm-9 mx-auto my-4">
    <div class="col-sm-12">
        <div class="card" style="width: 1310px;">
            <div class="card-body">
                <h4 class="text-center text-primary">Users</h4>
                
                <!-- Search Form -->
                <form method="POST" class="mb-4">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Search by Name or Email" value="<?php echo htmlspecialchars($search); ?>">
                        <button type="submit" class="btn btn-primary">Search</button>
                    </div>
                </form>
            </div>
            <table class="table table-bordered table-striped table-responsive">
                <thead class="thead-dark">
                    <tr class="bg-secondary">
                        <th>#</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Password</th>
                        <th>City</th>
                        <th>Country</th>
                        <th>Student or Professional</th>
                        <th>College Name</th>
                        <th>Organisation Name (For Professional)</th>
                        <th>Experience</th>
                        <th>Purpose of Certification</th>
                        <th>Mobile No</th>
                        <th>Status</th>
                        <th>Documents</th>
                        <th>Date</th>
                        <th>Action</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $i = 1;
                    while($row = mysqli_fetch_assoc($res)) { ?>
                        <tr>
                            <td><?php echo $i++; ?></td>
                            <td><?php echo $row['username']; ?></td>
                            <td><?php echo $row['email']; ?></td>
                            <td><?php echo $row['password']; ?></td>
                            <td><?php echo $row['location']; ?></td>
                            <td><?php echo $row['country']; ?></td>
                            <td><?php echo $row['student']; ?></td>
                            <td><?php echo $row['collage']; ?></td>
                            <td><?php echo $row['organisation']; ?></td>
                            <td><?php echo $row['years']; ?></td>
                            <td><?php echo $row['purpose']; ?></td>
                            <td><?php echo $row['mobile']; ?></td>
                            <td>
                                <?php 
                                if($row['status'] == 1){
                                    echo "<a class='badge badge-success' href='?type=status&operation=deactive&id=".$row['id']."'>Active</a>&nbsp;";
                                } else {
                                    echo "<a class='badge badge-warning' href='?type=status&operation=active&id=".$row['id']."'>Inactive</a>&nbsp;";
                                }
                                ?>
                            </td>
                            <td>
                                <a href="download.php?file=<?php echo $row['doc1']; ?>">Doc1</a>
                                <a href="download.php?file=<?php echo $row['doc2']; ?>">Doc2</a>
                                <a href="download.php?file=<?php echo $row['doc3']; ?>">Doc3</a>
                            </td>
                            <td><?php echo $row['created_at']; ?></td>
                            <td class="text-center">
                                <!-- Deletion Action -->
                                <a class="text-danger" href="javascript:confirmDelete(<?php echo $row['id']; ?>)">
                                    <i class="fa fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Confirmation Script for Deletion -->
<script>
    function confirmDelete(id) {
        if(confirm('Are you sure you want to delete this user?')) {
            window.location.href = '?type=delete&id=' + id;
        }
    }
</script>

<!-- Pagination -->
<div class="text-center mt-4">
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <li class="page-item <?php echo ($page == 1) ? 'disabled' : ''; ?>">
                <a class="page-link" href="?page=1<?php echo ($search) ? '&search=' . $search : ''; ?>">First</a>
            </li>
            <li class="page-item <?php echo ($page == 1) ? 'disabled' : ''; ?>">
                <a class="page-link" href="?page=<?php echo ($page - 1) ? $page - 1 : 1; ?><?php echo ($search) ? '&search=' . $search : ''; ?>">Previous</a>
            </li>
            
            <?php for ($p = 1; $p <= $total_pages; $p++): ?>
                <li class="page-item <?php echo ($page == $p) ? 'active' : ''; ?>">
                    <a class="page-link" href="?page=<?php echo $p; ?><?php echo ($search) ? '&search=' . $search : ''; ?>"><?php echo $p; ?></a>
                </li>
            <?php endfor; ?>

            <li class="page-item <?php echo ($page == $total_pages) ? 'disabled' : ''; ?>">
                <a class="page-link" href="?page=<?php echo ($page + 1 <= $total_pages) ? $page + 1 : $total_pages; ?><?php echo ($search) ? '&search=' . $search : ''; ?>">Next</a>
            </li>
            <li class="page-item <?php echo ($page == $total_pages) ? 'disabled' : ''; ?>">
                <a class="page-link" href="?page=<?php echo $total_pages; ?><?php echo ($search) ? '&search=' . $search : ''; ?>">Last</a>
            </li>
        </ul>
    </nav>
</div>

<style>
  /* Page Background Color */
body {
    background-color: #f4f4f4; /* Light background color */
}

/* Card Styling */
.card {
    background-color: #ffffff; /* White card background */
    border-radius: 8px;
    box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.1);
    margin: 0 auto; /* Center the card */
}

/* Card Body Padding */
.card-body {
    padding: 20px;
}

/* Card Title */
h4 {
    font-family: 'Arial', sans-serif;
    color: #003366;
    font-size: 32px;
    text-align: center;
}

/* Table Styling */
.table {
    width: 100%;
    margin-top: 20px;
    table-layout: fixed; /* Ensures the table width is distributed evenly */
}

.thead-dark {
    background-color: #003366;
    color: white;
}

th, td {
    padding: 12px;
    text-align: center;
    word-wrap: break-word; /* Prevents overflow on smaller screens */
}

/* Styling for even rows */
tr:nth-child(even) {
    background-color: #f9f9f9;
}

/* Badge Styling */
.badge {
    padding: 8px 16px;
    font-size: 14px;
}

/* Success & Danger Badge */
.badge-success {
    background-color: #28a745;
}

.badge-warning {
    background-color: #ffc107;
}

/* Hover Effects */
tr:hover {
    background-color: #eaeaea;
}

/* Text Color */
.text-center {
    text-align: center;
}

.text-danger {
    font-size: 18px;
}

/* Pagination */
.pagination {
    justify-content: center;
}

.page-item {
    margin: 0 5px;
}

.page-link {
    padding: 8px 16px;
    font-size: 14px;
}

/* Responsive Design */
@media (max-width: 768px) {
    .table {
        font-size: 12px; /* Adjust font size for mobile */
        overflow-x: auto; /* Allows horizontal scrolling */
    }

    .table th, .table td {
        padding: 8px; /* Reduce padding on smaller screens */
    }

    /* Adjust pagination button size on mobile */
    .page-link {
        font-size: 12px;
        padding: 6px 12px;
    }

    /* Adjust search form on mobile */
    .input-group {
        flex-direction: column; /* Stack search input and button */
    }

    .input-group input {
        margin-bottom: 10px; /* Add margin between input and button */
        width: 100%;
    }

    /* Hide long columns on mobile (optional, depending on your use case) */
    .table td:nth-child(n+10), .table th:nth-child(n+10) {
        display: none; /* Hide columns after 'Mobile No' for better mobile display */
    }
}

</style>


</body>
</html>
