<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2>
                    <i class="fas fa-user-plus me-2" style="color: var(--nis-primary);"></i>
                    Create New User
                </h2>
                <a href="<?= APP_URL ?>/users" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-2"></i> Back to Users
                </a>
            </div>
        </div>
    </div>
    
    <!-- Display Errors -->
    <?php if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])): ?>
        <div class="alert alert-danger">
            <ul class="mb-0">
                <?php foreach ($_SESSION['errors'] as $error): ?>
                    <li><?= $error ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php unset($_SESSION['errors']); ?>
    <?php endif; ?>
    
    <!-- Display Form Data for repopulation -->
    <?php $formData = $_SESSION['form_data'] ?? []; ?>
    <?php unset($_SESSION['form_data']); ?>
    
    <div class="card">
        <div class="card-header">
            <i class="fas fa-user me-2"></i> User Information
        </div>
        <div class="card-body">
            <form method="POST" action="<?= APP_URL ?>/users/store">
                <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Username <span class="text-danger">*</span></label>
                        <input type="text" name="username" class="form-control" 
                               value="<?= htmlspecialchars($formData['username'] ?? '') ?>" 
                               required maxlength="50">
                        <small class="text-muted">Unique username for login</small>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Full Name <span class="text-danger">*</span></label>
                        <input type="text" name="full_name" class="form-control" 
                               value="<?= htmlspecialchars($formData['full_name'] ?? '') ?>" 
                               required maxlength="150">
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Email Address <span class="text-danger">*</span></label>
                        <input type="email" name="email" class="form-control" 
                               value="<?= htmlspecialchars($formData['email'] ?? '') ?>" 
                               required maxlength="150">
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Role <span class="text-danger">*</span></label>
                        <select name="role" class="form-select" required>
                            <option value="">Select Role</option>
                            <option value="SuperAdmin" <?= ($formData['role'] ?? '') == 'SuperAdmin' ? 'selected' : '' ?>>SuperAdmin</option>
                            <option value="Admin" <?= ($formData['role'] ?? '') == 'Admin' ? 'selected' : '' ?>>Admin</option>
                            <option value="ImmigrationOfficer" <?= ($formData['role'] ?? '') == 'ImmigrationOfficer' ? 'selected' : '' ?>>Immigration Officer</option>
                            <option value="Reviewer" <?= ($formData['role'] ?? '') == 'Reviewer' ? 'selected' : '' ?>>Reviewer</option>
                        </select>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Password <span class="text-danger">*</span></label>
                        <input type="password" name="password" class="form-control" required minlength="8">
                        <small class="text-muted">Minimum 8 characters</small>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Confirm Password <span class="text-danger">*</span></label>
                        <input type="password" name="confirm_password" class="form-control" required>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <div class="form-check mt-4">
                            <input type="checkbox" name="is_active" class="form-check-input" value="1" 
                                   <?= (!isset($formData['is_active']) || $formData['is_active'] == '1') ? 'checked' : '' ?>>
                            <label class="form-check-label">Active Account</label>
                        </div>
                    </div>
                </div>
                
                <hr>
                
                <div class="text-end">
                    <a href="<?= APP_URL ?>/users" class="btn btn-outline-secondary me-2">
                        <i class="fas fa-times me-2"></i> Cancel
                    </a>
                    <button type="submit" class="btn btn-nis">
                        <i class="fas fa-save me-2"></i> Create User
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>