{% extends "common/base.html" %}
{% from 'components/header.html' import header %}

{% block title %}Room Members - {{ room.name }}{% endblock %}

{% block extra_css %}
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
<link href="{{ url_for('static', filename='css/room_members.css') }}?v={{ 'css/room_members.css'|asset_version }}" rel="stylesheet" />
<style>
    .form-check-input:checked {
        background-color: var(--primary-color);
        border-color: var(--primary-color);
    }
    .form-check-input:focus {
        border-color: var(--primary-color);
        box-shadow: 0 0 0 0.25rem rgba(var(--primary-rgb), 0.25);
    }
</style>
{% endblock %}

{% block content %}
{{ header(
    title="Room Members - " + room.name,
    description="Manage who has access to this room",
    button_text="Back to Room",
    button_url=url_for('rooms.room', room_id=room.id),
    icon="fa-users",
    button_class="btn-secondary",
    button_icon="fa-arrow-left"
) }}

<div class="container-fluid py-4">
    <div class="row">
        <div class="col-md-8">
            <div class="card shadow-sm">
                <div class="card-header bg-white">
                    <h5 class="card-title mb-0">Current members in this room</h5>
                </div>
                <div class="card-body">
                    {% if room.member_permissions %}
                    <div class="list-group">
                        {% for perm in room.member_permissions %}
                        {% set member = perm.user %}
                        <div class="list-group-item d-flex justify-content-between align-items-center member-row">
                            <div class="d-flex align-items-center">
                                <img src="{{ url_for('profile_pic', filename=member.profile_picture) if member.profile_picture else url_for('static', filename='default-avatar.png') }}"
                                     alt="{{ member.username }}"
                                     class="rounded-circle me-3"
                                     style="width: 40px; height: 40px; object-fit: cover;">
                                <div>
                                    <h6 class="mb-0">{{ member.username }} {{ member.last_name }}</h6>
                                    <small class="text-muted">{{ member.email }}</small>
                                </div>
                            </div>
                            <div class="d-flex align-items-center gap-2">
                            {% if member.id != room.created_by %}
                                <form action="{{ url_for('rooms.update_member_permissions', room_id=room.id, user_id=member.id) }}" method="POST" class="d-flex align-items-center gap-2 auto-save-perms-form">
                                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                    <div class="form-check form-check-inline mb-0">
                                        <input class="form-check-input" type="checkbox" name="can_view" id="can_view_{{ member.id }}" checked disabled>
                                        <input type="hidden" name="can_view" value="1">
                                        <label class="form-check-label" for="can_view_{{ member.id }}" title="View permission is always required" style="color: var(--secondary-color);"><i class="fas fa-eye"></i></label>
                                    </div>
                                    <div class="form-check form-check-inline mb-0">
                                        <input class="form-check-input" type="checkbox" name="can_download" id="can_download_{{ member.id }}" {% if perm.can_download %}checked{% endif %}>
                                        <label class="form-check-label" for="can_download_{{ member.id }}" title="Can Download Files" style="color: var(--secondary-color);"><i class="fas fa-download"></i></label>
                                    </div>
                                    <div class="form-check form-check-inline mb-0">
                                        <input class="form-check-input" type="checkbox" name="can_upload" id="can_upload_{{ member.id }}" {% if perm.can_upload %}checked{% endif %}>
                                        <label class="form-check-label" for="can_upload_{{ member.id }}" title="Can Upload Files" style="color: var(--secondary-color);"><i class="fas fa-upload"></i></label>
                                    </div>
                                    <div class="form-check form-check-inline mb-0">
                                        <input class="form-check-input" type="checkbox" name="can_delete" id="can_delete_{{ member.id }}" {% if perm.can_delete %}checked{% endif %}>
                                        <label class="form-check-label" for="can_delete_{{ member.id }}" title="Can Delete Files" style="color: var(--secondary-color);"><i class="fas fa-trash"></i></label>
                                    </div>
                                    <div class="form-check form-check-inline mb-0">
                                        <input class="form-check-input" type="checkbox" name="can_rename" id="can_rename_{{ member.id }}" {% if perm.can_rename %}checked{% endif %}>
                                        <label class="form-check-label" for="can_rename_{{ member.id }}" title="Can Rename Files" style="color: var(--secondary-color);"><i class="fas fa-i-cursor"></i></label>
                                    </div>
                                    <div class="form-check form-check-inline mb-0">
                                        <input class="form-check-input" type="checkbox" name="can_move" id="can_move_{{ member.id }}" {% if perm.can_move %}checked{% endif %}>
                                        <label class="form-check-label" for="can_move_{{ member.id }}" title="Can Move Files" style="color: var(--secondary-color);"><i class="fas fa-arrows-alt"></i></label>
                                    </div>
                                </form>
                                <form action="{{ url_for('rooms.remove_member', room_id=room.id, user_id=member.id) }}" method="POST" class="d-inline ms-2">
                                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                    <button type="submit" class="btn btn-remove-member px-3 d-flex align-items-center gap-1">
                                        <i class="fas fa-user-minus"></i> Remove
                                    </button>
                                </form>
                            {% else %}
                                <span class="badge creator-badge px-3 py-2 d-flex align-items-center gap-1" style="height: 32px;">
                                    <i class="fas fa-user"></i> Creator
                                </span>
                            {% endif %}
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    {% else %}
                    <p class="text-muted mb-0">No members in this room yet.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card shadow-sm">
                <div class="card-header bg-white">
                    <h5 class="card-title mb-0">Add New Member/h5>
                </div>
                <div class="card-body">
                    <form action="{{ url_for('rooms.add_member', room_id=room.id) }}" method="POST">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                        <div class="mb-3">
                            <label for="user_id" class="form-label">Select a user from the available users</label>
                            <select class="form-select select2" id="user_id" name="user_id" required>
                                <option value="">Search for a user...</option>
                                {% for user in available_users %}
                                <option value="{{ user.id }}">{{ user.username }} {{ user.last_name }} ({{ user.email }})</option>
                                {% endfor %}
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-user-plus me-2"></i>Add Member to the list
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src="{{ url_for('static', filename='js/room-members.js') }}?v={{ 'js/room-members.js'|asset_version }}"></script>
{% endblock %} 