<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'Approval Center';
$adminSubtitle = 'One admin queue for pending listings, companies, payments, verification, renewals, slots, bookings and support.';
if (function_exists('mj_v289_ensure_schema')) { mj_v289_ensure_schema(); }
if (function_exists('mj291_payment_ensure_schema')) { mj291_payment_ensure_schema(); }
if (function_exists('mj_v288_save_admin_note_from_post')) { mj_v288_save_admin_note_from_post((int)$admin['id']); }
$cards = function_exists('mj_v289_approval_cards') ? mj_v289_approval_cards() : [];
$queue = [];
$add = function($type,$title,$status,$date,$url,$icon='fas fa-circle',$id=0) use (&$queue){ $queue[] = compact('type','title','status','date','url','icon','id'); };
try { if(table_exists('mj_listings')) foreach(all_rows("SELECT id,title,status,created_at FROM mj_listings WHERE status IN ('pending','submitted','pending_review','review') ORDER BY id DESC LIMIT 12") as $r) $add('Listing',$r['title']??('Listing #'.$r['id']),$r['status']??'', $r['created_at']??'', 'listing-edit.php?id='.(int)$r['id'],'fas fa-tags',(int)$r['id']); } catch(Throwable $e) {}
try { if(table_exists('mj_companies')) foreach(all_rows("SELECT id,name,status,created_at FROM mj_companies WHERE status IN ('pending','submitted','pending_review','review') ORDER BY id DESC LIMIT 12") as $r) $add('Company',$r['name']??('Company #'.$r['id']),$r['status']??'', $r['created_at']??'', 'company-edit.php?id='.(int)$r['id'],'fas fa-building',(int)$r['id']); } catch(Throwable $e) {}
try { if(table_exists('mj_payment_batches')) foreach(all_rows("SELECT id,batch_no,status,total_amount,currency_code,created_at FROM mj_payment_batches WHERE status IN ('pending_review','submitted','uploaded','need_more_info') ORDER BY id DESC LIMIT 12") as $r) $add('Payment Checkout',($r['batch_no'] ?: ('PAY-'.$r['id'])).' '.money($r['total_amount']??0,$r['currency_code']??'SGD'),$r['status']??'', $r['created_at']??'', 'payment-center.php?tab=review','fas fa-file-invoice-dollar',(int)$r['id']); } catch(Throwable $e) {}
try { if(table_exists('mj_upgrade_payment_proofs')) foreach(all_rows("SELECT id,status,amount,currency_code,created_at FROM mj_upgrade_payment_proofs WHERE status IS NULL OR status='' OR status IN ('pending','submitted','pending_review','review') ORDER BY id DESC LIMIT 12") as $r) $add('Payment Proof','Payment proof #'.$r['id'].' '.money($r['amount']??0,$r['currency_code']??'SGD'),$r['status']??'', $r['created_at']??'', 'payment-center.php?tab=review','fas fa-receipt',(int)$r['id']); } catch(Throwable $e) {}
try { if(table_exists('mj_verification_requests')) foreach(all_rows("SELECT id,verification_type,status,created_at FROM mj_verification_requests WHERE status IN ('pending','submitted','pending_review','review') ORDER BY id DESC LIMIT 12") as $r) $add('Verification',ucwords(str_replace('_',' ',$r['verification_type']??'Verification')).' #'.$r['id'],$r['status']??'', $r['created_at']??'', 'approval-center.php','fas fa-shield-alt',(int)$r['id']); } catch(Throwable $e) {}
try { if(table_exists('mj_support_tickets')) foreach(all_rows("SELECT id,subject,status,created_at FROM mj_support_tickets WHERE status IN ('open','pending','new') OR status IS NULL ORDER BY id DESC LIMIT 12") as $r) $add('Support',$r['subject']??('Ticket #'.$r['id']),$r['status']??'', $r['created_at']??'', 'support.php','fas fa-headset',(int)$r['id']); } catch(Throwable $e) {}
try { if(table_exists('mj_appointments')) foreach(all_rows("SELECT id,status,appointment_datetime,created_at FROM mj_appointments WHERE status IN ('pending','submitted','pending_review') ORDER BY id DESC LIMIT 12") as $r) $add('Booking','Appointment #'.$r['id'],$r['status']??'', $r['appointment_datetime'] ?: ($r['created_at']??''), 'bookings.php','fas fa-calendar-check',(int)$r['id']); } catch(Throwable $e) {}
usort($queue, fn($a,$b)=>strcmp((string)$b['date'],(string)$a['date']));
$queue = array_slice($queue,0,40);
include __DIR__.'/_header.php';
?>

<div class="row mb-3">
  <?php foreach($cards as $c): ?>
  <div class="col-xl-2 col-md-4 col-6 mb-3"><a href="<?= e($c['url']) ?>" class="mj-approval-card h-100"><span class="mj-approval-icon mj-tone-<?= e($c['tone']) ?>"><i class="<?= e($c['icon']) ?>"></i></span><b><?= e(number_format((int)$c['count'])) ?></b><small><?= e($c['label']) ?></small></a></div>
  <?php endforeach; ?>
</div>

<div class="card card-primary card-outline">
  <div class="card-header d-flex align-items-center"><h3 class="card-title"><i class="fas fa-list-check mr-2"></i>Unified Pending Queue</h3><div class="ml-auto"><a href="dashboard.php" class="btn btn-sm btn-outline-secondary">Dashboard</a></div></div>
  <div class="card-body p-0 table-responsive">
    <table class="table table-hover mb-0">
      <thead><tr><th>Type</th><th>Item</th><th>Status</th><th>Date</th><th class="text-right">Action</th></tr></thead>
      <tbody>
        <?php foreach($queue as $r): ?>
        <tr>
          <td><i class="<?= e($r['icon']) ?> text-primary mr-1"></i><?= e($r['type']) ?></td>
          <td><b><?= e(mb_strimwidth($r['title'],0,80,'...')) ?></b><?php if(function_exists('mj_v288_safe_admin_note_form') && !empty($r['id'])): ?><?= mj_v288_safe_admin_note_form(strtolower($r['type']),(int)$r['id']) ?><?php endif; ?></td>
          <td><?= mj_v289_render_status_badge($r['status']) ?></td>
          <td><?= e($r['date']) ?></td>
          <td class="text-right"><a href="<?= e($r['url']) ?>" class="btn btn-sm btn-primary"><i class="fas fa-eye mr-1"></i>Review</a></td>
        </tr>
        <?php endforeach; ?>
        <?php if(!$queue): ?><tr><td colspan="5" class="text-center text-muted p-5"><i class="fas fa-check-circle fa-3x mb-3 text-success"></i><h5>No pending approvals</h5><p class="mb-0">New listings, companies, proofs, bookings and support will appear here.</p></td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

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