<?php $this->load->view('superadmin/layouts/header'); ?>

<!-- Page Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h3 class="mb-1">All Campaigns</h3>
        <p class="text-muted mb-0">Monitor campaigns across all portals</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= base_url('superadmin/campaigns/create') ?>" class="btn btn-sa-primary">
            <i class="bi bi-plus-circle me-2"></i>Create Campaign
        </a>
        <div class="btn-group">
            <button class="btn btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-funnel me-2"></i>Filter by Portal
            </button>
            <ul class="dropdown-menu dropdown-menu-end">
                <li><a class="dropdown-item <?= empty($portal_filter) ? 'active' : '' ?>" href="<?= base_url('superadmin/campaigns') ?>">All Portals</a></li>
                <li><hr class="dropdown-divider"></li>
                <?php if(!empty($portals)): ?>
                <?php foreach($portals as $portal): ?>
                <li><a class="dropdown-item <?= $portal_filter == $portal->code ? 'active' : '' ?>" href="<?= base_url('superadmin/campaigns?portal=' . $portal->code) ?>"><?= htmlspecialchars($portal->name) ?></a></li>
                <?php endforeach; ?>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</div>

<!-- Flash Messages -->
<?php if($this->session->flashdata('success')): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <i class="bi bi-check-circle me-2"></i><?= $this->session->flashdata('success') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <i class="bi bi-exclamation-circle me-2"></i><?= $this->session->flashdata('error') ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Campaigns</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['total'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-primary">
                    <i class="bi bi-megaphone-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Active</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['active'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-play-circle-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Scheduled</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['scheduled'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-warning">
                    <i class="bi bi-clock-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Completed</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['completed'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-info">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Campaigns Table -->
<div class="table-card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5><i class="bi bi-megaphone me-2"></i>Campaign List</h5>
        <?php if(!empty($selected_portal)): ?>
        <span class="badge bg-primary">Filtered: <?= htmlspecialchars($selected_portal) ?></span>
        <?php endif; ?>
    </div>
    <div class="card-body">
        <table class="table table-hover datatable">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Campaign Name</th>
                    <th>Portal</th>
                    <th>Created By</th>
                    <th>Total Messages</th>
                    <th>Sent</th>
                    <th>Failed</th>
                    <th>Status</th>
                    <th>Created</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($campaigns)): ?>
                <?php foreach($campaigns as $campaign): ?>
                <tr>
                    <td><?= $campaign->id ?></td>
                    <td>
                        <strong><?= htmlspecialchars($campaign->name ?? 'Unnamed') ?></strong>
                    </td>
                    <td>
                        <span class="badge bg-secondary"><?= htmlspecialchars($campaign->portal_name ?? $campaign->portal_id ?? 'Unknown') ?></span>
                    </td>
                    <td><?= htmlspecialchars($campaign->created_by ?? '-') ?></td>
                    <td><span class="badge bg-light text-dark"><?= number_format($campaign->schedule_count ?? 0) ?></span></td>
                    <td><span class="badge bg-success"><?= number_format($campaign->sent_count ?? 0) ?></span></td>
                    <td><span class="badge bg-danger"><?= number_format(($campaign->schedule_count ?? 0) - ($campaign->sent_count ?? 0)) ?></span></td>
                    <td>
                        <?php 
                        $status = strtolower($campaign->status ?? 'pending');
                        $statusClass = 'secondary';
                        if($status == 'active' || $status == 'running') $statusClass = 'success';
                        elseif($status == 'scheduled') $statusClass = 'warning';
                        elseif($status == 'completed' || $status == 'finished') $statusClass = 'info';
                        elseif($status == 'failed' || $status == 'error') $statusClass = 'danger';
                        ?>
                        <span class="badge bg-<?= $statusClass ?>"><?= ucfirst($status) ?></span>
                    </td>
                    <td><?= date('M j, Y', strtotime($campaign->created_at ?? 'now')) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="9" class="text-center py-4 text-muted">
                        <i class="bi bi-megaphone fs-1 d-block mb-2"></i>
                        No campaigns found
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->load->view('superadmin/layouts/footer'); ?>
