{% extends "base.html" %}

{% block title %}Assign Users to Consultants{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <h2><i class="fas fa-user-friends mr-2"></i>Assign Users to Consultants</h2>
            
            <!-- Assignment Form -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5><i class="fas fa-plus mr-2"></i>New Assignment</h5>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('admin.assign_user_to_consultant') }}">
                        <div class="row">
                            <div class="col-md-5">
                                <label for="user_id">Select User:</label>
                                <select name="user_id" id="user_id" class="form-control" required>
                                    <option value="">Choose a user...</option>
                                    {% for user in users %}
                                        <option value="{{ user.id }}">{{ user.username }} ({{ user.email }})</option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div class="col-md-5">
                                <label for="consultant_id">Select Consultant:</label>
                                <select name="consultant_id" id="consultant_id" class="form-control" required>
                                    <option value="">Choose a consultant...</option>
                                    {% for consultant in consultants %}
                                        <option value="{{ consultant.id }}">{{ consultant.username }} ({{ consultant.email }})</option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div class="col-md-2">
                                <label>&nbsp;</label>
                                <button type="submit" class="btn btn-primary btn-block">
                                    <i class="fas fa-link mr-1"></i>Assign
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Current Assignments -->
            <div class="card">
                <div class="card-header">
                    <h5><i class="fas fa-list mr-2"></i>Current User-Consultant Assignments</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>User</th>
                                    <th>Email</th>
                                    <th>Assigned Consultant</th>
                                    <th>Consultant Email</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for user in users %}
                                <tr>
                                    <td>{{ user.username }}</td>
                                    <td>{{ user.email }}</td>
                                    <td>
                                        {% if user.assigned_consultant %}
                                            <span class="badge badge-success">{{ user.assigned_consultant.username }}</span>
                                        {% else %}
                                            <span class="badge badge-secondary">Not Assigned</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if user.assigned_consultant %}
                                            {{ user.assigned_consultant.email }}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if user.assigned_consultant %}
                                            <form method="POST" action="{{ url_for('admin.unassign_user_from_consultant', user_id=user.id) }}" style="display: inline;">
                                                <button type="submit" class="btn btn-sm btn-warning" onclick="return confirm('Are you sure you want to unassign this user?')">
                                                    <i class="fas fa-unlink mr-1"></i>Unassign
                                                </button>
                                            </form>
                                        {% else %}
                                            <span class="text-muted">No action needed</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}