<?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">User Data Assignments</h3>
        <p class="text-muted mb-0">View and manage data assigned to users across all portals</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-person-plus me-2"></i>Assign Data to User
        </a>
        <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>
</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; ?>

<!-- Summary 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 Users</p>
                    <h3 class="stat-value mb-0"><?= count($assignments) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-primary">
                    <i class="bi bi-people-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">Users with Data</p>
                    <h3 class="stat-value mb-0"><?= count(array_filter($assignments, function($a) { return $a->assigned_count > 0; })) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-success">
                    <i class="bi bi-person-check-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">Total Assigned Records</p>
                    <h3 class="stat-value mb-0"><?= number_format(array_sum(array_column($assignments, 'assigned_count'))) ?></h3>
                </div>
                <div class="stat-icon bg-gradient-warning">
                    <i class="bi bi-database-fill"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Assignments Table -->
<div class="table-card">
    <div class="card-header">
        <h5><i class="bi bi-table me-2"></i>User Assignments</h5>
    </div>
    <div class="card-body">
        <table class="table table-hover datatable">
            <thead>
                <tr>
                    <th>User</th>
                    <th>Email</th>
                    <th>Portal</th>
                    <th>Role</th>
                    <th>Assigned Data</th>
                    <th>Sent</th>
                    <th>Pending</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($assignments)): ?>
                <?php foreach($assignments as $assignment): ?>
                <tr>
                    <td>
                        <div class="d-flex align-items-center">
                            <div class="user-avatar me-2" style="width: 35px; height: 35px; font-size: 0.8rem;">
                                <?= strtoupper(substr($assignment->username, 0, 1)) ?>
                            </div>
                            <strong><?= htmlspecialchars($assignment->username) ?></strong>
                        </div>
                    </td>
                    <td><?= htmlspecialchars($assignment->email) ?></td>
                    <td>
                        <span class="badge bg-secondary"><?= htmlspecialchars($assignment->portal_name ?: 'N/A') ?></span>
                    </td>
                    <td>
                        <?php if($assignment->role == 'admin'): ?>
                        <span class="badge bg-warning text-dark"><i class="bi bi-shield-fill me-1"></i>Admin</span>
                        <?php else: ?>
                        <span class="badge bg-light text-dark">User</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <strong class="text-primary"><?= number_format($assignment->assigned_count) ?></strong>
                    </td>
                    <td>
                        <span class="text-success"><?= number_format($assignment->sent_count) ?></span>
                    </td>
                    <td>
                        <span class="text-warning"><?= number_format($assignment->pending_count) ?></span>
                    </td>
                    <td>
                        <div class="btn-group btn-group-sm">
                            <?php if($assignment->assigned_count > 0): ?>
                            <a href="<?= base_url('superadmin/data/user_data/' . $assignment->user_id) ?>" class="btn btn-outline-primary" title="View Data">
                                <i class="bi bi-eye"></i>
                            </a>
                            <a href="<?= base_url('superadmin/data/unassign_from_user/' . $assignment->user_id . '?count=all') ?>" 
                               class="btn btn-outline-danger" title="Unassign All"
                               onclick="return confirm('Unassign all data from this user?')">
                                <i class="bi bi-x-circle"></i>
                            </a>
                            <?php endif; ?>
                            <a href="<?= base_url('superadmin/data/assign_to_user?user=' . $assignment->user_id) ?>" class="btn btn-outline-success" title="Assign More">
                                <i class="bi bi-plus-circle"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="8" class="text-center py-4 text-muted">
                        <i class="bi bi-people fs-1 d-block mb-2"></i>
                        No users found
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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