<?php
require_once '../config/db.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';
requireLogin();

$page_title = 'Borrowers';
$active_nav = 'borrowers';

$search   = trim($_GET['search'] ?? '');
$page     = max(1, intval($_GET['page'] ?? 1));
$per_page = 20;
$result   = getPaginatedBorrowers($conn, $search, $page, $per_page);
$borrowers = $result['data'];
$total_pages = $result['pages'];
$total_count = $result['total'];

include 'header.php';
?>

<!-- Search bar -->
<div class="mb-2" style="position:relative;">
    <input type="text" class="form-control" id="searchInput"
           placeholder="Search borrowers…"
           value="<?php echo htmlspecialchars($search); ?>"
           style="padding-left:38px;"
           oninput="liveSearch(this.value)">
    <i class="fas fa-search" style="position:absolute;left:13px;top:50%;transform:translateY(-50%);color:var(--muted);font-size:14px;pointer-events:none;"></i>
</div>

<div style="font-size:12px;color:var(--muted);margin-bottom:8px;"><?php echo number_format($total_count); ?> borrower(s) found</div>
<a href="/cash_lending/mobile/add-borrower.php" class="btn btn-primary btn-block mb-3">
    <i class="fas fa-user-plus"></i> Add New Borrower
</a>

<div class="card" id="borrowerList">
    <?php if (!empty($borrowers)): ?>
        <?php foreach ($borrowers as $b): ?>
            <?php
            $initials  = urlencode($b['first_name'] . '+' . $b['last_name']);
            $photo_src = !empty($b['photo']) && file_exists($b['photo'])
                ? '/cash_lending/' . $b['photo']
                : "https://ui-avatars.com/api/?name={$initials}&size=44&background=1a56db&color=fff&rounded=true";
            ?>
            <a href="/cash_lending/mobile/borrower-details.php?id=<?php echo $b['id']; ?>" class="list-row">
                <img src="<?php echo htmlspecialchars($photo_src); ?>" class="row-avatar" alt="">
                <div class="row-info">
                    <div class="row-name"><?php echo htmlspecialchars($b['first_name'] . ' ' . $b['last_name']); ?></div>
                    <div class="row-sub"><?php echo htmlspecialchars($b['contact']); ?> · <?php echo htmlspecialchars($b['id_type']); ?></div>
                </div>
                <i class="fas fa-chevron-right chevron"></i>
            </a>
        <?php endforeach; ?>
    <?php else: ?>
        <div class="empty-state"><i class="fas fa-users"></i><p>No borrowers found</p></div>
    <?php endif; ?>
</div>

<script>
function liveSearch(q) {
    if (q.length === 0 || q.length >= 2) {
        window.location.href = '/cash_lending/mobile/borrowers.php' + (q ? '?search=' + encodeURIComponent(q) + '&page=1' : '');
    }
}
</script>

<?php if ($total_pages > 1): ?>
<div style="display:flex;justify-content:space-between;align-items:center;padding:12px 0;gap:8px;">
    <?php if ($page > 1): ?>
        <a href="?search=<?php echo urlencode($search); ?>&page=<?php echo $page-1; ?>"
           style="flex:1;text-align:center;padding:10px;border:1.5px solid var(--border);border-radius:10px;
                  font-size:13px;font-weight:700;color:var(--text);text-decoration:none;">
            ← Prev
        </a>
    <?php else: ?>
        <div style="flex:1;"></div>
    <?php endif; ?>
    <span style="font-size:12px;color:var(--muted);white-space:nowrap;">
        Page <?php echo $page; ?> of <?php echo $total_pages; ?>
    </span>
    <?php if ($page < $total_pages): ?>
        <a href="?search=<?php echo urlencode($search); ?>&page=<?php echo $page+1; ?>"
           style="flex:1;text-align:center;padding:10px;border:1.5px solid var(--border);border-radius:10px;
                  font-size:13px;font-weight:700;color:var(--text);text-decoration:none;">
            Next →
        </a>
    <?php else: ?>
        <div style="flex:1;"></div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php include 'footer.php'; ?>
