{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title %}Users in {{ company.name }}{% endblock %}

{% block content %}
<div class="container mt-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="mb-0">Users in {{ company.name }}</h2>
        <a href="{% url 'invite_user' %}" class="btn btn-primary">Invite New User</a>
    </div>

    {% if users_in_company %}
    <div class="card shadow-lg border-0 rounded-lg">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user_obj in users_in_company %}
                        <tr>
                            <td>{{ user_obj.username }}</td>
                            <td>{{ user_obj.email }}</td>
                            <td>
                                <form action="{% url 'user_update_role' pk=user_obj.pk %}" method="post" class="d-inline">
                                    {% csrf_token %}
                                    <select name="role" class="form-select form-select-sm d-inline w-auto" onchange="this.form.submit()">
                                        <option value="ADMIN" {% if user_obj.profile.role == 'ADMIN' %}selected{% endif %}>Admin</option>
                                        <option value="STANDARD" {% if user_obj.profile.role == 'STANDARD' %}selected{% endif %}>Standard User</option>
                                    </select>
                                </form>
                            </td>
                            <td>
                                {% if user_obj != request.user %}
                                <form action="{% url 'user_delete' pk=user_obj.pk %}" method="post" class="d-inline ms-2">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this user?');">Delete</button>
                                </form>
                                {% else %}
                                    <span class="text-muted"> (You)</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% else %}
    <div class="alert alert-info">No users found in your company yet. Invite some!</div>
    {% endif %}

    <div class="mt-4">
        <a href="{% url 'settings' %}" class="btn btn-secondary">Back to Settings</a>
    </div>
</div>
{% endblock %}