<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'CRM Dashboard';
$adminSubtitle = 'Lead health, unanswered enquiries, customer activity and seller response overview.';
$leads = table_exists('mj_crm_leads') ? safe_count_public('SELECT COUNT(*) c FROM mj_crm_leads') : 0;
$new = table_exists('mj_crm_leads') ? safe_count_public('SELECT COUNT(*) c FROM mj_crm_leads WHERE status IN ("new","open")') : 0;
$hot = table_exists('mj_crm_leads') ? safe_count_public('SELECT COUNT(*) c FROM mj_crm_leads WHERE lead_tag IN ("hot","Hot") OR priority IN ("hot","high")') : 0;
$customers = table_exists('mj_customers') ? safe_count_public('SELECT COUNT(*) c FROM mj_customers') : (table_exists('mj_users') ? safe_count_public('SELECT COUNT(*) c FROM mj_users') : 0);
$rows = table_exists('mj_crm_leads') ? mj_v290_simple_rows('SELECT * FROM mj_crm_leads ORDER BY id DESC LIMIT 20') : [];
include __DIR__.'/_header.php';
?>
<div class="mj-admin-pro-ribbon"><a href="customers.php"><i class="fas fa-address-book"></i><span><b><?=e($customers)?></b><small>Total customers / users</small></span></a><a href="customers.php?status=new"><i class="fas fa-user-plus"></i><span><b><?=e($new)?></b><small>New/open leads</small></span></a><a href="customers.php?tag=hot"><i class="fas fa-fire"></i><span><b><?=e($hot)?></b><small>Hot leads</small></span></a><a href="user-activity.php"><i class="fas fa-stream"></i><span><b>Timeline</b><small>User activity timeline</small></span></a><a href="broadcast.php"><i class="fas fa-bullhorn"></i><span><b>Broadcast</b><small>Message selected users</small></span></a></div>
<div class="card"><div class="card-header"><h3 class="card-title">Latest CRM Leads</h3></div><div class="card-body table-responsive"><table class="table"><thead><tr><th>ID</th><th>Name</th><th>Source</th><th>Status</th><th>Date</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td>#<?=e($r['id']??'')?></td><td><?=e($r['name']??$r['customer_name']??'Lead')?><br><small><?=e($r['email']??$r['mobile']??'')?></small></td><td><?=e($r['source']??$r['lead_source']??'')?></td><td><?=mj_v289_render_status_badge($r['status']??'new')?></td><td><?=e($r['created_at']??'')?></td></tr><?php endforeach; ?><?php if(!$rows): ?><tr><td colspan="5" class="text-center text-muted p-4">No CRM lead table/data found yet. Leads from listings, QR, company pages and bookings will appear here.</td></tr><?php endif; ?></tbody></table></div></div>
<?php include __DIR__.'/_footer.php'; ?>
