<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1"><?= e($campaign['campaign_name']) ?></h4>
        <span class="text-muted"><?= e($campaign['channel_name']) ?> | <?= status_badge($campaign['status']) ?></span>
    </div>
    <div class="d-flex gap-2">
        <?php if (in_array($campaign['status'], ['draft', 'scheduled', 'paused'])): ?>
        <button class="btn btn-success" onclick="sendCampaign(<?= $campaign['id'] ?>)"><i class="fas fa-paper-plane me-1"></i>Send Now</button>
        <button class="btn btn-outline-danger" onclick="cancelCampaign(<?= $campaign['id'] ?>)"><i class="fas fa-ban me-1"></i>Cancel</button>
        <?php endif; ?>
        <a href="/communications/bulk" class="btn btn-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="card border-primary h-100"><div class="card-body text-center"><h6 class="text-muted">Total</h6><h2><?= number_format($campaign['total_recipients']) ?></h2></div></div></div>
    <div class="col-md-3"><div class="card border-success h-100"><div class="card-body text-center"><h6 class="text-muted">Sent</h6><h2><?= number_format($campaign['sent_count']) ?></h2></div></div></div>
    <div class="col-md-3"><div class="card border-danger h-100"><div class="card-body text-center"><h6 class="text-muted">Failed</h6><h2><?= number_format($campaign['failed_count']) ?></h2></div></div></div>
    <div class="col-md-3"><div class="card h-100"><div class="card-body text-center"><h6 class="text-muted">Created</h6><p class="mb-0"><?= format_datetime($campaign['created_at']) ?></p></div></div></div>
</div>

<div class="card mb-4">
    <div class="card-header"><h6 class="mb-0">Message</h6></div>
    <div class="card-body">
        <?php if ($campaign['subject']): ?><p><strong>Subject:</strong> <?= e($campaign['subject']) ?></p><?php endif; ?>
        <div class="border p-3 rounded bg-light"><?= nl2br(e($campaign['body'])) ?></div>
    </div>
</div>

<div class="card">
    <div class="card-header"><h6 class="mb-0">Recipients</h6></div>
    <div class="card-body">
        <table id="recipients-table" class="table table-striped table-hover w-100"
               data-url="/communications/api/bulk/<?= $campaign['id'] ?>/recipients"
               data-columns='<?= json_encode([
                   ["data"=>"id","title"=>"#","width"=>"50px"],
                   ["data"=>"recipient_address","title"=>"Address"],
                   ["data"=>"full_name_en","title"=>"Member"],
                   ["data"=>"status","title"=>"Status"],
                   ["data"=>"sent_at","title"=>"Sent At"]
               ]) ?>'>
            <thead><tr><th>#</th><th>Address</th><th>Member</th><th>Status</th><th>Sent At</th></tr></thead>
        </table>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    DataTableHelper.init('recipients-table', {
        columnDefs: [
            { targets: 3, render: function(d){
                var map = {pending:'warning',sent:'success',failed:'danger',bounced:'danger'};
                return '<span class="badge bg-'+(map[d]||'secondary')+'">'+d+'</span>';
            }},
            { targets: 4, render: function(d){ return d ? new Date(d).toLocaleString() : '-'; }}
        ]
    });
});
function sendCampaign(id){
    Swal.fire({title:'Send Campaign?',text:'This will queue all pending recipients.',icon:'question',showCancelButton:true,confirmButtonText:'Send'}).then(r=>{
        if(r.isConfirmed) $.post('/communications/bulk/'+id+'/send',{_token:'<?= csrf_token() ?>'},function(r){
            if(r.success){FormHelper.showToast('success',r.message);setTimeout(()=>location.reload(),1000);}
            else FormHelper.showToast('error',r.message);
        });
    });
}
function cancelCampaign(id){
    Swal.fire({title:'Cancel?',icon:'warning',showCancelButton:true,confirmButtonColor:'#e53e3e',confirmButtonText:'Yes, Cancel'}).then(r=>{
        if(r.isConfirmed) $.post('/communications/bulk/'+id+'/cancel',{_token:'<?= csrf_token() ?>'},function(r){
            if(r.success){FormHelper.showToast('success',r.message);setTimeout(()=>location.reload(),1000);}
            else FormHelper.showToast('error',r.message);
        });
    });
}
</script>
<?php $__view->endPush() ?>