<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
if (function_exists('mj_v290_ensure_schema')) { mj_v290_ensure_schema(); }
if (function_exists('mj291_payment_ensure_schema')) { mj291_payment_ensure_schema(); }
$adminTitle = 'Finance Dashboard';
$adminSubtitle = 'Fast billing overview with correct paid, pending and awaiting-payment totals.';
$m = function_exists('mj293_finance_metrics') ? mj293_finance_metrics() : (function_exists('mj_v290_finance_metrics') ? mj_v290_finance_metrics() : []);
$proofs = [];
try {
    if (table_exists('mj_payment_batches')) {
        $proofs = all_rows('SELECT b.*, u.name user_name, u.email user_email, i.invoice_no FROM mj_payment_batches b LEFT JOIN mj_users u ON u.id=b.user_id LEFT JOIN mj_invoices i ON i.id=b.invoice_id ORDER BY b.id DESC LIMIT 15');
    }
} catch (Throwable $e) { $proofs = []; }
$invoices = [];
try { $invoices = table_exists('mj_invoices') ? all_rows('SELECT invoice_no,status,total_amount,currency_code,created_at FROM mj_invoices ORDER BY id DESC LIMIT 12') : []; } catch (Throwable $e) { $invoices = []; }
include __DIR__.'/_header.php';
?>
<div class="row mj-admin-stat-row">
<?php foreach([
 ['Awaiting User Payment',money($m['awaiting_user_payment_amount']??0,'SGD'),(int)($m['awaiting_user_payment_count']??0).' invoice(s)','secondary','payment-center.php?tab=waiting'],
 ['Pending Admin Review',money($m['pending_review_amount']??0,'SGD'),(int)($m['pending_review_count']??0).' proof(s)','warning','payment-center.php?tab=review'],
 ['Paid / Approved',money($m['paid_amount']??0,'SGD'),(int)($m['paid_count']??0).' paid record(s)','success','payment-center.php?tab=history'],
 ['Need More Info',money($m['need_more_info_amount']??0,'SGD'),(int)($m['need_more_info_count']??0).' record(s)','info','payment-center.php?tab=review'],
 ['Rejected',money($m['rejected_amount']??0,'SGD'),(int)($m['rejected_count']??0).' rejected','danger','payment-center.php?tab=history'],
 ['Open Amount',money($m['open_amount']??0,'SGD'),(int)($m['open_count']??0).' open item(s)','primary','payment-center.php?tab=waiting'],
 ['Receipt Amount',money($m['receipt_amount']??0,'SGD'),(int)($m['receipt_count']??0).' receipt(s)','success','payment-center.php?tab=history'],
 ['Invoice Paid Amount',money($m['invoice_paid_amount']??0,'SGD'),(int)($m['invoice_paid_count']??0).' paid invoice(s)','info','payment-center.php?tab=invoices'],
 ['Wallet Debit',money($m['wallet_payment_debit_amount']??0,'SGD'),(int)($m['wallet_payment_debit_count']??0).' wallet payment(s)','dark','payment-center.php?tab=wallet'],
 ['Wallet Top-up Credit',money($m['wallet_topup_credit_amount']??0,'SGD'),(int)($m['wallet_topup_credit_count']??0).' top-up(s)','primary','payment-center.php?tab=wallet'],
 ['Payment Methods','Open','Admin settings','secondary','upgrade-payment-settings.php']
] as $s): ?><div class="col-xl-3 col-md-6 mb-3"><a class="mj-stat-card mj-stat-<?=e($s[3])?>" href="<?=e($s[4])?>"><div><span><?=e($s[0])?></span><strong><?=$s[1]?></strong><small><?=e($s[2])?></small></div><i class="fas fa-coins"></i></a></div><?php endforeach; ?>
</div>
<div class="alert alert-info"><b>Finance tally rule:</b> bulk checkout totals use <b>payment batches</b> as the source of truth, receipts show final collected amount, and wallet payment debits are tracked separately so totals do not double-count.</div>
<div class="row"><div class="col-xl-7"><div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title">Latest Invoice / Payment Batches</h3><a href="payment-center.php?tab=review" class="btn btn-sm btn-primary float-right">Review All</a></div><div class="card-body table-responsive"><table class="table table-hover"><thead><tr><th>Batch</th><th>User</th><th>Invoice</th><th>Amount</th><th>Status</th></tr></thead><tbody><?php foreach($proofs as $p): ?><tr><td>#<?=e($p['batch_no']??$p['id'])?></td><td><?=e($p['user_name']??'User')?><br><small><?=e($p['user_email']??'')?></small></td><td><?=e($p['invoice_no']??('-'))?></td><td><?=money($p['total_amount']??0,$p['currency_code']??'SGD')?></td><td><?=function_exists('mj291_status_badge')?mj291_status_badge((string)($p['status']??'')):mj_v289_render_status_badge($p['status']??'')?></td></tr><?php endforeach; ?><?php if(!$proofs): ?><tr><td colspan="5" class="text-center text-muted p-4">No payment batches found.</td></tr><?php endif; ?></tbody></table></div></div></div><div class="col-xl-5"><div class="card"><div class="card-header"><h3 class="card-title">Recent Invoices</h3></div><div class="card-body table-responsive"><table class="table"><thead><tr><th>No</th><th>Status</th><th>Total</th></tr></thead><tbody><?php foreach($invoices as $i): ?><tr><td><?=e($i['invoice_no']??'Invoice')?><br><small><?=e($i['created_at']??'')?></small></td><td><?=function_exists('mj291_status_badge')?mj291_status_badge((string)($i['status']??'')):mj_v289_render_status_badge($i['status']??'')?></td><td><?=money($i['total_amount']??0,$i['currency_code']??'SGD')?></td></tr><?php endforeach; ?><?php if(!$invoices): ?><tr><td colspan="3" class="text-center text-muted p-4">No invoices found.</td></tr><?php endif; ?></tbody></table></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
