<?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">Message Center</h3>
        <p class="text-muted mb-0">View and send messages across all portals</p>
    </div>
    <a href="<?= base_url('superadmin/messages/send') ?>" class="btn btn-sa-primary">
        <i class="bi bi-send-fill me-2"></i>Send New Message
    </a>
</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 Messages</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-chat-dots-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">Sent</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['sent'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-check-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">Pending</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['pending'] ?? 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">Failed</p>
                    <h3 class="stat-value mb-0"><?= number_format($stats['failed'] ?? 0) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-danger">
                    <i class="bi bi-x-circle-fill"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Portal Messages Quick Links -->
<div class="table-card mb-4">
    <div class="card-header">
        <h5><i class="bi bi-grid-3x3-gap me-2"></i>Messages by Portal</h5>
    </div>
    <div class="card-body">
        <div class="row">
            <?php foreach($portals as $portal): ?>
            <div class="col-md-4 mb-3">
                <a href="<?= base_url('superadmin/messages/portal/' . $portal->code) ?>" class="text-decoration-none">
                    <div class="border rounded p-3 d-flex justify-content-between align-items-center hover-shadow">
                        <div>
                            <strong><?= htmlspecialchars($portal->name) ?></strong>
                            <small class="d-block text-muted"><?= $portal->code ?></small>
                        </div>
                        <i class="bi bi-chevron-right text-muted"></i>
                    </div>
                </a>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<!-- Recent Messages -->
<div class="table-card">
    <div class="card-header">
        <h5><i class="bi bi-clock-history me-2"></i>Recent Messages</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover datatable">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Portal</th>
                        <th>Campaign</th>
                        <th>Message</th>
                        <th>Status</th>
                        <th>Scheduled</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($recent_messages)): ?>
                    <?php foreach($recent_messages as $msg): ?>
                    <tr>
                        <td><?= $msg->id ?></td>
                        <td><span class="badge bg-secondary"><?= htmlspecialchars($msg->portal_name ?? $msg->portal_id ?? '-') ?></span></td>
                        <td><?= htmlspecialchars($msg->campaign_name ?? '-') ?></td>
                        <td>
                            <span title="<?= htmlspecialchars($msg->message ?? '') ?>">
                                <?= htmlspecialchars(substr($msg->message ?? '', 0, 50)) ?><?= strlen($msg->message ?? '') > 50 ? '...' : '' ?>
                            </span>
                        </td>
                        <td>
                            <?php 
                            $statusClass = 'secondary';
                            if($msg->status == 'sent') $statusClass = 'success';
                            elseif($msg->status == 'failed') $statusClass = 'danger';
                            elseif($msg->status == 'pending') $statusClass = 'warning';
                            ?>
                            <span class="badge bg-<?= $statusClass ?>"><?= ucfirst($msg->status) ?></span>
                        </td>
                        <td><?= $msg->scheduled_date ?> <?= $msg->scheduled_time ?></td>
                        <td><?= date('M j, Y H:i', strtotime($msg->created_at)) ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="7" class="text-center py-4 text-muted">
                            <i class="bi bi-chat-dots fs-1 d-block mb-2"></i>
                            No messages found
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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