<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
if (function_exists('mj291_payment_ensure_schema')) { mj291_payment_ensure_schema(); }
$adminTitle = 'Finance Summary';
$adminSubtitle = 'Fast paid, pending, invoice, receipt and wallet tally summary.';
$currency = setting('default_currency','SGD') ?: 'SGD';
$m = function_exists('mj293_finance_metrics') ? mj293_finance_metrics() : [];
include __DIR__.'/_header.php';
?>

<div class="row">
 <?php foreach([
   ['Awaiting User Payment', money($m['awaiting_user_payment_amount'] ?? 0, $currency), (int)($m['awaiting_user_payment_count'] ?? 0), 'warning', 'payment-center.php?tab=waiting'],
   ['Pending Admin Review', money($m['pending_review_amount'] ?? 0, $currency), (int)($m['pending_review_count'] ?? 0), 'info', 'payment-center.php?tab=review'],
   ['Paid / Approved', money($m['paid_amount'] ?? 0, $currency), (int)($m['paid_count'] ?? 0), 'success', 'payment-center.php?tab=history'],
   ['Open Amount', money($m['open_amount'] ?? 0, $currency), (int)($m['open_count'] ?? 0), 'primary', 'payment-center.php?tab=waiting'],
 ] as $box): ?>
 <div class="col-md-3"><a href="<?=e($box[4])?>" class="small-box bg-<?=e($box[3])?> d-block text-white"><div class="inner"><h3><?=$box[1]?></h3><p><?=e($box[0])?> · <?=e($box[2])?> record(s)</p></div><div class="icon"><i class="fas fa-coins"></i></div></a></div>
 <?php endforeach; ?>
</div>

<div class="card"><div class="card-body">
  <h5 class="font-weight-bold">Cross-check Tally</h5>
  <div class="table-responsive"><table class="table table-bordered mb-0"><thead><tr><th>Metric</th><th class="text-right">Count</th><th class="text-right">Amount</th><th>Use</th></tr></thead><tbody>
    <tr><td>Receipt Amount</td><td class="text-right"><?=e((int)($m['receipt_count'] ?? 0))?></td><td class="text-right font-weight-bold"><?=money($m['receipt_amount'] ?? 0, $currency)?></td><td>Actual receipt records generated after approval.</td></tr>
    <tr><td>Invoice Paid Amount</td><td class="text-right"><?=e((int)($m['invoice_paid_count'] ?? 0))?></td><td class="text-right font-weight-bold"><?=money($m['invoice_paid_amount'] ?? 0, $currency)?></td><td>Invoices marked paid.</td></tr>
    <tr><td>Wallet Payment Debit</td><td class="text-right"><?=e((int)($m['wallet_payment_debit_count'] ?? 0))?></td><td class="text-right font-weight-bold"><?=money($m['wallet_payment_debit_amount'] ?? 0, $currency)?></td><td>Wallet payments deducted for invoices.</td></tr>
    <tr><td>Wallet Top-up Credit</td><td class="text-right"><?=e((int)($m['wallet_topup_credit_count'] ?? 0))?></td><td class="text-right font-weight-bold"><?=money($m['wallet_topup_credit_amount'] ?? 0, $currency)?></td><td>Approved user top-ups credited after admin review.</td></tr>
  </tbody></table></div>
  <p class="text-muted mb-0 mt-3">Use <a href="payment-center.php">Payment Center</a> for all review, approval, wallet, invoice and receipt actions. Old payment pages remain redirects only.</p>
</div></div>

<?php include __DIR__.'/_footer.php'; ?>
