<?php
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/mj_deal_helpers.php';
$admin=require_admin(); mj_deal_ensure_schema();
$adminTitle='Deal Reports';
$adminSubtitle='Lead, view, WhatsApp/call/email and enquiry performance for display-only deals.';
$from=trim((string)($_GET['from']??'')); $to=trim((string)($_GET['to']??'')); $countryId=(int)($_GET['country_id']??0);
$where=['1=1']; $params=[];
if($from!==''){ $where[]='d.created_at>=?'; $params[]=date('Y-m-d 00:00:00',strtotime($from)); }
if($to!==''){ $where[]='d.created_at<=?'; $params[]=date('Y-m-d 23:59:59',strtotime($to)); }
if($countryId>0){ $where[]='d.country_id=?'; $params[]=$countryId; }
$rows=[]; try { $rows=all_rows('SELECT d.*, co.name company_name, c.name country_name, cat.name category_name FROM mj_deals d LEFT JOIN mj_companies co ON co.id=d.company_id LEFT JOIN mj_countries c ON c.id=d.country_id LEFT JOIN mj_categories cat ON cat.id=d.category_id WHERE '.implode(' AND ',$where).' ORDER BY (d.enquiry_count+d.whatsapp_click_count+d.call_click_count+d.email_click_count) DESC, d.view_count DESC LIMIT 200',$params); } catch(Throwable $e){}
$totalViews=0; $totalEnq=0; $totalWa=0; $totalCall=0; $totalEmail=0; foreach($rows as $r){$totalViews+=(int)($r['view_count']??0);$totalEnq+=(int)($r['enquiry_count']??0);$totalWa+=(int)($r['whatsapp_click_count']??0);$totalCall+=(int)($r['call_click_count']??0);$totalEmail+=(int)($r['email_click_count']??0);} 
$countries=table_exists('mj_countries')?all_rows('SELECT id,name FROM mj_countries WHERE is_active=1 ORDER BY sort_order,name LIMIT 300'):[];
include __DIR__.'/_header.php';
?>
<div class="row">
 <div class="col-lg-2 col-6"><div class="small-box bg-info"><div class="inner"><h3><?=number_format($totalViews)?></h3><p>Views</p></div><div class="icon"><i class="fas fa-eye"></i></div></div></div>
 <div class="col-lg-2 col-6"><div class="small-box bg-primary"><div class="inner"><h3><?=number_format($totalEnq)?></h3><p>Enquiries</p></div><div class="icon"><i class="fas fa-envelope"></i></div></div></div>
 <div class="col-lg-2 col-6"><div class="small-box bg-success"><div class="inner"><h3><?=number_format($totalWa)?></h3><p>WhatsApp</p></div><div class="icon"><i class="fab fa-whatsapp"></i></div></div></div>
 <div class="col-lg-2 col-6"><div class="small-box bg-warning"><div class="inner"><h3><?=number_format($totalCall)?></h3><p>Calls</p></div><div class="icon"><i class="fas fa-phone"></i></div></div></div>
 <div class="col-lg-2 col-6"><div class="small-box bg-secondary"><div class="inner"><h3><?=number_format($totalEmail)?></h3><p>Email Clicks</p></div><div class="icon"><i class="fas fa-at"></i></div></div></div>
 <div class="col-lg-2 col-6"><div class="small-box bg-danger"><div class="inner"><h3><?=number_format(count($rows))?></h3><p>Deals</p></div><div class="icon"><i class="fas fa-fire"></i></div></div></div>
</div>
<div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title"><i class="fas fa-chart-line mr-2"></i>Deal Performance</h3><div class="card-tools"><a href="deals.php" class="btn btn-sm btn-outline-primary">Deals Manager</a></div></div><div class="card-body">
<form class="row mb-3" method="get"><div class="col-md-2 mb-2"><input type="date" class="form-control" name="from" value="<?=e($from)?>"></div><div class="col-md-2 mb-2"><input type="date" class="form-control" name="to" value="<?=e($to)?>"></div><div class="col-md-3 mb-2"><select class="form-control" name="country_id"><option value="0">All countries</option><?php foreach($countries as $c): ?><option value="<?=(int)$c['id']?>" <?=$countryId===(int)$c['id']?'selected':''?>><?=e($c['name'])?></option><?php endforeach; ?></select></div><div class="col-md-5 mb-2"><button class="btn btn-primary">Filter</button> <a class="btn btn-light" href="report-deals.php">Reset</a> <button type="button" onclick="window.print()" class="btn btn-outline-secondary"><i class="fas fa-print mr-1"></i>Print</button></div></form>
<div class="alert alert-info"><i class="fas fa-info-circle mr-1"></i>These are lead-generation reports only. Customer payment and merchant payout are not part of the Deals module.</div>
<div class="table-responsive"><table class="table table-striped table-hover"><thead><tr><th>Deal</th><th>Company</th><th>Country</th><th>Status</th><th>Views</th><th>Enquiry</th><th>WhatsApp</th><th>Call</th><th>Email</th><th>Share</th><th>Lead Rate</th></tr></thead><tbody>
<?php if(!$rows): ?><tr><td colspan="11" class="text-center text-muted py-4">No report data.</td></tr><?php endif; ?>
<?php foreach($rows as $r): $leads=(int)$r['enquiry_count']+(int)$r['whatsapp_click_count']+(int)$r['call_click_count']+(int)$r['email_click_count']; $views=max(1,(int)$r['view_count']); ?><tr><td><a href="deal-create.php?edit=<?=(int)$r['id']?>"><b><?=e($r['title'])?></b></a><br><small class="text-muted"><?=e($r['category_name']??'')?></small></td><td><?=e($r['company_name']?:'Direct Seller')?></td><td><?=e($r['country_name']?:'-')?></td><td><?=mj_deal_status_badge((string)$r['status'])?></td><td><?=number_format((int)$r['view_count'])?></td><td><?=number_format((int)$r['enquiry_count'])?></td><td><?=number_format((int)$r['whatsapp_click_count'])?></td><td><?=number_format((int)$r['call_click_count'])?></td><td><?=number_format((int)$r['email_click_count'])?></td><td><?=number_format((int)$r['share_count'])?></td><td><?=number_format(($leads/$views)*100,1)?>%</td></tr><?php endforeach; ?>
</tbody></table></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
