<?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) ?> - Data View</h3>
        <p class="text-muted mb-0">Viewing <?= number_format($total) ?> records</p>
    </div>
    <a href="<?= base_url('superadmin/data') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-2"></i>Back to Data
    </a>
</div>

<!-- Data Table -->
<div class="table-card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5><i class="bi bi-table me-2"></i>Data Records</h5>
        <span class="badge bg-primary">Page <?= $pagination['current_page'] ?> of <?= $pagination['total_pages'] ?></span>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover table-sm">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>City</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($records)): ?>
                    <?php foreach($records as $record): ?>
                    <tr>
                        <td><?= $record->id ?></td>
                        <td><?= htmlspecialchars($record->name ?? $record->first_name ?? '-') ?></td>
                        <td><?= htmlspecialchars($record->phone ?? $record->mobile ?? '-') ?></td>
                        <td><?= htmlspecialchars($record->email ?? '-') ?></td>
                        <td><?= htmlspecialchars($record->city ?? '-') ?></td>
                        <td><?= date('M j, Y', strtotime($record->created_at ?? 'now')) ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="6" class="text-center py-4 text-muted">
                            No records found
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        <?php if($pagination['total_pages'] > 1): ?>
        <nav class="mt-4">
            <ul class="pagination justify-content-center">
                <?php if($pagination['current_page'] > 1): ?>
                <li class="page-item">
                    <a class="page-link" href="<?= base_url('superadmin/data/view/' . $portal->code . '?page=' . ($pagination['current_page'] - 1)) ?>">Previous</a>
                </li>
                <?php endif; ?>
                
                <?php for($i = max(1, $pagination['current_page'] - 2); $i <= min($pagination['total_pages'], $pagination['current_page'] + 2); $i++): ?>
                <li class="page-item <?= $i == $pagination['current_page'] ? 'active' : '' ?>">
                    <a class="page-link" href="<?= base_url('superadmin/data/view/' . $portal->code . '?page=' . $i) ?>"><?= $i ?></a>
                </li>
                <?php endfor; ?>
                
                <?php if($pagination['current_page'] < $pagination['total_pages']): ?>
                <li class="page-item">
                    <a class="page-link" href="<?= base_url('superadmin/data/view/' . $portal->code . '?page=' . ($pagination['current_page'] + 1)) ?>">Next</a>
                </li>
                <?php endif; ?>
            </ul>
        </nav>
        <?php endif; ?>
    </div>
</div>

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