<?php
/**
 * @var object $campaigns
 * @var string $status
 * @var int $totalCampaigns
 * @var int $activeCampaigns
 * @var int $emailTemplates
 * @var int $smsTemplates
 */

$isDashboard = !isset($campaigns);
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-bullhorn me-2 text-gold"></i><?= $isDashboard ? 'Marketing Dashboard' : 'Campaigns' ?></h5>
</div>

<?php if ($isDashboard): ?>
<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="glass-card text-center p-3">
            <h3 class="text-gold mb-1"><?= (int)$totalCampaigns ?></h3>
            <small class="text-muted">Total Campaigns</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card text-center p-3">
            <h3 class="text-success mb-1"><?= (int)$activeCampaigns ?></h3>
            <small class="text-muted">Active Campaigns</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card text-center p-3">
            <h3 class="text-info mb-1"><?= (int)$emailTemplates ?></h3>
            <small class="text-muted">Email Templates</small>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card text-center p-3">
            <h3 class="text-warning mb-1"><?= (int)$smsTemplates ?></h3>
            <small class="text-muted">SMS Templates</small>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-4">
        <a href="<?= url('marketing/campaigns') ?>" class="text-decoration-none">
            <div class="glass-card p-3 text-center">
                <i class="fas fa-bullhorn fa-2x text-gold mb-2"></i>
                <h6>Campaigns</h6>
            </div>
        </a>
    </div>
    <div class="col-md-4">
        <a href="<?= url('marketing/email-templates') ?>" class="text-decoration-none">
            <div class="glass-card p-3 text-center">
                <i class="fas fa-envelope fa-2x text-gold mb-2"></i>
                <h6>Email Templates</h6>
            </div>
        </a>
    </div>
    <div class="col-md-4">
        <a href="<?= url('marketing/sms-templates') ?>" class="text-decoration-none">
            <div class="glass-card p-3 text-center">
                <i class="fas fa-sms fa-2x text-gold mb-2"></i>
                <h6>SMS Templates</h6>
            </div>
        </a>
    </div>
</div>
<?php else: ?>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Campaigns</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#campaignModal">
                <i class="fas fa-plus me-1"></i>Create Campaign
            </button>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-3">
                <select class="form-select form-select-sm bg-transparent" onchange="window.location.href=this.value">
                    <option value="<?= url('marketing/campaigns') ?>">All Status</option>
                    <option value="draft" <?= $status === 'draft' ? 'selected' : '' ?>>Draft</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="scheduled" <?= $status === 'scheduled' ? 'selected' : '' ?>>Scheduled</option>
                    <option value="completed" <?= $status === 'completed' ? 'selected' : '' ?>>Completed</option>
                    <option value="cancelled" <?= $status === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Type</th>
                        <th>Audience</th>
                        <th>Status</th>
                        <th>Scheduled</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($campaigns->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-bullhorn fa-2x mb-2"></i>
                                <p class="mb-0">No campaigns found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($campaigns->currentPage - 1) * $campaigns->perPage + 1; ?>
                        <?php foreach ($campaigns->data as $c): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><strong><?= e($c->name) ?></strong></td>
                                <td><span class="badge bg-info"><?= e(ucfirst($c->type)) ?></span></td>
                                <td><?= e(ucfirst($c->audience)) ?></td>
                                <td><span class="badge bg-<?= statusBadge($c->status) ?>"><?= e(ucfirst($c->status)) ?></span></td>
                                <td><?= $c->scheduled_at ? formatDateTime($c->scheduled_at) : '-' ?></td>
                                <td><?= formatDateTime($c->created_at) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($campaigns->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($campaigns->currentPage - 1) * $campaigns->perPage) + 1 ?>
                    to <?= min($campaigns->currentPage * $campaigns->perPage, $campaigns->total) ?>
                    of <?= $campaigns->total ?> entries
                </div>
                <?= paginateLinks($campaigns, url('marketing/campaigns')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="modal fade" id="campaignModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h6 class="modal-title"><i class="fas fa-plus-circle text-gold me-2"></i>Create Campaign</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('marketing/campaigns/store') ?>">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Campaign Name</label>
                                <input type="text" name="name" class="form-control bg-transparent" required maxlength="255">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label class="form-label">Type</label>
                                <select name="type" class="form-select bg-transparent" required>
                                    <option value="email">Email</option>
                                    <option value="sms">SMS</option>
                                    <option value="both">Both</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label class="form-label">Audience</label>
                                <select name="audience" class="form-select bg-transparent" required>
                                    <option value="all">All</option>
                                    <option value="customers">Customers</option>
                                    <option value="leads">Leads</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Subject (Email)</label>
                        <input type="text" name="subject" class="form-control bg-transparent" maxlength="255">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Message</label>
                        <textarea name="message" class="form-control bg-transparent" rows="4" required maxlength="5000"></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Schedule (optional)</label>
                        <input type="datetime-local" name="scheduled_at" class="form-control bg-transparent">
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Create Campaign</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>
