<?php
require_once __DIR__.'/../includes/functions.php';
require_once __DIR__.'/../includes/v292_admin_portal_helpers.php';
$admin=require_admin(); mj292_ensure_schema();
$adminTitle='Revenue Report'; $adminSubtitle='Revenue grouped by listing renewal, sponsored, featured, slots, admin requests and wallet funding.';
$rows=[];
try {
  if (table_exists('mj_invoice_items')) {
    $where='1=1'; $params=[];
    if (!empty($_GET['from'])) { $where.=' AND DATE(created_at)>=?'; $params[]=$_GET['from']; }
    if (!empty($_GET['to'])) { $where.=' AND DATE(created_at)<=?'; $params[]=$_GET['to']; }
    $rows=all_rows("SELECT COALESCE(NULLIF(item_type,''),'invoice_item') revenue_type, COALESCE(currency_code,'SGD') currency_code, COUNT(*) item_count, SUM(COALESCE(total_amount,0)) total_amount FROM mj_invoice_items WHERE {$where} GROUP BY revenue_type,currency_code ORDER BY total_amount DESC", $params);
  }
} catch(Throwable $e) { $rows=[]; }
$walletRows=[];
try {
  if (table_exists('mj_wallet_transactions')) {
    $walletRows=all_rows("SELECT transaction_type revenue_type, currency_code, direction, COUNT(*) item_count, SUM(amount) total_amount FROM mj_wallet_transactions GROUP BY transaction_type,currency_code,direction ORDER BY total_amount DESC LIMIT 50");
  }
} catch(Throwable $e) {}
include __DIR__.'/_header.php';
?>
<div class="card card-primary card-outline mb-3"><div class="card-body"><form method="get" class="row align-items-end"><div class="col-md-3"><label>From</label><input class="form-control" type="date" name="from" value="<?=e($_GET['from']??'')?>"></div><div class="col-md-3"><label>To</label><input class="form-control" type="date" name="to" value="<?=e($_GET['to']??'')?>"></div><div class="col-md-6"><button class="btn btn-primary">Filter</button> <button type="button" onclick="window.print()" class="btn btn-outline-info">Print</button></div></form></div></div>
<div class="row"><div class="col-lg-7"><div class="card mj-v292-card"><div class="card-header"><h3 class="card-title">Service Revenue by Invoice Item</h3></div><div class="table-responsive"><table class="table table-hover mb-0"><thead><tr><th>Revenue Type</th><th>Currency</th><th class="text-right">Items</th><th class="text-right">Amount</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td><?=e(ucwords(str_replace('_',' ', $r['revenue_type'])))?></td><td><?=e($r['currency_code'])?></td><td class="text-right"><?=e($r['item_count'])?></td><td class="text-right font-weight-bold"><?=mj292_money0($r['total_amount'],$r['currency_code'])?></td></tr><?php endforeach; if(!$rows): ?><tr><td colspan="4" class="text-center text-muted p-4">No revenue records found.</td></tr><?php endif; ?></tbody></table></div></div></div><div class="col-lg-5"><div class="card mj-v292-card"><div class="card-header"><h3 class="card-title">Wallet Ledger Separation</h3></div><div class="table-responsive"><table class="table table-sm mb-0"><thead><tr><th>Type</th><th>Direction</th><th>Amount</th></tr></thead><tbody><?php foreach($walletRows as $w): ?><tr><td><?=e(ucwords(str_replace('_',' ',$w['revenue_type'])))?></td><td><?=e($w['direction'])?></td><td><?=mj292_money0($w['total_amount'],$w['currency_code'])?></td></tr><?php endforeach; if(!$walletRows): ?><tr><td colspan="3" class="text-center text-muted p-4">No wallet ledger records.</td></tr><?php endif; ?></tbody></table></div><div class="card-footer text-muted small">Wallet top-up is funding. Wallet invoice debit is service payment. They are shown separately to avoid double-counting.</div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
