<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/middleware.php';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/referrals.php';
require_once __DIR__ . '/../includes/notifications.php';
require_admin();

$id = isset($_GET['view']) ? (int)$_GET['view'] : 0;
$action = $_POST['action'] ?? '';

if ($action === 'update_status') {
    ensure_csrf();
    $aid = (int)($_POST['application_id'] ?? 0);
    $status = $_POST['status'] ?? '';
    $notes = sanitize_str($_POST['admin_notes'] ?? '');
    $stmt = $pdo->prepare('UPDATE applications SET status=?, admin_notes=?, updated_at=? WHERE id=?');
    $stmt->execute([$status,$notes,date('c'),$aid]);
    
    // Notify user of status change
    $appInfo = $pdo->prepare("SELECT user_id, status FROM applications WHERE id=?");
    $appInfo->execute([$aid]);
    $appData = $appInfo->fetch();
    if ($appData) {
        notify_inapp($pdo, (int)$appData['user_id'], 'status_update', 'Application Update', "Your application status is now: " . ucfirst($status), ['application_id' => $aid]);
    }
    
    set_flash('success','Status updated');
    redirect(url('admin/applications.php?view='.$aid));
}

if ($action === 'confirm_payment') {
    ensure_csrf();
    $pid = (int)($_POST['payment_id'] ?? 0);
    $aid = (int)($_POST['application_id'] ?? 0);
    $status = $_POST['payment_status'] ?? 'confirmed';
    $stmt = $pdo->prepare('UPDATE payments SET status=? WHERE id=?');
    $stmt->execute([$status,$pid]);
    if ($status === 'confirmed') {
        $pdo->prepare("UPDATE applications SET status='processing', updated_at=? WHERE id=?")->execute([date('c'),$aid]);
        
        // Get details for commission
        $stmt = $pdo->prepare("SELECT a.user_id, p.amount FROM applications a JOIN payments p ON p.application_id=a.id WHERE p.id=?");
        $stmt->execute([$pid]);
        $d = $stmt->fetch();
        if ($d) {
            award_commission($pdo, (int)$d['user_id'], $aid, (float)$d['amount']);
            // Notify
            notify_inapp($pdo, (int)$d['user_id'], 'payment_approved', 'Payment Approved', 'Your payment has been manually approved.', ['application_id' => $aid]);
        }
    }
    set_flash('success','Payment updated');
    redirect(url('admin/applications.php?view='.$aid));
}

if ($action === 'upload_certificate') {
    ensure_csrf();
    $aid = (int)($_POST['application_id'] ?? 0);
    $file = $_FILES['certificate'] ?? null;
    $name = $file ? save_uploaded_file($file, ['pdf'], 2*1024*1024, __DIR__ . '/../uploads/certificates') : null;
    if ($name) {
        $path = url('uploads/certificates/' . $name);
        $stmt = $pdo->prepare('INSERT OR REPLACE INTO certificates (application_id, certificate_path, uploaded_at) VALUES (?,?,?)');
        $stmt->execute([$aid,$path,date('c')]);
        $pdo->prepare("UPDATE applications SET status='approved', updated_at=? WHERE id=?")->execute([date('c'),$aid]);
        
        // Notify user
        $appInfo = $pdo->prepare("SELECT user_id FROM applications WHERE id=?");
        $appInfo->execute([$aid]);
        $appData = $appInfo->fetch();
        if ($appData) {
            notify_inapp($pdo, (int)$appData['user_id'], 'certificate_ready', 'Certificate Ready', 'Your business certificate is ready for download!', ['application_id' => $aid]);
        }
        
        set_flash('success','Certificate uploaded and application approved');
    } else {
        set_flash('error','Upload failed');
    }
    redirect(url('admin/applications.php?view='.$aid));
}

// Delete Application Handler
if ($action === 'delete_application') {
    ensure_csrf();
    $aid = (int)($_POST['application_id'] ?? 0);
    
    if ($aid > 0) {
        try {
            // Get application info for notification before deleting
            $appInfo = $pdo->prepare("SELECT user_id, status FROM applications WHERE id=?");
            $appInfo->execute([$aid]);
            $appData = $appInfo->fetch();
            
            // Delete related records first (foreign key constraints)
            $pdo->prepare("DELETE FROM application_details WHERE application_id=?")->execute([$aid]);
            $pdo->prepare("DELETE FROM payments WHERE application_id=?")->execute([$aid]);
            $pdo->prepare("DELETE FROM certificates WHERE application_id=?")->execute([$aid]);
            
            // Delete the application
            $stmt = $pdo->prepare("DELETE FROM applications WHERE id=?");
            $stmt->execute([$aid]);
            
            if ($appData) {
                notify_inapp($pdo, (int)$appData['user_id'], 'application_deleted', 'Application Deleted', 'Your application has been removed from the system.', []);
            }
            
            set_flash('success', 'Application #' . $aid . ' has been deleted successfully');
            redirect(url('admin/applications.php'));
        } catch (Exception $e) {
            set_flash('error', 'Failed to delete application: ' . $e->getMessage());
            redirect(url('admin/applications.php?view=' . $aid));
        }
    } else {
        set_flash('error', 'Invalid application ID');
        redirect(url('admin/applications.php'));
    }
}

include __DIR__ . '/../includes/header.php';
?> 
<style>
/* Layout Utilities */
.admin-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 32px; gap: 20px; flex-wrap: wrap; }
.admin-title { margin: 0; font-size: 28px; font-weight: 800; color: var(--text); letter-spacing: -0.02em; }

/* Filters */
.filter-bar { background: #fff; border: 1px solid var(--border); border-radius: 16px; padding: 16px 24px; display: flex; align-items: center; gap: 12px; margin-bottom: 32px; box-shadow: var(--shadow-sm); }
.filter-label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.1em; opacity: 0.8; }

/* Status Badges */
.status-pill { 
    padding: 6px 14px; 
    border-radius: 999px; 
    font-size: 11px; 
    font-weight: 700; 
    text-transform: uppercase; 
    letter-spacing: 0.05em;
    display: inline-flex; 
    align-items: center; 
    gap: 6px; 
}
.status-submitted { background: #e0e7ff; color: #3730a3; border: 1px solid #c7d2fe; }
.status-processing { background: #fef9c3; color: #854d0e; border: 1px solid #fef08a; }
.status-approved { background: #d1fae5; color: #065f46; border: 1px solid #bbf7d0; }
.status-rejected { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }
.status-draft { background: #f3f4f6; color: #374151; border: 1px solid #e5e7eb; }


/* Application Grid/List */
.app-card { background: #fff; border: 1px solid var(--border); border-radius: 24px; padding: 24px; transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1); position: relative; display: flex; flex-direction: column; gap: 16px; box-shadow: var(--shadow-sm); }
.app-card:hover { border-color: var(--primary); transform: translateY(-4px); box-shadow: var(--shadow); }
.app-id { font-size: 11px; font-weight: 800; color: var(--muted); background: #f1f5f9; padding: 4px 10px; border-radius: 8px; letter-spacing: 0.05em; }

/* Detail View Styles */
.detail-grid { display: grid; grid-template-columns: 1.8fr 1fr; gap: 32px; }

@media (max-width: 1024px) { .detail-grid { grid-template-columns: 1fr; } }

.section-card { background: #fff; border: 1px solid var(--border); border-radius: 24px; padding: 32px; margin-bottom: 32px; box-shadow: var(--shadow-sm); }
.section-title { font-size: 18px; font-weight: 700; color: var(--text); margin: 0 0 24px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); padding-bottom: 16px; }

.info-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 24px; }

.info-item { display: flex; flex-direction: column; gap: 4px; }
.info-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.info-value { font-size: 14px; font-weight: 600; color: var(--text); }

.action-form { background: #fcfcfd; border-radius: 20px; padding: 24px; border: 1px solid var(--border); }
.form-label { display: block; font-size: 13px; font-weight: 700; color: var(--text); margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.8; }
.form-select, .form-textarea, .form-input { width: 100%; padding: 12px 16px; border: 1px solid var(--border); border-radius: 12px; font-size: 14px; background: #fff; transition: all 0.3s ease; }
.form-select:focus, .form-textarea:focus, .form-input:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.1); }

.doc-link { display: flex; align-items: center; gap: 16px; padding: 16px; border: 1px solid var(--border); border-radius: 20px; text-decoration: none; color: var(--text); transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1); background: #fff; box-shadow: var(--shadow-sm); }
.doc-link:hover { border-color: var(--primary); transform: translateY(-4px); box-shadow: var(--shadow); }
.doc-icon { width: 44px; height: 44px; border-radius: 14px; background: rgba(16, 185, 129, 0.1); color: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 20px; }


@media (max-width: 767px) {
  .admin-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  .filter-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  .filter-bar > div {
    flex-wrap: wrap;
    justify-content: flex-start;
  }
  .detail-grid {
    grid-template-columns: 1fr;
  }
  .info-list {
    grid-template-columns: 1fr; /* Stack info items on small screens */
  }
  .doc-link {
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
  }
  .doc-link .doc-icon {
    margin-bottom: 8px;
  }
  .action-form > div { /* For payment update form's flex container */
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  .action-form .btn {
    width: 100%;
    justify-content: center;
  }
  .table-wrap { display: none; }
  .mobile-grid { display: grid; gap: 16px; grid-template-columns: 1fr; }
}
@media(min-width:768px){
  .mobile-grid { display: none; }
}
</style>

<?php
if ($id > 0) {
    $stmt = $pdo->prepare('SELECT a.*, u.full_name as user_name, u.email as user_email, s.service_name FROM applications a JOIN users u ON a.user_id=u.id JOIN services s ON a.service_id=s.id WHERE a.id=?');
    $stmt->execute([$id]);
    $app = $stmt->fetch();
    if (!$app) {
        echo '<div class="card">Not found</div>';
        include __DIR__ . '/../includes/footer.php'; exit;
    }
    $det = $pdo->prepare('SELECT * FROM application_details WHERE application_id=?'); $det->execute([$id]); $det = $det->fetch();
    $pay = $pdo->prepare('SELECT * FROM payments WHERE application_id=? ORDER BY id DESC LIMIT 1'); $pay->execute([$id]); $pay = $pay->fetch();
    $cert = $pdo->prepare('SELECT * FROM certificates WHERE application_id=?'); $cert->execute([$id]); $cert = $cert->fetch();
    ?>
    <div class="admin-header">
        <div style="display:flex; align-items:center; gap:12px">
            <a href="<?=url('admin/applications.php')?>" class="btn" style="padding:8px"><?=ui_icon('open', 18)?></a>
            <h2 class="admin-title">Application #<?=e((string)$app['id'])?></h2>
            <span class="status-pill status-<?=e($app['status'])?>"><?=e($app['status'])?></span>
        </div>
        <div style="font-size:14px; color:var(--muted)">
            Created on <?=date('M j, Y H:i', strtotime($app['created_at']))?>
        </div>
    </div>

    <div class="detail-grid">
        <div class="main-col">
            <!-- Applicant Info -->
            <div class="section-card">
                <h3 class="section-title"><?=ui_icon('users', 18)?> Applicant & Business Details</h3>
                <div class="info-list">
                    <div class="info-item"><span class="info-label">Customer</span><span class="info-value"><?=e($app['user_name'])?></span></div>
                    <div class="info-item"><span class="info-label">Email</span><span class="info-value"><?=e($app['user_email'])?></span></div>
                    <div class="info-item"><span class="info-label">Service Type</span><span class="info-value"><?=e($app['service_name'])?></span></div>
                </div>
                
                <?php if ($det): ?>
                <div style="margin-top:24px; display:grid; gap:20px">
                    <div class="info-item"><span class="info-label">Proposed Names</span>
                        <div style="display:flex; gap:8px; margin-top:4px">
                            <span class="badge"><?=e($det['proposed_name_1'] ?? 'N/A')?></span>
                            <?php if(!empty($det['proposed_name_2'])): ?><span class="badge" style="background:#f1f5f9; color:#475569"><?=e($det['proposed_name_2'])?></span><?php endif; ?>
                        </div>
                    </div>
                    <div class="info-item"><span class="info-label">Nature of Business</span><span class="info-value"><?=nl2br(e($det['nature_of_business'] ?? 'N/A'))?></span></div>
                    <div class="info-item"><span class="info-label">Business Address</span><span class="info-value"><?=nl2br(e($det['business_address'] ?? 'N/A'))?></span></div>
                    
                    <div class="info-list" style="border-top:1px solid #f1f5f9; padding-top:16px">
                        <div class="info-item"><span class="info-label">Full Legal Name</span><span class="info-value"><?=e($det['full_name'] ?? 'N/A')?></span></div>
                        <div class="info-item"><span class="info-label">Phone</span><span class="info-value"><?=e($det['phone'] ?? 'N/A')?></span></div>
                        <div class="info-item"><span class="info-label">ID Type & Number</span><span class="info-value"><?=e($det['id_type'] ?? '')?> - <?=e($det['id_number'] ?? 'N/A')?></span></div>
                    </div>
                </div>

                <div style="margin-top:24px; display:grid; grid-template-columns:1fr 1fr; gap:16px">
                    <?php if (!empty($det['id_document_path'])): ?>
                        <a href="<?=e($det['id_document_path'])?>" target="_blank" class="doc-link">
                            <div class="doc-icon"><?=ui_icon('faq', 18)?></div>
                            <div><div style="font-weight:600; font-size:13px">Identity Document</div><div style="font-size:11px; color:var(--muted)">Click to view</div></div>
                        </a>
                    <?php endif; ?>
                    <?php if (!empty($det['passport_path'])): ?>
                        <a href="<?=e($det['passport_path'])?>" target="_blank" class="doc-link">
                            <div class="doc-icon"><?=ui_icon('users', 18)?></div>
                            <div><div style="font-weight:600; font-size:13px">Passport Photo</div><div style="font-size:11px; color:var(--muted)">Click to view</div></div>
                        </a>
                    <?php endif; ?>
                </div>
                <?php else: ?>
                    <div style="text-align:center; padding:40px; color:var(--muted)">No additional details provided.</div>
                <?php endif; ?>
            </div>

            <!-- Payment Details -->
            <div class="section-card">
                <h3 class="section-title"><?=ui_icon('pricing', 18)?> Payment Information</h3>
                <?php if ($pay): ?>
                    <div class="info-list" style="margin-bottom:20px">
                        <div class="info-item"><span class="info-label">Amount Paid</span><span class="info-value" style="color:var(--primary); font-size:18px">₦<?=e(money((float)$pay['amount']))?></span></div>
                        <div class="info-item"><span class="info-label">Reference</span><span class="info-value"><?=e($pay['payment_reference'])?></span></div>
                        <div class="info-item"><span class="info-label">Payment Status</span><span class="badge" style="background:#fefce8; color:#854d0e"><?=ucfirst($pay['status'])?></span></div>
                    </div>
                    
                    <?php if (!empty($pay['receipt_path'])): ?>
                        <a href="<?=e($pay['receipt_path'])?>" target="_blank" class="doc-link" style="margin-bottom:20px">
                            <div class="doc-icon"><?=ui_icon('approved', 18)?></div>
                            <div><div style="font-weight:600; font-size:13px">Payment Receipt</div><div style="font-size:11px; color:var(--muted)">Proof of transfer</div></div>
                        </a>
                    <?php endif; ?>

                    <form method="post" class="action-form">
                        <?=csrf_field()?>
                        <input type="hidden" name="action" value="confirm_payment">
                        <input type="hidden" name="application_id" value="<?=e((string)$app['id'])?>">
                        <input type="hidden" name="payment_id" value="<?=e((string)$pay['id'])?>">
                        <div style="display:flex; gap:12px; align-items:flex-end">
                            <div style="flex:1">
                                <label class="form-label">Update Payment Status</label>
                                <select name="payment_status" class="form-select">
                                    <?php foreach (['pending','confirmed','rejected'] as $ps): ?>
                                        <option value="<?=$ps?>" <?=$ps===$pay['status']?'selected':''?>><?=ucfirst($ps)?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <button class="btn primary" type="submit">Update Payment</button>
                        </div>
                    </form>
                <?php else: ?>
                    <div style="text-align:center; padding:40px; color:var(--muted)">No payment record found for this application.</div>
                <?php endif; ?>
            </div>
        </div>

        <div class="side-col">
            <!-- Status Update -->
            <div class="section-card">
                <h3 class="section-title"><?=ui_icon('processing', 18)?> Workflow Action</h3>
                <form method="post" class="action-form" style="background:transparent; padding:0; border:none">
                    <?=csrf_field()?>
                    <input type="hidden" name="action" value="update_status">
                    <input type="hidden" name="application_id" value="<?=e((string)$app['id'])?>">
                    
                    <div style="margin-bottom:16px">
                        <label class="form-label">Application Status</label>
                        <select name="status" class="form-select">
                            <?php foreach (['submitted','processing','approved','rejected'] as $st): ?>
                                <option value="<?=$st?>" <?=$st===$app['status']?'selected':''?>><?=ucfirst($st)?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div style="margin-bottom:16px">
                        <label class="form-label">Internal Admin Notes</label>
                        <textarea name="admin_notes" class="form-textarea" rows="4" placeholder="Add notes here..."><?=e((string)($app['admin_notes'] ?? ''))?></textarea>
                    </div>
                    
                    <button class="btn primary" type="submit" style="width:100%">Update Workflow</button>
                </form>
            </div>

            <!-- Final Certificate -->
            <div class="section-card">
                <h3 class="section-title"><?=ui_icon('approved', 18)?> Final Certificate</h3>
                <?php if ($cert): ?>
                    <a href="<?=e($cert['certificate_path'])?>" target="_blank" class="doc-link" style="margin-bottom:20px; border-color:#10b981; background:#ecfdf5">
                        <div class="doc-icon" style="background:#10b981; color:#fff"><?=ui_icon('check', 18)?></div>
                        <div><div style="font-weight:600; font-size:13px; color:#065f46">Approved Certificate</div><div style="font-size:11px; color:#059669">Uploaded on <?=date('M j', strtotime($cert['uploaded_at']))?></div></div>
                    </a>
                <?php endif; ?>

                <form method="post" enctype="multipart/form-data" class="action-form">
                    <?=csrf_field()?>
                    <input type="hidden" name="action" value="upload_certificate">
                    <input type="hidden" name="application_id" value="<?=e((string)$app['id'])?>">
                    <label class="form-label">Upload PDF Certificate</label>
                    <input type="file" name="certificate" accept=".pdf" class="form-input" required style="padding:6px">
                    <p style="font-size:11px; color:var(--muted); margin:8px 0 16px">This will automatically mark the application as "Approved".</p>
                    <button class="btn" type="submit" style="width:100%; border:1px solid var(--primary); color:var(--primary)">Upload & Approve</button>
                </form>
            </div>

            <!-- Danger Zone - Delete Application -->
            <div class="section-card" style="border-color:#fecaca; background:#fef2f2">
                <h3 class="section-title" style="color:#991b1b; border-color:#fecaca"><?=ui_icon('close', 18)?> Danger Zone</h3>
                <p style="font-size:13px; color:#7f1d1d; margin-bottom:16px">This action cannot be undone. All application data, payments, and certificates will be permanently deleted.</p>
                <form method="post" class="action-form" style="background:transparent; padding:0; border:none" onsubmit="return confirm('Are you sure you want to permanently delete Application #<?=e((string)$app['id'])?>? This cannot be undone.');">
                    <?=csrf_field()?>
                    <input type="hidden" name="action" value="delete_application">
                    <input type="hidden" name="application_id" value="<?=e((string)$app['id'])?>">
                    <button class="btn" type="submit" style="width:100%; background:#dc2626; color:#fff; border:none">
                        <?=ui_icon('close', 16)?> Delete Application
                    </button>
                </form>
            </div>
        </div>
    </div>
    <?php
    include __DIR__ . '/../includes/footer.php'; exit;
}

$status = $_GET['status'] ?? '';
$query = 'SELECT a.id, a.status, a.total_price, a.created_at, u.full_name as user_name, s.service_name FROM applications a JOIN users u ON a.user_id=u.id JOIN services s ON a.service_id=s.id';
$params = [];
if (in_array($status, ['draft','submitted','processing','approved','rejected'], true)) {
    $query .= ' WHERE a.status = ?';
    $params[] = $status;
}
$query .= ' ORDER BY a.created_at DESC';
$stmt = $pdo->prepare($query); $stmt->execute($params); $rows = $stmt->fetchAll();
?>

<div class="admin-header">
    <h2 class="admin-title">Manage Applications</h2>
    <div style="display:flex; gap:12px">
        <a href="<?=url('admin/dashboard.php')?>" class="btn"><?=ui_icon('dashboard', 16)?> Dashboard</a>
    </div>
</div>

<div class="filter-bar">
    <span class="filter-label">Filter:</span>
    <div style="display:flex; gap:8px">
        <a href="<?=url('admin/applications.php')?>" class="btn <?=empty($status)?'primary':'btn-outline'?>" style="padding:6px 12px; font-size:13px">All</a>
        <?php foreach (['submitted','processing','approved','rejected'] as $st): ?>
            <a href="?status=<?=$st?>" class="btn <?=$status===$st?'primary':'btn-outline'?>" style="padding:6px 12px; font-size:13px"><?=ucfirst($st)?></a>
        <?php endforeach; ?>
    </div>
</div>

<div class="card" style="padding:0; overflow:hidden">
    <div class="table-wrap">
        <table class="striped">
            <thead>
                <tr>
                    <th style="padding-left:24px">ID</th>
                    <th>Customer</th>
                    <th>Service Type</th>
                    <th>Amount</th>
                    <th>Status</th>
                    <th>Date Created</th>
                    <th style="padding-right:24px; text-align:right">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php if(empty($rows)): ?>
                    <tr><td colspan="7" style="text-align:center; padding:60px; color:var(--muted)">No applications found matching your criteria.</td></tr>
                <?php endif; ?>
                <?php foreach ($rows as $r): $st = $r['status']; ?>
                    <tr>
                        <td style="padding-left:24px"><span class="app-id">#<?=e((string)$r['id'])?></span></td>
                        <td><div style="font-weight:600; color:var(--text)"><?=e($r['user_name'])?></div></td>
                        <td><div style="font-size:13px"><?=e($r['service_name'])?></div></td>
                        <td><div style="font-weight:700; color:var(--primary)">₦<?=e(money((float)$r['total_price']))?></div></td>
                        <td><span class="status-pill status-<?=e($st)?>"><?=e($st)?></span></td>
                        <td><div style="font-size:12px; color:var(--muted)"><?=date('M j, Y', strtotime($r['created_at']))?></div></td>
                        <td style="padding-right:24px; text-align:right">
                            <a class="btn" href="<?=url('admin/applications.php?view='.e((string)$r['id']))?>" style="padding:6px 12px; font-size:13px">
                                <?=ui_icon('open',14)?> View Details
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="mobile-grid" style="padding:16px">
        <?php foreach ($rows as $r): $st = $r['status']; ?>
            <div class="app-card">
                <div style="display:flex; justify-content:space-between; align-items:flex-start">
                    <span class="app-id">#<?=e((string)$r['id'])?></span>
                    <span class="status-pill status-<?=e($st)?>"><?=e($st)?></span>
                </div>
                <div>
                    <div style="font-weight:700; font-size:16px; margin-bottom:2px"><?=e($r['user_name'])?></div>
                    <div style="font-size:13px; color:var(--muted)"><?=e($r['service_name'])?></div>
                </div>
                <div style="display:flex; justify-content:space-between; align-items:center; margin-top:8px; border-top:1px solid #f1f5f9; padding-top:12px">
                    <div style="font-weight:700; color:var(--primary)">₦<?=e(money((float)$r['total_price']))?></div>
                    <a class="btn primary" href="<?=url('admin/applications.php?view='.e((string)$r['id']))?>" style="padding:6px 12px; font-size:12px">Manage</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
