<?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 Portals</h3>
        <p class="text-muted mb-0">Manage all marketing portals from one place</p>
    </div>
    <a href="<?= base_url('superadmin/portals/create') ?>" class="btn btn-sa-primary">
        <i class="bi bi-plus-circle me-2"></i>Add New Portal
    </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; ?>

<!-- Portal Cards -->
<div class="row">
    <?php if(!empty($portals)): ?>
    <?php foreach($portals as $portal): ?>
    <div class="col-xl-4 col-lg-6 mb-4">
        <div class="stat-card h-100">
            <div class="d-flex justify-content-between align-items-start mb-3">
                <div>
                    <h5 class="mb-1"><?= htmlspecialchars($portal['portal_name']) ?></h5>
                    <small class="text-muted"><?= htmlspecialchars($portal['portal_code']) ?></small>
                </div>
                <span class="portal-status <?= $portal['status'] ?>">
                    <?= ucfirst($portal['status']) ?>
                </span>
            </div>
            
            <?php if(!empty($portal['portal_url'])): ?>
            <p class="mb-3">
                <a href="<?= $portal['portal_url'] ?>" target="_blank" class="text-decoration-none">
                    <i class="bi bi-link-45deg me-1"></i><?= $portal['portal_url'] ?>
                </a>
            </p>
            <?php endif; ?>
            
            <div class="row text-center mb-3">
                <div class="col-4">
                    <div class="border rounded py-2">
                        <h5 class="mb-0"><?= $portal['user_count'] ?? 0 ?></h5>
                        <small class="text-muted">Users</small>
                    </div>
                </div>
                <div class="col-4">
                    <div class="border rounded py-2">
                        <h5 class="mb-0"><?= $portal['campaign_count'] ?? 0 ?></h5>
                        <small class="text-muted">Campaigns</small>
                    </div>
                </div>
                <div class="col-4">
                    <div class="border rounded py-2">
                        <h5 class="mb-0"><?= number_format($portal['message_count'] ?? 0) ?></h5>
                        <small class="text-muted">Messages</small>
                    </div>
                </div>
            </div>
            
            <div class="d-flex gap-2">
                <a href="<?= base_url('superadmin/portals/edit/' . $portal['id']) ?>" class="btn btn-outline-primary btn-sm flex-fill">
                    <i class="bi bi-pencil me-1"></i>Edit
                </a>
                <a href="<?= base_url('superadmin/portals/view/' . $portal['id']) ?>" class="btn btn-outline-info btn-sm flex-fill">
                    <i class="bi bi-eye me-1"></i>View
                </a>
                <?php if($portal['status'] == 'active'): ?>
                <button class="btn btn-outline-warning btn-sm" onclick="toggleStatus(<?= $portal['id'] ?>, 'inactive')" title="Deactivate">
                    <i class="bi bi-pause-circle"></i>
                </button>
                <?php else: ?>
                <button class="btn btn-outline-success btn-sm" onclick="toggleStatus(<?= $portal['id'] ?>, 'active')" title="Activate">
                    <i class="bi bi-play-circle"></i>
                </button>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
    <?php else: ?>
    <div class="col-12">
        <div class="table-card">
            <div class="card-body text-center py-5">
                <i class="bi bi-grid-3x3-gap text-muted" style="font-size: 4rem;"></i>
                <h5 class="mt-3">No Portals Found</h5>
                <p class="text-muted">Start by adding your first portal</p>
                <a href="<?= base_url('superadmin/portals/create') ?>" class="btn btn-sa-primary">
                    <i class="bi bi-plus-circle me-2"></i>Add New Portal
                </a>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

<!-- Portals Table View -->
<div class="table-card mt-4">
    <div class="card-header">
        <h5><i class="bi bi-table me-2"></i>Portal List</h5>
    </div>
    <div class="card-body">
        <table class="table table-hover datatable">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Portal Name</th>
                    <th>Code</th>
                    <th>URL</th>
                    <th>Users</th>
                    <th>Campaigns</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($portals)): ?>
                <?php foreach($portals as $portal): ?>
                <tr>
                    <td><?= $portal['id'] ?></td>
                    <td><strong><?= htmlspecialchars($portal['portal_name']) ?></strong></td>
                    <td><code><?= htmlspecialchars($portal['portal_code']) ?></code></td>
                    <td>
                        <?php if(!empty($portal['portal_url'])): ?>
                        <a href="<?= $portal['portal_url'] ?>" target="_blank"><?= $portal['portal_url'] ?></a>
                        <?php else: ?>
                        <span class="text-muted">-</span>
                        <?php endif; ?>
                    </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="portal-status <?= $portal['status'] ?>">
                            <?= ucfirst($portal['status']) ?>
                        </span>
                    </td>
                    <td><?= date('M j, Y', strtotime($portal['created_at'])) ?></td>
                    <td>
                        <div class="btn-group btn-group-sm">
                            <a href="<?= base_url('superadmin/portals/edit/' . $portal['id']) ?>" class="btn btn-outline-primary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <a href="<?= base_url('superadmin/portals/view/' . $portal['id']) ?>" class="btn btn-outline-info" title="View">
                                <i class="bi bi-eye"></i>
                            </a>
                            <button class="btn btn-outline-danger" onclick="deletePortal(<?= $portal['id'] ?>)" title="Delete">
                                <i class="bi bi-trash"></i>
                            </button>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
function toggleStatus(id, status) {
    if(confirm('Are you sure you want to change the portal status?')) {
        window.location.href = '<?= base_url('superadmin/portals/toggle_status/') ?>' + id + '/' + status;
    }
}

function deletePortal(id) {
    if(confirm('Are you sure you want to delete this portal? This action cannot be undone.')) {
        window.location.href = '<?= base_url('superadmin/portals/delete/') ?>' + id;
    }
}
</script>

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