<?php
ob_start();
if (session_status() === PHP_SESSION_NONE) { session_start(); }
if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); }

require_once('../titancore/titanconfig.php');
require_once('../titancore/titanheader.php');

$uid = $_SESSION['user_id'];

// ---------------- PAGINATION LOGIC START ----------------
$limit = 15;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) { $page = 1; }
$offset = ($page - 1) * $limit;

// Updated table name here
$count_query = "SELECT COUNT(*) AS total FROM voter_link_without_otp_history WHERE user_id = ?";
$stmt_count = $conn->prepare($count_query);
$stmt_count->bind_param("s", $uid);
$stmt_count->execute();
$count_result = $stmt_count->get_result();
$total_records = 0;

if ($count_result && $row_count = $count_result->fetch_assoc()) {
    $total_records = $row_count['total'];
}
$stmt_count->close();

$total_pages = ceil($total_records / $limit);
if ($page > $total_pages && $total_pages > 0) { 
    $page = $total_pages; 
    $offset = ($page - 1) * $limit; 
}

// Updated table name here
$query = "SELECT * FROM voter_link_without_otp_history WHERE user_id = ? ORDER BY id DESC LIMIT ? OFFSET ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("sii", $uid, $limit, $offset);
$stmt->execute();
$result = $stmt->get_result();

$start_entry = $total_records == 0 ? 0 : $offset + 1;
$end_entry = min($offset + $limit, $total_records);
// ---------------- PAGINATION LOGIC END ----------------
?>

<div class="row mb-3">
    <div class="col">
        <div class="card bg-100 shadow-none border">
            <div class="row gx-0 flex-between-center p-3">
                <div class="col-sm-auto d-flex align-items-center">
                    <div class="icon-item bg-primary-subtle shadow-none me-3">
                        <span class="fas fa-list-alt text-primary fs-4"></span>
                    </div>
                    <div>
                        <h6 class="text-primary fs-10 mb-0">History Log</h6>
                        <h4 class="text-primary fw-bold mb-0">Voter Mobile Link <span class="text-info fw-medium">Without OTP List</span></h4>
                    </div>
                </div>
                <div class="col-md-auto">
                    <a href="VoterMobileLink_Without_Otp.php" class="btn btn-sm btn-primary shadow-none fw-bold"><i class="fas fa-plus me-1"></i> New Link Request</a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="card mb-3 border-0 shadow-sm border-top border-4 border-primary">
    <div class="card-body p-0">
        <div class="table-responsive scrollbar">
            <table class="table table-sm table-striped fs-10 mb-0 text-center align-middle">
                <thead class="bg-200 text-900">
                    <tr>
                        <th class="py-2">#ID</th>
                        <th>Voter ID (EPIC)</th>
                        <th>Mobile Linked</th>
                        <th>System Status</th>
                        <th>Message</th>
                        <th>Date & Time</th>
                    </tr>
                </thead>
                <tbody class="list">
                    <?php if($result && $result->num_rows > 0): ?>
                        <?php while($row = $result->fetch_assoc()): ?>
                            <tr>
                                <td class="py-2 fw-bold text-900">#<?php echo $row['id']; ?></td>
                                <td>
                                    <code class="badge badge-subtle-primary fs-11 px-3 py-2 font-monospace">
                                        <?php echo htmlspecialchars($row['epic_no']); ?>
                                    </code>
                                </td>
                                <td>
                                    <span class="fw-bold text-800">
                                        <?php echo htmlspecialchars($row['mobile_no']); ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-subtle-success fs-10 px-2 py-1">
                                        <?php echo htmlspecialchars($row['system_status']); ?>
                                    </span>
                                </td>
                                <td class="text-success fw-semi-bold">
                                    <?php echo htmlspecialchars($row['message']); ?>
                                </td>
                                <td class="text-700">
                                    <?php echo date('d M Y, h:i A', strtotime($row['created_at'])); ?>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr><td colspan="6" class="py-4 text-500 fst-italic">No linking history found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card-footer d-flex align-items-center justify-content-between py-2 border-top bg-100">
        <p class="mb-0 fs-10 text-700 fw-semi-bold">
            Showing <?php echo $start_entry; ?> to <?php echo $end_entry; ?> of <?php echo $total_records; ?> entries
        </p>
        <?php if ($total_pages > 1): ?>
        <nav aria-label="Page navigation">
            <ul class="pagination pagination-sm mb-0">
                <li class="page-item <?php echo ($page <= 1) ? 'disabled' : ''; ?>">
                    <a class="page-link shadow-none" href="?page=<?php echo $page - 1; ?>" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                
                <?php 
                $start_loop = max(1, $page - 2);
                $end_loop = min($total_pages, $page + 2);
                for ($i = $start_loop; $i <= $end_loop; $i++): 
                ?>
                    <li class="page-item <?php echo ($page == $i) ? 'active' : ''; ?>">
                        <a class="page-link shadow-none" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                    </li>
                <?php endfor; ?>

                <li class="page-item <?php echo ($page >= $total_pages) ? 'disabled' : ''; ?>">
                    <a class="page-link shadow-none" href="?page=<?php echo $page + 1; ?>" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>
        <?php endif; ?>
    </div>
</div>

<?php 
if (isset($stmt)) { $stmt->close(); }
require_once('../titancore/TitanFooter.php'); 
ob_end_flush();
?>