<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-edit me-2" style="color: var(--nis-primary);"></i>
                    Edit User: <?= htmlspecialchars($user['username']) ?>
                </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['error'])): ?>
        <div class="alert alert-danger">
            <?= $_SESSION['error'] ?>
        </div>
        <?php unset($_SESSION['error']); ?>
    <?php endif; ?>
    
    <div class="card">
        <div class="card-header">
            <i class="fas fa-user me-2"></i> Edit User Information
        </div>
        <div class="card-body">
            <form method="POST" action="<?= APP_URL ?>/users/<?= $user['id'] ?>/update">
                <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Username</label>
                        <input type="text" class="form-control" value="<?= htmlspecialchars($user['username']) ?>" readonly disabled>
                        <small class="text-muted">Username cannot be changed</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($user['full_name']) ?>" required>
                    </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($user['email']) ?>" required>
                    </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="SuperAdmin" <?= $user['role'] == 'SuperAdmin' ? 'selected' : '' ?>>SuperAdmin</option>
                            <option value="Admin" <?= $user['role'] == 'Admin' ? 'selected' : '' ?>>Admin</option>
                            <option value="ImmigrationOfficer" <?= $user['role'] == 'ImmigrationOfficer' ? 'selected' : '' ?>>Immigration Officer</option>
                            <option value="Reviewer" <?= $user['role'] == 'Reviewer' ? 'selected' : '' ?>>Reviewer</option>
                        </select>
                        <?php if ($user['id'] == $_SESSION['user_id']): ?>
                            <small class="text-warning">You are editing your own account. Changing role may affect your access.</small>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">New Password</label>
                        <input type="password" name="password" class="form-control" minlength="8">
                        <small class="text-muted">Leave blank to keep current password. Minimum 8 characters if changing.</small>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Confirm New Password</label>
                        <input type="password" name="confirm_password" class="form-control">
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <div class="form-check">
                            <input type="checkbox" name="is_active" class="form-check-input" value="1" 
                                   <?= $user['is_active'] ? '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> Update User
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    <?php if ($user['id'] != $_SESSION['user_id']): ?>
    <div class="card mt-4 border-danger">
        <div class="card-header bg-danger text-white">
            <i class="fas fa-exclamation-triangle me-2"></i> Danger Zone
        </div>
        <div class="card-body">
            <h5>Delete User Account</h5>
            <p class="text-muted">Once you delete a user, there is no going back. Please be certain.</p>
            <button type="button" class="btn btn-danger" onclick="confirmDelete(<?= $user['id'] ?>)">
                <i class="fas fa-trash me-2"></i> Delete User
            </button>
            <form id="delete-form-<?= $user['id'] ?>" action="<?= APP_URL ?>/users/<?= $user['id'] ?>/delete" 
                  method="POST" style="display: none;">
                <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
            </form>
        </div>
    </div>
    <?php endif; ?>
</div>

<script>
function confirmDelete(userId) {
    if (confirm('Are you sure you want to delete this user? This action cannot be undone.')) {
        document.getElementById('delete-form-' + userId).submit();
    }
}
</script>