<?php
require_once __DIR__.'/../includes/functions.php';
require_once __DIR__.'/../includes/v292_admin_portal_helpers.php';
$admin=require_admin(); mj292_ensure_schema();
$id=(int)($_GET['id'] ?? $_GET['user_id'] ?? 0);
if($id<=0 && !empty($_GET['q']) && table_exists('mj_users')){ $q='%'.mb_strtolower((string)$_GET['q'],'UTF-8').'%'; try{$u=one('SELECT * FROM mj_users WHERE LOWER(email) LIKE ? OR LOWER(name) LIKE ? OR mobile LIKE ? ORDER BY id DESC LIMIT 1',[$q,$q,$q]); $id=(int)($u['id']??0);}catch(Throwable $e){} }
$user=$id>0 && table_exists('mj_users') ? one('SELECT * FROM mj_users WHERE id=? LIMIT 1',[$id]) : null;
$adminTitle='User 360'; $adminSubtitle='Profile, listings, companies, invoices, receipts, wallet and admin notes in one place.';
$listings=$companies=$invoices=$receipts=$walletTx=[];
if($user){ try{$listings=table_exists('mj_listings')?all_rows('SELECT * FROM mj_listings WHERE user_id=? ORDER BY id DESC LIMIT 10',[$id]):[];}catch(Throwable $e){} try{$companies=table_exists('mj_companies')?all_rows('SELECT * FROM mj_companies WHERE user_id=? ORDER BY id DESC LIMIT 10',[$id]):[];}catch(Throwable $e){} try{$invoices=table_exists('mj_invoices')?all_rows('SELECT * FROM mj_invoices WHERE user_id=? ORDER BY id DESC LIMIT 10',[$id]):[];}catch(Throwable $e){} try{$receipts=table_exists('mj_receipts')?all_rows('SELECT * FROM mj_receipts WHERE user_id=? ORDER BY id DESC LIMIT 10',[$id]):[];}catch(Throwable $e){} try{$walletTx=table_exists('mj_wallet_transactions')?all_rows('SELECT * FROM mj_wallet_transactions WHERE user_id=? ORDER BY id DESC LIMIT 10',[$id]):[];}catch(Throwable $e){} }
include __DIR__.'/_header.php';
?>
<form class="card card-body mb-3" method="get"><div class="row"><div class="col-md-8"><input class="form-control" name="q" placeholder="Search user by name, email, mobile or ID" value="<?=e($_GET['q']??'')?>"></div><div class="col-md-4"><button class="btn btn-primary">Search User</button></div></div></form>
<?php if(!$user): ?><div class="alert alert-warning">Select a user from Users page or search above.</div><?php else: ?>
<div class="card mj-v292-card mb-3"><div class="card-body d-flex flex-wrap justify-content-between"><div><h3 class="font-weight-bold mb-1"><?=e($user['name']??('User #'.$id))?></h3><p class="text-muted mb-0"><?=e($user['email']??'')?> <?=!empty($user['mobile'])?' · '.e($user['mobile']):''?></p></div><div><a class="btn btn-outline-primary" href="users.php?search=<?=e($user['email']??$id)?>">Edit / Manage</a> <a class="btn btn-outline-success" href="payment-center.php?tab=wallet&user_id=<?=e($id)?>">Wallet</a></div></div></div>
<?php mj292_quick_cards([
 ['Listings','Active and expired','report-listings.php?q='.$id,count($listings),'success','fas fa-tags'],['Companies','Company profiles','report-companies.php?q='.$id,count($companies),'info','fas fa-building'],['Invoices','Billing records','report-invoices.php?q='.$id,count($invoices),'primary','fas fa-file-invoice'],['Receipts','Paid receipts','report-receipts.php?q='.$id,count($receipts),'success','fas fa-receipt']
]); ?>
<div class="row"><div class="col-lg-6"><div class="card"><div class="card-header"><h3 class="card-title">Latest Invoices</h3></div><div class="table-responsive"><table class="table mb-0"><tbody><?php foreach($invoices as $i): ?><tr><td><b><?=e($i['invoice_no']??('INV-'.$i['id']))?></b><br><?=mj292_status_badge((string)($i['status']??''))?></td><td class="text-right"><?=mj292_money0($i['total_amount']??0,$i['currency_code']??'SGD')?></td><td><a class="btn btn-xs btn-outline-primary" target="_blank" href="../invoice-print.php?invoice_id=<?=e($i['id'])?>">Print</a></td></tr><?php endforeach; if(!$invoices): ?><tr><td class="text-muted p-3">No invoices.</td></tr><?php endif; ?></tbody></table></div></div></div><div class="col-lg-6"><div class="card"><div class="card-header"><h3 class="card-title">Wallet Ledger</h3></div><div class="table-responsive"><table class="table mb-0"><tbody><?php foreach($walletTx as $w): ?><tr><td><?=e($w['created_at']??'')?><br><small><?=e($w['transaction_type']??'')?></small></td><td class="text-right"><?=e($w['direction']==='debit'?'-':'+')?><?=mj292_money0($w['amount']??0,$w['currency_code']??'SGD')?></td></tr><?php endforeach; if(!$walletTx): ?><tr><td class="text-muted p-3">No wallet movement.</td></tr><?php endif; ?></tbody></table></div></div></div></div>
<div class="card"><div class="card-header"><h3 class="card-title">Listings / Companies</h3></div><div class="card-body"><div class="row"><div class="col-md-6"><h5>Listings</h5><?php foreach($listings as $l): ?><a class="d-block mb-2" href="listing-view.php?id=<?=e($l['id'])?>"><?=e($l['title']??('Listing #'.$l['id']))?> <?=mj292_status_badge((string)($l['status']??''))?></a><?php endforeach; if(!$listings): ?><p class="text-muted">No listings.</p><?php endif; ?></div><div class="col-md-6"><h5>Companies</h5><?php foreach($companies as $c): ?><a class="d-block mb-2" href="company-edit.php?id=<?=e($c['id'])?>"><?=e($c['name']??('Company #'.$c['id']))?> <?=mj292_status_badge((string)($c['status']??''))?></a><?php endforeach; if(!$companies): ?><p class="text-muted">No companies.</p><?php endif; ?></div></div></div></div>
<?php endif; include __DIR__.'/_footer.php'; ?>
