<?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'];
$utype = $_SESSION['utype'] ?? $_SESSION['user_type'] ?? 'Retailer';

// Admin ko sab dikhega, Retailer ko sirf apna
$filter = ($utype == 'TitanAdmin') ? "" : "WHERE user_id = '$uid'";

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

// Total records check karne ke liye exact dynamic filter ke sath query
$count_query = "SELECT COUNT(*) AS total FROM npci_status_history $filter";
$count_res = $conn->query($count_query);
$total_records = 0;
if ($count_res && $row_count = $count_res->fetch_assoc()) {
    $total_records = $row_count['total'];
}

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

// Paginated query limit aur offset ke sath
$query = "SELECT * FROM npci_status_history $filter ORDER BY id DESC LIMIT $limit OFFSET $offset";
$result = $conn->query($query);

// Entry entries information label sets
$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-clipboard-check 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">NPCI Status <span class="text-info fw-medium">Records</span></h4>
                    </div>
                </div>
                <div class="col-md-auto">
                    <a href="npci_status.php" class="btn btn-sm btn-primary shadow-none fw-bold"><i class="fas fa-search me-1"></i> New Check</a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="card mb-3 border-0 shadow-sm">
    <div class="card-body p-0">
        <div class="table-responsive scrollbar">
            <table class="table table-sm table-striped table-hover fs-10 mb-0 text-center align-middle">
                <thead class="bg-200 text-900">
                    <tr>
                        <th class="py-2">#ID</th>
                        <th>Aadhaar No.</th>
                        <th>Name</th>
                        <th>Bank Name</th>
                        <th>Acc Status</th>
                        <th>Mobile</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">#<?php echo $row['id']; ?></td>
                                <td><span class="fw-semi-bold text-600"><?php echo htmlspecialchars($row['aadhar_no']); ?></span></td>
                                <td>
                                    <span class="fw-bold text-dark d-block"><?php echo htmlspecialchars($row['name']); ?></span>
                                    <span class="text-500" style="font-size: 11px;"><?php echo htmlspecialchars($row['local_name']); ?></span>
                                </td>
                                <td><span class="fw-bold text-primary"><?php echo htmlspecialchars($row['bank_name']); ?></span></td>
                                <td>
                                    <?php if(strtoupper($row['account_status']) == 'A'): ?>
                                        <span class="badge badge-subtle-success fs-10 px-2 py-1"><i class="fas fa-check-circle me-1"></i> ACTIVE (A)</span>
                                    <?php else: ?>
                                        <span class="badge badge-subtle-danger fs-10 px-2 py-1"><i class="fas fa-times-circle me-1"></i> <?php echo htmlspecialchars($row['account_status']); ?></span>
                                    <?php endif; ?>
                                </td>
                                <td><span class="text-700 fw-semi-bold"><i class="fas fa-phone-alt fs-11 me-1"></i> <?php echo htmlspecialchars($row['mobile']); ?></span></td>
                                <td><?php echo date('d M Y, h:i A', strtotime($row['created_at'])); ?></td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr><td colspan="7" class="py-4 text-500">No NPCI records found. Try checking a status first.</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 
require_once('../titancore/TitanFooter.php'); 
ob_end_flush();
?>