<?php
// DB connection
$host = "localhost";
$username = "job_portal";
$password = "job_portal";
$dbname = "job_portal";

$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Search + Pagination setup
$search = isset($_GET['search']) ? trim($_GET['search']) : "";
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Count total rows
$count_sql = "SELECT COUNT(*) as total FROM memberships WHERE email LIKE ?";
$stmt = $conn->prepare($count_sql);
$like = "%$search%";
$stmt->bind_param("s", $like);
$stmt->execute();
$res = $stmt->get_result();
$total = $res->fetch_assoc()['total'];
$total_pages = ceil($total / $limit);
$stmt->close();

// Fetch data
$sql = "SELECT * FROM memberships WHERE email LIKE ? ORDER BY id DESC LIMIT ?, ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sii", $like, $offset, $limit);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html>
<head>
    <title>All Memberships</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        table th, table td {
            font-size: 12px;
            vertical-align: top;
        }
    </style>
</head>
<body class="bg-light">
<div class="container-fluid p-4">
    <h3 class="text-center mb-4">All Membership Records</h3>

    <form class="mb-3 d-flex" method="get">
        <input type="text" name="search" class="form-control me-2" placeholder="Search by Email" value="<?= htmlspecialchars($search) ?>">
        <button class="btn btn-primary">Search</button>
       
    </form>

      
    <div class="table-responsive">
        <table class="table table-bordered table-hover table-sm">
            <thead class="table-primary">
                <tr>
                    <th>ID</th>
                    <th>Entry Date</th>
                    <th>Return Date</th>
                    <th>Applicant Name</th>
                    <th>Title</th>
                    <th>Address</th>
                    <th>Country</th>
                    <th>Post Code</th>
                    <th>Phone (Home)</th>
                    <th>Phone (Work)</th>
                    <th>Mobile No</th>
                    <th>Fax</th>
                    <th>Email</th>
                    <th>Unit Type</th>
                    <th>Season</th>
                    <th>No. Weeks</th>
                    <th>Occupancy Years</th>
                    <th>Purchase Price</th>
                    <th>Membership Type</th>
                    <th>Tenure</th>
                    <th>Deposit</th>
                    <th>Payment Mode</th>
                    <th>Balance</th>
                    <th>Hotel Place</th>
                    <th>Room No</th>
                    <th>Remarks</th>
                    <th>Quota Applied</th>
                    <th>Client Approval</th>
                    <th>Manager Approval</th>
                    <th>Consultant Name</th>
                    <th>Manager Name</th>
					<th>Password</th>
   
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php if ($result->num_rows > 0): ?>
                <?php while ($row = $result->fetch_assoc()): ?>
                    <tr>
                        <td><?= $row['id'] ?></td>
                        <td><?= $row['entry_date'] ?></td>
                        <td><?= $row['member_return_date'] ?></td>
                        <td><?= htmlspecialchars($row['applicant_name']) ?></td>
                        <td><?= $row['applicant_title'] ?></td>
                        <td><?= nl2br(htmlspecialchars($row['address'])) ?></td>
                        <td><?= $row['country'] ?></td>
                        <td><?= $row['post_code'] ?></td>
                        <td><?= $row['phone_home'] ?></td>
                        <td><?= $row['phone_work'] ?></td>
                        <td><?= $row['mobile_no'] ?></td>
                        <td><?= $row['fax'] ?></td>
                        <td><?= $row['email'] ?></td>
                        <td><?= $row['unit_type'] ?></td>
                        <td><?= $row['season'] ?></td>
                        <td><?= $row['no_weeks'] ?></td>
                        <td><?= $row['occupancy_years'] ?></td>
                        <td><?= $row['purchase_price'] ?></td>
                        <td><?= $row['membership_type'] ?></td>
                        <td><?= $row['tenure'] ?></td>
                        <td><?= $row['deposit'] ?></td>
                        <td><?= $row['payment_mode'] ?></td>
                        <td><?= $row['balance'] ?></td>
                        <td><?= $row['hotel_place'] ?></td>
                        <td><?= $row['room_no'] ?></td>
                        <td><?= nl2br(htmlspecialchars($row['remarks'])) ?></td>
                        <td><?= $row['quota_applied'] ? 'Yes' : 'No' ?></td>
                        <td><?= $row['client_approval'] ? 'Yes' : 'No' ?></td>
                        <td><?= $row['manager_approval'] ? 'Yes' : 'No' ?></td>
                        <td><?= $row['consultant_name'] ?></td>
                        <td><?= $row['manager_name'] ?></td>
						  <td><?= htmlspecialchars($row['password']) ?></td>
                        <td>
                            <a href="edit_membership.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                            <a href="delete_membership.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-danger" onClick="return confirm('Are you sure to delete?')">Delete</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="33" class="text-center">No records found.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    <nav>
        <ul class="pagination justify-content-center">
            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                    <a class="page-link" href="?search=<?= urlencode($search) ?>&page=<?= $i ?>"><?= $i ?></a>
                </li>  <p> <a href="logout.php" class="btn btn-danger me-2"> Logout</a>
        <button onClick="window.print()" class="btn btn-primary"> Print</button></p>
    
            <?php endfor; ?>
        </ul>
    </nav>
</div>
</body>
</html>

<?php
$stmt->close();
$conn->close();
?>
