<?php
require_once __DIR__ . '/../lib/audience_rules.php';
$id=(int)($_GET['id']??0);$q=$conn->query("SELECT c.*,a.name audience_name FROM campaign c LEFT JOIN audience a ON a.id=c.audience_id WHERE c.id=$id AND c.account_id=$account_id LIMIT 1");if(!$q||!$q->num_rows){echo '<div class="alert alert-error">Campaign not found</div>';return;}$camp=$q->fetch_assoc();$rules=crm_decode_rules($camp['rules_json']??'');$count=crm_count_customers_by_rules($conn,$account_id,$rules);$rows=crm_get_customers_by_rules($conn,$account_id,$rules,200,0);$types=[];if(!empty($camp['type_whatsapp']))$types[]='WhatsApp';if(!empty($camp['type_email']))$types[]='Email';if(!empty($camp['type_sms']))$types[]='SMS';
?>
<div class="fade-in"><div class="toolbar"><a href="index.php?page=campaigns" class="btn btn-secondary">← Back</a><a href="index.php?page=campaign_edit&id=<?= $id ?>" class="btn btn-primary">Edit</a><button class="btn btn-secondary" onclick="prepareCampaignSend()">Send Campaign Now</button></div><div class="card" style="padding:20px;margin-bottom:16px"><h3 style="margin-top:0"><?= htmlspecialchars($camp['name']) ?></h3><p class="text-muted"><?= htmlspecialchars($camp['description']?:'') ?></p><div class="d-flex gap-8" style="flex-wrap:wrap"><span class="badge badge-blue">Type: <?= htmlspecialchars(implode(', ',$types)?:'—') ?></span><span class="badge badge-purple">Target: <?= $camp['target_type']==='audience'?'Audience - '.htmlspecialchars($camp['audience_name']?:'—'):'Dynamic Filter' ?></span><span class="badge badge-green"><?= $count ?> Recipients</span></div></div><div class="card" style="padding:20px;margin-bottom:16px"><h4>Messages</h4><div class="form-grid"><div><label class="form-label">WhatsApp</label><div class="message-box"><?= nl2br(htmlspecialchars($camp['message_whatsapp']?:'—')) ?></div></div><div><label class="form-label">SMS</label><div class="message-box"><?= nl2br(htmlspecialchars($camp['message_sms']?:'—')) ?></div></div><div><label class="form-label">Email</label><div class="message-box"><strong><?= htmlspecialchars($camp['email_subject']?:'') ?></strong><br><?= nl2br(htmlspecialchars($camp['message_email']?:'—')) ?></div></div></div></div><div class="card"><div class="table-wrapper"><table class="crm-table"><thead><tr><th>Customer</th><th>Business</th><th>Phone</th><th>Email</th><th>Contact</th></tr></thead><tbody><?php while($c=$rows->fetch_assoc()): ?><tr><td><?= htmlspecialchars($c['customer_name']) ?></td><td><?= htmlspecialchars($c['business_name']?:'—') ?></td><td><?= htmlspecialchars($c['phone']?:'—') ?></td><td><?= htmlspecialchars($c['email']?:'—') ?></td><td><button class="btn btn-primary btn-sm" onclick="openMiniContact('<?= addslashes($c['customer_name']) ?>','<?= addslashes($c['phone']?:'') ?>','<?= addslashes($c['email']?:'') ?>')">Send Message</button></td></tr><?php endwhile; ?></tbody></table></div></div><?php $logs = tableExists($conn,'campaign_logs') ? $conn->query("SELECT cl.*, c.customer_name FROM campaign_logs cl LEFT JOIN customers c ON c.id=cl.customer_id WHERE cl.campaign_id=$id ORDER BY cl.sent_at DESC, cl.id DESC LIMIT 300") : false; ?>
<div class="card" style="margin-top:16px"><div class="card-header"><span class="card-title">Send Results</span></div><div class="table-wrapper"><table class="crm-table"><thead><tr><th>Date</th><th>Customer</th><th>Type</th><th>Status</th><th>Response</th></tr></thead><tbody><?php if(!$logs || $logs->num_rows===0): ?><tr><td colspan="5" class="text-center text-muted">No send results yet</td></tr><?php else: while($l=$logs->fetch_assoc()): ?><tr><td><?= htmlspecialchars($l['sent_at'] ?? '') ?></td><td><?= htmlspecialchars($l['customer_name'] ?? ('#'.$l['customer_id'])) ?></td><td><?= htmlspecialchars($l['type']) ?></td><td><span class="badge badge-<?= $l['status']==='sent'?'green':'red' ?>"><?= htmlspecialchars($l['status']) ?></span></td><td><?= htmlspecialchars(mb_strimwidth($l['response'] ?? '',0,120,'...')) ?></td></tr><?php endwhile; endif; ?></tbody></table></div></div></div><div class="modal-overlay" id="miniContactModal"><div class="modal"><div class="modal-header"><span class="modal-title" id="miniContactTitle">Contact</span><button class="modal-close" onclick="closeModal('miniContactModal')">✕</button></div><div class="modal-body"><div class="form-group"><label class="form-label">Recipient</label><div id="miniRecipient" class="form-control"></div></div><div class="form-group"><label class="form-label">Message</label><textarea id="miniMessage" class="form-control" rows="5"></textarea></div></div><div class="modal-footer"><button class="btn btn-secondary" onclick="closeModal('miniContactModal')">Close</button><button class="btn btn-primary" onclick="showToast('Use the campaign send flow / customer contact integration','info')">Send</button></div></div></div><style>.message-box{padding:12px;border:1px solid var(--border);border-radius:10px;background:var(--bg-base);min-height:80px}</style><script>function openMiniContact(n,p,e){document.getElementById('miniContactTitle').textContent='Contact: '+n;document.getElementById('miniRecipient').textContent=p||e||'No recipient';document.getElementById('miniMessage').value='';openModal('miniContactModal')}async function prepareCampaignSend(){const r=await apiCall('handlers/campaigns.php',{action:'send',id:<?= $id ?>});if(r.success)showToast('Sent '+r.sent+' messages, failed '+r.failed,'success');else showToast(r.message||'Error','error')}</script>
