<?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 Portal</h3>
        <p class="text-muted mb-0">Assign master data records to a specific portal</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>

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

<!-- Info Card -->
<div class="alert alert-info mb-4">
    <i class="bi bi-info-circle me-2"></i>
    <strong>Unassigned Records:</strong> <?= number_format($unassigned_count ?? 0) ?> records are currently not assigned to any portal.
</div>

<!-- Assign Form -->
<div class="table-card">
    <div class="card-header">
        <h5><i class="bi bi-plus-circle me-2"></i>Assign Data</h5>
    </div>
    <div class="card-body">
        <form method="post" action="<?= base_url('superadmin/data/assign') ?>">
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Select Portal <span class="text-danger">*</span></label>
                    <select name="portal_id" class="form-select" required>
                        <option value="">Choose a portal...</option>
                        <?php foreach($portals as $portal): ?>
                        <option value="<?= $portal->code ?>"><?= htmlspecialchars($portal->name) ?> (<?= $portal->code ?>)</option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Number of Records <span class="text-danger">*</span></label>
                    <input type="number" name="assign_count" class="form-control" min="1" max="1000000" value="1000" required>
                    <small class="text-muted">Maximum 1,000,000 records per operation</small>
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">Data Source <span class="text-danger">*</span></label>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="assign_type" id="unassigned" value="unassigned" checked>
                    <label class="form-check-label" for="unassigned">
                        Unassigned records only (<?= number_format($unassigned_count ?? 0) ?> available)
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="assign_type" id="all" value="all">
                    <label class="form-check-label" for="all">
                        Any records (may overwrite existing assignments)
                    </label>
                </div>
            </div>

            <hr class="my-4">
            
            <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>

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