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

<!-- Welcome Section -->
<div class="row mb-4">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h3 class="mb-1">Welcome back, <?= $this->session->userdata('super_admin_name') ?>!</h3>
                <p class="text-muted mb-0">Here's what's happening across all portals today.</p>
            </div>
            <div class="text-muted">
                <i class="bi bi-calendar3 me-2"></i><?= date('l, F j, Y') ?>
            </div>
        </div>
    </div>
</div>

<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Portals</p>
                    <h2 class="stat-value mb-0"><?= $stats['total_portals'] ?? 3 ?></h2>
                    <small class="text-success"><i class="bi bi-check-circle me-1"></i><?= $stats['active_portals'] ?? 3 ?> Active</small>
                </div>
                <div class="stat-icon bg-gradient-primary">
                    <i class="bi bi-grid-3x3-gap-fill"></i>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Users</p>
                    <h2 class="stat-value mb-0"><?= number_format($stats['total_users'] ?? 0) ?></h2>
                    <small class="text-muted"><i class="bi bi-person-check me-1"></i>Across all portals</small>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-people-fill"></i>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Campaigns</p>
                    <h2 class="stat-value mb-0"><?= number_format($stats['total_campaigns'] ?? 0) ?></h2>
                    <small class="text-muted"><i class="bi bi-megaphone me-1"></i>All campaigns</small>
                </div>
                <div class="stat-icon bg-gradient-warning">
                    <i class="bi bi-megaphone-fill"></i>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Messages</p>
                    <h2 class="stat-value mb-0"><?= number_format($stats['total_messages'] ?? 0) ?></h2>
                    <small class="text-success"><i class="bi bi-check2-all me-1"></i><?= number_format($stats['sent_messages'] ?? 0) ?> Sent</small>
                </div>
                <div class="stat-icon bg-gradient-info">
                    <i class="bi bi-chat-dots-fill"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Portal Overview & Quick Actions -->
<div class="row mb-4">
    <div class="col-lg-8">
        <div class="table-card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5><i class="bi bi-grid-3x3-gap me-2"></i>Portal Overview</h5>
                <a href="<?= base_url('superadmin/portals') ?>" class="btn btn-sm btn-sa-primary">View All</a>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Portal</th>
                            <th>Users</th>
                            <th>Campaigns</th>
                            <th>Messages</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(!empty($portals)): ?>
                        <?php foreach($portals as $portal): ?>
                        <tr>
                            <td>
                                <strong><?= htmlspecialchars($portal['portal_name']) ?></strong>
                                <br><small class="text-muted"><?= htmlspecialchars($portal['portal_url'] ?? '') ?></small>
                            </td>
                            <td><span class="badge bg-primary"><?= $portal['user_count'] ?? 0 ?></span></td>
                            <td><span class="badge bg-info"><?= $portal['campaign_count'] ?? 0 ?></span></td>
                            <td><span class="badge bg-secondary"><?= number_format($portal['message_count'] ?? 0) ?></span></td>
                            <td>
                                <span class="portal-status <?= $portal['status'] ?? 'active' ?>">
                                    <?= ucfirst($portal['status'] ?? 'Active') ?>
                                </span>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php else: ?>
                        <tr>
                            <td colspan="5" class="text-center py-4 text-muted">
                                <i class="bi bi-grid-3x3-gap fs-1 d-block mb-2"></i>
                                No portals configured yet
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <div class="col-lg-4">
        <div class="table-card h-100">
            <div class="card-header">
                <h5><i class="bi bi-lightning me-2"></i>Quick Actions</h5>
            </div>
            <div class="card-body">
                <a href="<?= base_url('superadmin/portals/create') ?>" class="quick-action mb-3">
                    <i class="bi bi-plus-circle"></i>
                    <div>
                        <strong>Add New Portal</strong>
                        <small class="d-block text-muted">Configure a new portal</small>
                    </div>
                </a>
                
                <a href="<?= base_url('superadmin/users') ?>" class="quick-action mb-3">
                    <i class="bi bi-people"></i>
                    <div>
                        <strong>Manage Users</strong>
                        <small class="d-block text-muted">View all portal users</small>
                    </div>
                </a>
                
                <a href="<?= base_url('superadmin/reports') ?>" class="quick-action mb-3">
                    <i class="bi bi-graph-up"></i>
                    <div>
                        <strong>View Reports</strong>
                        <small class="d-block text-muted">Analytics & statistics</small>
                    </div>
                </a>
                
                <a href="<?= base_url('superadmin/settings') ?>" class="quick-action">
                    <i class="bi bi-gear"></i>
                    <div>
                        <strong>Settings</strong>
                        <small class="d-block text-muted">Profile & preferences</small>
                    </div>
                </a>
            </div>
        </div>
    </div>
</div>

<!-- Recent Activity -->
<div class="row">
    <div class="col-12">
        <div class="table-card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5><i class="bi bi-activity me-2"></i>Recent Activity</h5>
            </div>
            <div class="card-body">
                <?php if(!empty($recent_activities)): ?>
                <?php foreach($recent_activities as $activity): ?>
                <div class="activity-item">
                    <div class="activity-icon bg-light text-<?= $activity['type'] == 'login' ? 'primary' : ($activity['type'] == 'create' ? 'success' : 'warning') ?>">
                        <i class="bi bi-<?= $activity['type'] == 'login' ? 'box-arrow-in-right' : ($activity['type'] == 'create' ? 'plus-circle' : 'pencil') ?>"></i>
                    </div>
                    <div class="activity-content">
                        <h6><?= htmlspecialchars($activity['description']) ?></h6>
                        <small><?= date('M j, Y g:i A', strtotime($activity['created_at'])) ?></small>
                    </div>
                </div>
                <?php endforeach; ?>
                <?php else: ?>
                <div class="text-center py-4 text-muted">
                    <i class="bi bi-clock-history fs-1 d-block mb-2"></i>
                    No recent activity
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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