<?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">Assign Data to User</h3>
        <p class="text-muted mb-0">Assign master data records to a specific user/admin for management</p>
    </div>
    <a href="<?= base_url('superadmin/data/user_assignments') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-2"></i>View Assignments
    </a>
</div>

<!-- Flash Messages -->
<?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; ?>

<?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; ?>

<div class="row">
    <div class="col-lg-8">
        <form method="POST" action="<?= base_url('superadmin/data/assign_to_user') ?>">
            <div class="table-card mb-4">
                <div class="card-header">
                    <h5><i class="bi bi-person-plus me-2"></i>Assignment Details</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">Select User <span class="text-danger">*</span></label>
                        <select class="form-select" name="user_id" id="user_select" required>
                            <option value="">-- Select User --</option>
                            <?php 
                            $current_portal = '';
                            foreach($users as $user): 
                                if ($current_portal != $user->portal_name):
                                    if ($current_portal != '') echo '</optgroup>';
                                    $current_portal = $user->portal_name ?: 'No Portal';
                                    echo '<optgroup label="' . htmlspecialchars($current_portal) . '">';
                                endif;
                            ?>
                            <option value="<?= $user->id ?>" data-portal="<?= $user->portal_id ?>">
                                <?= htmlspecialchars($user->username) ?> (<?= $user->email ?>) - <?= ucfirst($user->role) ?>
                            </option>
                            <?php endforeach; ?>
                            <?php if ($current_portal != '') echo '</optgroup>'; ?>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Portal Filter (Optional)</label>
                        <select class="form-select" name="portal_id" id="portal_select">
                            <option value="">-- All Portals --</option>
                            <?php foreach($portals as $portal): ?>
                            <option value="<?= $portal->code ?>"><?= htmlspecialchars($portal->name) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <small class="text-muted">Filter data from a specific portal</small>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Assignment Type</label>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="assign_type" id="type_unassigned" value="unassigned_user" checked>
                            <label class="form-check-label" for="type_unassigned">
                                Only data not yet assigned to any user
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="assign_type" id="type_all" value="portal_data">
                            <label class="form-check-label" for="type_all">
                                Any available data (may reassign from other users)
                            </label>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Number of Records to Assign <span class="text-danger">*</span></label>
                        <input type="number" class="form-control" name="assign_count" required min="1" max="100000" value="1000">
                        <small class="text-muted">Enter the number of data records to assign to this user</small>
                    </div>
                </div>
            </div>
            
            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-sa-primary">
                    <i class="bi bi-check-circle me-2"></i>Assign Data
                </button>
                <a href="<?= base_url('superadmin/data') ?>" class="btn btn-outline-secondary">
                    Cancel
                </a>
            </div>
        </form>
    </div>
    
    <div class="col-lg-4">
        <div class="table-card mb-4">
            <div class="card-header">
                <h5><i class="bi bi-info-circle me-2"></i>About User Assignments</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">
                    Assigning data to users allows them to:
                </p>
                <ul class="small text-muted">
                    <li>View and manage their assigned data</li>
                    <li>Edit data records (if they have admin rights)</li>
                    <li>Send messages to their assigned contacts</li>
                    <li>Track their own campaign performance</li>
                </ul>
                <hr>
                <p class="text-muted small mb-0">
                    <strong>Tip:</strong> Use the portal filter to assign data only from a specific portal to maintain data organization.
                </p>
            </div>
        </div>
        
        <div class="table-card">
            <div class="card-header">
                <h5><i class="bi bi-people me-2"></i>Quick Stats</h5>
            </div>
            <div class="card-body">
                <div class="d-flex justify-content-between py-2 border-bottom">
                    <span class="text-muted">Total Users</span>
                    <strong><?= count($users) ?></strong>
                </div>
                <div class="d-flex justify-content-between py-2">
                    <span class="text-muted">Total Portals</span>
                    <strong><?= count($portals) ?></strong>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.getElementById('user_select').addEventListener('change', function() {
    const selectedOption = this.options[this.selectedIndex];
    const portalId = selectedOption.dataset.portal;
    if (portalId) {
        document.getElementById('portal_select').value = portalId;
    }
});
</script>

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