<?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($user->username) ?> - Assigned Data</h3>
        <p class="text-muted mb-0">
            <?= htmlspecialchars($user->email) ?> 
            <?php if($portal): ?>
            | <span class="badge bg-secondary"><?= htmlspecialchars($portal->name) ?></span>
            <?php endif; ?>
        </p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= base_url('superadmin/data/assign_to_user') ?>" class="btn btn-sa-primary">
            <i class="bi bi-plus-circle me-2"></i>Assign More Data
        </a>
        <a href="<?= base_url('superadmin/data/user_assignments') ?>" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-2"></i>Back to Assignments
        </a>
    </div>
</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; ?>

<!-- Stats -->
<div class="row mb-4">
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Total Assigned</p>
                    <h3 class="stat-value mb-0"><?= number_format($total) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-primary">
                    <i class="bi bi-database-fill"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">Current Page</p>
                    <h3 class="stat-value mb-0"><?= $pagination['current_page'] ?> / <?= $pagination['total_pages'] ?></h3>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-file-earmark-text"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <p class="stat-label mb-1">User Role</p>
                    <h3 class="stat-value mb-0"><?= ucfirst($user->role) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-warning">
                    <i class="bi bi-person-badge"></i>
                </div>
            </div>
        </div>
    </div>
</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 (Showing <?= count($records) ?> of <?= number_format($total) ?>)</h5>
        <div>
            <a href="<?= base_url('superadmin/data/unassign_from_user/' . $user->id . '?count=all') ?>" 
               class="btn btn-outline-danger btn-sm"
               onclick="return confirm('Unassign ALL data from this user?')">
                <i class="bi bi-x-circle me-1"></i>Unassign All
            </a>
        </div>
    </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>Mobile</th>
                        <th>Email</th>
                        <th>City</th>
                        <th>Status</th>
                        <th>Portal</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($records)): ?>
                    <?php foreach($records as $record): ?>
                    <tr>
                        <td><?= $record->id ?></td>
                        <td><?= htmlspecialchars($record->name ?? '-') ?></td>
                        <td><?= htmlspecialchars($record->mobile ?? '-') ?></td>
                        <td><?= htmlspecialchars($record->email ?? '-') ?></td>
                        <td><?= htmlspecialchars($record->city ?? '-') ?></td>
                        <td>
                            <?php 
                            $status = $record->status ?? 'pending';
                            $status_class = $status == 'sent' ? 'success' : ($status == 'failed' ? 'danger' : 'warning');
                            ?>
                            <span class="badge bg-<?= $status_class ?>"><?= ucfirst($status) ?></span>
                        </td>
                        <td>
                            <span class="badge bg-secondary"><?= htmlspecialchars($record->portal_id ?? 'N/A') ?></span>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="7" class="text-center py-4 text-muted">
                            No data 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="?page=<?= $pagination['current_page'] - 1 ?>">Previous</a>
                </li>
                <?php endif; ?>
                
                <?php 
                $start = max(1, $pagination['current_page'] - 2);
                $end = min($pagination['total_pages'], $pagination['current_page'] + 2);
                for($i = $start; $i <= $end; $i++): 
                ?>
                <li class="page-item <?= $i == $pagination['current_page'] ? 'active' : '' ?>">
                    <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                </li>
                <?php endfor; ?>
                
                <?php if($pagination['current_page'] < $pagination['total_pages']): ?>
                <li class="page-item">
                    <a class="page-link" href="?page=<?= $pagination['current_page'] + 1 ?>">Next</a>
                </li>
                <?php endif; ?>
            </ul>
        </nav>
        <?php endif; ?>
    </div>
</div>

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