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

{% block title %}Rooms - {% if site_settings.company_name %}DocuPulse for {{ site_settings.company_name }}{% else %}DocuPulse{% endif %}{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/rooms.css') }}?v={{ 'css/rooms.css'|asset_version }}">
{% endblock %}

{% block content %}
{{ header(
    title="Rooms",
    description="Manage your rooms and documents",
    button_text="New Room",
    button_url="#",
    icon="fa-door-open",
    button_icon="fa-plus"
) }}

<div class="container mt-4">
    <!-- Search and Filter Section -->
    <div class="bg-white rounded-lg shadow-sm p-4 mb-4">
        <form method="GET" class="d-flex align-items-center w-100 justify-content-between" id="roomFilterForm" style="gap: 1rem;">
            <input type="text" name="search" placeholder="Search rooms..." value="{{ search }}" class="form-control flex-grow-1" id="roomSearchInput" autocomplete="off" style="min-width: 0;" />
            <button type="button" id="clearRoomsFilter" class="px-4 py-2 rounded-lg text-white font-medium transition-colors duration-200 ms-2 flex-shrink-0"
                style="background-color: var(--primary-color); border: 1px solid var(--primary-color);"
                onmouseover="this.style.backgroundColor='var(--primary-light)'"
                onmouseout="this.style.backgroundColor='var(--primary-color)'">
                Clear
            </button>
        </form>
    </div>

    <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
        {% for room in rooms %}
        <div class="col">
            <div class="card h-100 shadow-sm hover-shadow">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-3">
                        <div>
                            <h5 class="card-title mb-0">{{ room.name }}</h5>
                            <div class="text-muted small mt-1">Created on {{ room.created_at.strftime('%b %d, %Y') }}</div>
                        </div>
                        <span class="badge bg-light text-dark">
                            <i class="fas fa-users"></i> {{ room.member_permissions|length }}
                        </span>
                    </div>
                    <p class="card-text text-muted">{{ room.description or 'No description' }}</p>
                    <div class="d-flex align-items-center mt-3">
                        <img src="{{ url_for('profile_pic', filename=room.creator.profile_picture) if room.creator.profile_picture else url_for('static', filename='default-avatar.png') }}"
                             alt="Creator"
                             class="rounded-circle me-2"
                             style="width: 32px; height: 32px; object-fit: cover;">
                        <div>
                            <small class="text-muted">Created by</small>
                            <div class="fw-medium">{{ room.creator.username }} {{ room.creator.last_name }}</div>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-transparent border-top-0">
                    <div class="d-flex gap-2">
                        <a href="{{ url_for('rooms.room', room_id=room.id) }}" class="btn btn-primary flex-grow-1">
                            <i class="fas fa-door-open me-2"></i>Open Room
                        </a>
                        {% if current_user.is_admin or (current_user.is_manager and room.member_permissions|selectattr('user_id', 'equalto', current_user.id)|list|length > 0) %}
                        <div class="dropdown">
                            <button class="btn btn-secondary dropdown-toggle" type="button" id="roomActions{{ room.id }}" data-bs-toggle="dropdown" aria-expanded="false">
                                <i class="fas fa-ellipsis-v"></i>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="roomActions{{ room.id }}">
                                <li>
                                    <a class="dropdown-item" href="{{ url_for('rooms.edit_room', room_id=room.id) }}">
                                        <i class="fas fa-edit me-2"></i>Edit Room
                                    </a>
                                </li>
                                <li>
                                    <a class="dropdown-item" href="{{ url_for('rooms.room_members', room_id=room.id) }}">
                                        <i class="fas fa-users me-2"></i>Manage Members
                                    </a>
                                </li>
                                <li><hr class="dropdown-divider"></li>
                                <li>
                                    <button type="button" class="dropdown-item text-danger" data-bs-toggle="modal" data-bs-target="#deleteRoomModal{{ room.id }}">
                                        <i class="fas fa-trash me-2"></i>Delete Room
                                    </button>
                                </li>
                            </ul>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% if current_user.is_admin %}
        <!-- Delete Room Modal -->
        <div class="modal fade" id="deleteRoomModal{{ room.id }}" tabindex="-1" aria-labelledby="deleteRoomModalLabel{{ room.id }}" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteRoomModalLabel{{ room.id }}">Move to Trash</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="d-flex align-items-center gap-3 mb-3">
                            <i class="fas fa-trash text-danger" style="font-size: 2rem;"></i>
                            <div>
                                <h6 class="mb-1">Move to Trash</h6>
                                <p class="text-muted mb-0" id="deleteRoomName{{ room.id }}">{{ room.name }}</p>
                            </div>
                        </div>
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle me-2"></i>
                            This room will be permanently deleted and cannot be recovered. All documents and files will be lost. Are you sure you want to proceed?
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <form action="{{ url_for('rooms.delete_room', room_id=room.id) }}" method="POST" class="d-inline">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                            <button type="submit" class="btn btn-danger">
                                <i class="fas fa-trash me-2"></i>Delete Room
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
        {% endfor %}
    </div>
</div>

<!-- Room Limit Modal -->
<div class="modal fade" id="roomLimitModal" tabindex="-1" aria-labelledby="roomLimitModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="roomLimitModalLabel">Room Limit Reached</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="d-flex align-items-center gap-3 mb-3">
                    <i class="fas fa-exclamation-circle text-warning" style="font-size: 2rem;"></i>
                    <div>
                        <h6 class="mb-1">Maximum Rooms Reached</h6>
                        <p class="text-muted mb-0">You have reached the maximum number of rooms allowed ({{ usage_stats.max_rooms }}).</p>
                    </div>
                </div>
                <div class="alert alert-info">
                    <i class="fas fa-info-circle me-2"></i>
                    Please contact your administrator if you need to create more rooms.
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

{% block extra_js %}
<script src="{{ url_for('static', filename='js/rooms-list.js') }}?v={{ 'js/rooms-list.js'|asset_version }}"></script>
<script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function() {
        // Get the New Room button and add click handler
        const newRoomBtn = document.querySelector('header button');
        if (newRoomBtn) {
            newRoomBtn.addEventListener('click', function(e) {
                if ({{ usage_stats.current_rooms }} >= {{ usage_stats.max_rooms }}) {
                    e.preventDefault();
                    var roomLimitModal = new bootstrap.Modal(document.getElementById('roomLimitModal'));
                    roomLimitModal.show();
                } else {
                    window.location.href = '{{ url_for("rooms.create_room") }}';
                }
            });
        }

        // Handle modal backdrop removal
        const roomLimitModal = document.getElementById('roomLimitModal');
        if (roomLimitModal) {
            roomLimitModal.addEventListener('hidden.bs.modal', function () {
                document.body.classList.remove('modal-open');
                const backdrop = document.querySelector('.modal-backdrop');
                if (backdrop) {
                    backdrop.remove();
                }
            });
        }
    });
</script>
{% endblock %}
{% endblock %} 