<?php
require 'includes/db_connect.php';

$limit = 10;
$page  = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;
$q     = isset($_GET['q']) ? $conn->real_escape_string($_GET['q']) : '';

// Total count
$countSql = "
    SELECT COUNT(*) AS total 
    FROM lgid 
    WHERE full_name LIKE '%$q%' 
       OR email LIKE '%$q%'
";
$countResult = $conn->query($countSql);

if (!$countResult) {
    die("SQL Error: " . $conn->error);
}

$total = $countResult->fetch_assoc()['total'];
$pages = ceil($total / $limit);

// Fetch rows
$sql = "
    SELECT * FROM lgid 
    WHERE full_name LIKE '%$q%' 
       OR email LIKE '%$q%' 
    ORDER BY reg_date DESC 
    LIMIT $start, $limit
";

$result = $conn->query($sql);

if (!$result) {
    die("SQL Error: " . $conn->error);
}

echo '<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead class="table-light">
<tr>
    <th>#</th>
    <th>Full Name</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Payment Status</th>
    <th>Applied At</th>
    <th>Actions</th>
</tr>
</thead>
<tbody>';

$sn = $start + 1;

if ($result->num_rows > 0) {

    while ($row = $result->fetch_assoc()) {

        echo '<tr>
            <td>' . $sn++ . '</td>
            <td>' . htmlspecialchars($row['full_name']) . '</td>
            <td>' . htmlspecialchars($row['email']) . '</td>
            <td>' . htmlspecialchars($row['phone']) . '</td>
            <td>';

        // Payment badge
        if ($row['payment_status'] === 'paid') {
            echo '<span class="badge bg-success">Paid</span>';
        } else {
            echo '<span class="badge bg-warning text-dark">Pending</span>';
        }

        echo '</td>
            <td>' . date("d M, Y", strtotime($row['created_at'])) . '</td>
            <td>';

        // Actions
        if ($row['payment_status'] === 'pending') {

            echo '
                <a href="pay_lgid.php?id=' . $row['id'] . '" 
                   class="btn btn-sm btn-warning mb-1">
                   Pay Now
                </a>
                <a href="confirm_payment.php?id=' . $row['id'] . '" 
                   class="btn btn-sm btn-success">
                   Confirm Payment
                </a>';

        } else {

            echo '
                <a href="certificates/' . htmlspecialchars($row['certificate_no']) . '.pdf" 
                   class="btn btn-sm btn-primary" 
                   target="_blank">
                   View Certificate
                </a>';
        }

        echo '</td></tr>';
    }

} else {
    echo '<tr>
            <td colspan="7" class="text-center text-muted">
                No applicants found.
            </td>
          </tr>';
}

echo '</tbody></table></div>';

// Pagination
if ($pages > 1) {
    echo '<nav>
            <ul class="pagination justify-content-center">';

    for ($i = 1; $i <= $pages; $i++) {
        $active = ($i == $page) ? 'active' : '';
        echo '<li class="page-item ' . $active . '">
                <a href="#" 
                   class="page-link pagination-link" 
                   data-page="' . $i . '">' . $i . '</a>
              </li>';
    }

    echo '</ul></nav>';
}
?>
