<?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"><?= htmlspecialchars($portal->name) ?> - Messages</h3>
        <p class="text-muted mb-0">View all messages for this portal</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= base_url('superadmin/messages/send') ?>" class="btn btn-sa-primary">
            <i class="bi bi-send-fill me-2"></i>Send Message
        </a>
        <a href="<?= base_url('superadmin/messages') ?>" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-2"></i>Back
        </a>
    </div>
</div>

<!-- Messages Table -->
<div class="table-card">
    <div class="card-header">
        <h5><i class="bi bi-chat-dots me-2"></i>Messages</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover datatable">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Recipient</th>
                        <th>Phone</th>
                        <th>Campaign</th>
                        <th>Message</th>
                        <th>Status</th>
                        <th>Scheduled</th>
                        <th>Sent At</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($messages)): ?>
                    <?php foreach($messages as $msg): ?>
                    <tr>
                        <td><?= $msg->id ?></td>
                        <td><?= htmlspecialchars($msg->recipient_name ?? '-') ?></td>
                        <td><?= htmlspecialchars($msg->recipient_phone ?? '-') ?></td>
                        <td><?= htmlspecialchars($msg->campaign_name ?? '-') ?></td>
                        <td>
                            <span title="<?= htmlspecialchars($msg->message ?? '') ?>">
                                <?= htmlspecialchars(substr($msg->message ?? '', 0, 40)) ?><?= strlen($msg->message ?? '') > 40 ? '...' : '' ?>
                            </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>
                            <?php if($msg->status == 'failed' && !empty($msg->error_message)): ?>
                            <small class="d-block text-danger"><?= htmlspecialchars($msg->error_message) ?></small>
                            <?php endif; ?>
                        </td>
                        <td><?= $msg->scheduled_date ?> <?= $msg->scheduled_time ?></td>
                        <td><?= $msg->sent_at ? date('M j, Y H:i', strtotime($msg->sent_at)) : '-' ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="8" class="text-center py-4 text-muted">
                            <i class="bi bi-chat-dots fs-1 d-block mb-2"></i>
                            No messages found for this portal
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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