<?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']??0); $listing=$id>0 && table_exists('mj_listings')?one('SELECT * FROM mj_listings WHERE id=? LIMIT 1',[$id]):null;
$user=null; $company=null; $events=[]; $items=[];
if($listing){ try{ if(!empty($listing['user_id']) && table_exists('mj_users')) $user=one('SELECT * FROM mj_users WHERE id=? LIMIT 1',[(int)$listing['user_id']]); }catch(Throwable $e){} try{ if(!empty($listing['company_id']) && table_exists('mj_companies')) $company=one('SELECT * FROM mj_companies WHERE id=? LIMIT 1',[(int)$listing['company_id']]); }catch(Throwable $e){} try{ if(table_exists('mj_lead_events')) $events=all_rows('SELECT * FROM mj_lead_events WHERE listing_id=? ORDER BY id DESC LIMIT 20',[$id]); }catch(Throwable $e){} try{ if(table_exists('mj_payment_batch_items')) $items=all_rows('SELECT * FROM mj_payment_batch_items WHERE listing_id=? ORDER BY id DESC LIMIT 20',[$id]); }catch(Throwable $e){} }
$adminTitle='Listing 360'; $adminSubtitle='Listing details, owner, company, paid upgrades, activity and lead events.';
include __DIR__.'/_header.php';
?>
<?php if(!$listing): ?><div class="alert alert-warning">Listing not found.</div><?php else: ?>
<div class="card mj-v292-card mb-3"><div class="card-body d-flex justify-content-between flex-wrap"><div><h3 class="font-weight-bold"><?=e($listing['title']??('Listing #'.$id))?></h3><p class="text-muted mb-0">Owner: <?=e($user['name']??'Unknown')?> <?=!empty($company['name'])?' · Company: '.e($company['name']):''?></p></div><div><?=mj292_status_badge((string)($listing['status']??''))?> <a class="btn btn-outline-primary ml-2" href="listing-edit.php?id=<?=e($id)?>">Edit</a></div></div></div>
<?php mj292_quick_cards([
 ['Views','Total listing views','report-leads.php?q='.$id,(int)($listing['views']??0),'primary','fas fa-eye'],['Sponsored','Paid sponsor status','#',(int)($listing['is_sponsored']??0)?'Yes':'No','warning','fas fa-bullhorn'],['Featured','Featured status','#',(int)($listing['is_featured']??0)?'Yes':'No','success','fas fa-star'],['Paid Items','Activation records','payment-center.php?tab=history',count($items),'info','fas fa-receipt']
]); ?>
<div class="row"><div class="col-lg-6"><div class="card"><div class="card-header"><h3 class="card-title">Paid Upgrade / Activation History</h3></div><div class="table-responsive"><table class="table mb-0"><tbody><?php foreach($items as $it): ?><tr><td><?=e(ucwords(str_replace('_',' ',$it['item_type']??'')))?><br><small><?=e($it['item_label']??'')?></small></td><td><?=mj292_status_badge((string)($it['activation_status']??$it['status']??''))?></td></tr><?php endforeach; if(!$items): ?><tr><td class="text-muted p-3">No paid item linked.</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">Lead Events</h3></div><div class="table-responsive"><table class="table mb-0"><tbody><?php foreach($events as $ev): ?><tr><td><?=e($ev['event_type']??'event')?><br><small><?=e($ev['created_at']??'')?></small></td><td><?=e($ev['ip_address']??'')?></td></tr><?php endforeach; if(!$events): ?><tr><td class="text-muted p-3">No lead events yet.</td></tr><?php endif; ?></tbody></table></div></div></div></div>
<?php endif; include __DIR__.'/_footer.php'; ?>
