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

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

{% block content %}
{{ header(
    title="Conversations",
    description="Manage your conversations and messages",
    button_text="New Conversation" if current_user.is_admin or current_user.is_manager else "",
    button_url="#" if current_user.is_admin or current_user.is_manager else "",
    icon="fa-comments",
    button_icon="fa-plus"
) }}

<!-- Conversation Limit Modal -->
<div class="modal fade" id="conversationLimitModal" tabindex="-1" aria-labelledby="conversationLimitModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="conversationLimitModalLabel">Conversation 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 Conversations Reached</h6>
                        <p class="text-muted mb-0">You have reached the maximum number of conversations allowed ({{ usage_stats.max_conversations }}).</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 conversations.
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

<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="conversationFilterForm" style="gap: 1rem;">
            <input type="text" name="search" placeholder="Search conversations..." value="{{ search }}" class="form-control flex-grow-1" id="conversationSearchInput" autocomplete="off" style="min-width: 0;" />
            <button type="button" id="clearConversationsFilter" 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 conversation in conversations %}
        <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">{{ conversation.name }}</h5>
                            <div class="text-muted small mt-1">Created on {{ conversation.created_at.strftime('%b %d, %Y') }}</div>
                        </div>
                        <span class="badge bg-light text-dark">
                            <i class="fas fa-users"></i> {{ conversation.members|length }}
                        </span>
                    </div>
                    <p class="card-text text-muted">{{ conversation.description or 'No description' }}</p>
                    <div class="d-flex align-items-center mt-3">
                        <img src="{{ url_for('profile_pic', filename=conversation.creator.profile_picture) if conversation.creator and conversation.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">{{ conversation.creator.username }} {{ conversation.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('conversations.conversation', conversation_id=conversation.id) }}" class="btn btn-primary flex-grow-1">
                            <i class="fas fa-comments me-2"></i>Open Conversation
                        </a>
                        {% if current_user.is_admin %}
                        <div class="dropdown">
                            <button class="btn btn-secondary dropdown-toggle" type="button" id="conversationActions{{ conversation.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="conversationActions{{ conversation.id }}">
                                <li>
                                    <a class="dropdown-item" href="{{ url_for('conversations.edit_conversation', conversation_id=conversation.id) }}">
                                        <i class="fas fa-edit me-2"></i>Edit Conversation
                                    </a>
                                </li>
                                <li><hr class="dropdown-divider"></li>
                                <li>
                                    <button type="button" class="dropdown-item text-danger" data-bs-toggle="modal" data-bs-target="#deleteConversationModal{{ conversation.id }}">
                                        <i class="fas fa-trash me-2"></i>Delete Conversation
                                    </button>
                                </li>
                            </ul>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% if current_user.is_admin %}
        <!-- Delete Conversation Modal -->
        <div class="modal fade" id="deleteConversationModal{{ conversation.id }}" tabindex="-1" aria-labelledby="deleteConversationModalLabel{{ conversation.id }}" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteConversationModalLabel{{ conversation.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="deleteConversationName{{ conversation.id }}">{{ conversation.name }}</p>
                            </div>
                        </div>
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle me-2"></i>
                            This conversation will be permanently deleted and cannot be recovered. All messages 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('conversations.delete_conversation', conversation_id=conversation.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 Conversation
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
        {% endfor %}
    </div>
</div>

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

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