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

$id = intval($_GET['id'] ?? 0);
$borrower = getBorrowerById($conn, $id);
if (!$borrower) { header('Location: /cash_lending/mobile/borrowers.php'); exit(); }

$loans = fetchAll($conn,
    "SELECT * FROM loans WHERE borrower_id = ? ORDER BY created_at DESC",
    [$id], "i"
);

$page_title = $borrower['first_name'] . ' ' . $borrower['last_name'];
$active_nav = 'borrowers';

$initials  = urlencode($borrower['first_name'] . '+' . $borrower['last_name']);
$photo_src = !empty($borrower['photo']) && file_exists($borrower['photo'])
    ? '/cash_lending/' . $borrower['photo']
    : "https://ui-avatars.com/api/?name={$initials}&size=90&background=1a56db&color=fff&rounded=true";

include 'header.php';
?>

<div class="text-center mb-3">
    <img src="<?php echo htmlspecialchars($photo_src); ?>"
         style="width:90px;height:90px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" alt="">
    <div class="fw-bold mt-1" style="font-size:18px;"><?php echo htmlspecialchars($borrower['first_name'] . ' ' . $borrower['last_name']); ?></div>
    <div class="text-muted" style="font-size:13px;"><?php echo htmlspecialchars($borrower['occupation'] ?? '—'); ?></div>
</div>

<div class="card mb-3">
    <div class="card-header"><i class="fas fa-info-circle"></i> Details</div>
    <div class="card-body">
        <?php foreach ([
            ['Contact',   $borrower['contact']],
            ['Address',   $borrower['address']],
            ['ID Type',   $borrower['id_type']],
            ['ID Number', $borrower['id_number']],
            ['Since',     date('M d, Y', strtotime($borrower['created_at']))],
        ] as [$label, $value]): ?>
            <div class="d-flex justify-between mb-1" style="font-size:13px;padding:6px 0;border-bottom:1px solid var(--border);">
                <span class="text-muted"><?php echo $label; ?></span>
                <span class="fw-bold" style="text-align:right;max-width:60%;"><?php echo htmlspecialchars($value); ?></span>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<div class="d-flex gap-2 mb-3">
    <a href="/cash_lending/mobile/add-loan.php?borrower_id=<?php echo $id; ?>" class="btn btn-primary w-100">
        <i class="fas fa-plus"></i> New Loan
    </a>
    <a href="/cash_lending/mobile/edit-borrower.php?id=<?php echo $id; ?>" class="btn btn-outline w-100">
        <i class="fas fa-edit"></i> Edit
    </a>
</div>

<div class="section-title">Loans</div>
<div class="card">
    <?php if (!empty($loans)): ?>
        <?php foreach ($loans as $loan): ?>
            <?php $badge = match($loan['status']) { 'paid'=>'badge-success','overdue'=>'badge-danger','approved'=>'badge-info','released'=>'badge-primary',default=>'badge-warning' }; ?>
            <a href="/cash_lending/mobile/loan-details.php?id=<?php echo $loan['id']; ?>" class="list-row">
                <div class="row-info">
                    <div class="row-name">₱<?php echo number_format($loan['loan_amount'], 2); ?></div>
                    <div class="row-sub mt-1"><span class="badge <?php echo $badge; ?>"><?php echo ucfirst($loan['status']); ?></span> · <?php echo in_array($loan['status'],['pending','approved']) ? 'Not yet released' : date('M d, Y', strtotime($loan['release_date'])); ?></div>
                </div>
                <div class="row-right">
                    <div style="font-size:12px;color:var(--muted);">Balance</div>
                    <div class="fw-bold">₱<?php echo number_format($loan['balance'], 2); ?></div>
                </div>
                <i class="fas fa-chevron-right chevron"></i>
            </a>
        <?php endforeach; ?>
    <?php else: ?>
        <div class="empty-state"><i class="fas fa-file-contract"></i><p>No loans yet</p></div>
    <?php endif; ?>
</div>

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