File size: 17,813 Bytes
9da4125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
{% extends 'base.html' %}

{% block title %}Сообщения{% endblock %}

{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/messages.css') }}">
<script src="{{ url_for('static', filename='js/chat.js') }}"></script>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="row">
        <!-- Список контактов (боковая панель) -->
        <div class="col-md-3">
            <div class="card">
                <div class="card-header pb-0">
                    <h6>Контакты</h6>
                    <div class="input-group mb-3">
                        <input type="text" id="contactSearch" class="form-control" placeholder="Поиск...">
                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                    </div>
                </div>
                <div class="card-body p-0">
                    <div class="list-group list-group-flush" id="contactsList">
                        {% for c in contacts %}
                        <a href="{{ url_for('messages', contact_id=c.contact_id) }}" class="list-group-item list-group-item-action {% if c.contact_id == contact.contact_id %}active{% endif %}">
                            <div class="d-flex w-100 justify-content-between align-items-center">
                                <h6 class="mb-1">{{ c.username }}</h6>
                                {% if c.unread_count > 0 %}
                                <span class="badge bg-danger">{{ c.unread_count }}</span>
                                {% endif %}
                            </div>
                        </a>
                        {% endfor %}
                        {% if contacts|length == 0 %}
                        <div class="text-center py-3">
                            <p class="text-muted">Нет контактов</p>
                            <a href="{{ url_for('contacts') }}" class="btn btn-sm btn-primary">
                                <i class="fas fa-user-plus"></i> Добавить контакты
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>
                <div class="card-footer">
                    <a href="{{ url_for('contacts') }}" class="btn btn-sm btn-primary w-100">
                        <i class="fas fa-users"></i> Управление контактами
                    </a>
                </div>
            </div>
        </div>
        
        <!-- Область сообщений -->
        <div class="col-md-9">
            <div class="card">
                {% if contact %}
                <div class="card-header pb-0">
                    <div class="d-flex justify-content-between align-items-center">
                        <h6>Чат с {{ contact.username }}</h6>
                    </div>
                </div>
                <div class="card-body">
                    <div class="chat-messages" id="chatMessages" style="height: 400px; overflow-y: auto;">
                        {% if messages|length > 0 %}
                            {% for message in messages %}
                                <div class="message {% if message.sender_id == user_id %}message-sent{% else %}message-received{% endif %} mb-3" data-message-id="{{ message.id }}">
                                    <div class="message-content p-3 rounded {% if message.sender_id == user_id %}bg-primary text-white{% else %}bg-light{% endif %}">
                                        {{ message.content }}
                                        {% if message.attachment_id %}
                                        <div class="message-attachment mt-2">
                                            {% set attachment = get_file_by_id(message.attachment_id) %}
                                            {% if attachment %}
                                                <div class="attachment-info">
                                                    <i class="fas {% if attachment.file_type == 'image' %}fa-image{% elif attachment.file_type == 'video' %}fa-video{% elif attachment.file_type == 'audio' %}fa-music{% elif attachment.file_type == 'text' %}fa-file-alt{% else %}fa-file{% endif %}"></i>
                                                    <a href="{{ url_for('download_file', file_id=attachment.id) }}" class="attachment-link">
                                                        {{ attachment.original_filename }}
                                                    </a>
                                                    <span class="attachment-size">{{ format_size(attachment.size) }}</span>
                                                </div>
                                                {% if attachment.file_type == 'image' %}
                                                <div class="attachment-preview mt-2">
                                                    <a href="{{ url_for('download_file', file_id=attachment.id) }}" target="_blank">
                                                        <img src="{{ url_for('download_file', file_id=attachment.id) }}" class="img-thumbnail" style="max-width: 200px; max-height: 200px;">
                                                    </a>
                                                </div>
                                                {% elif attachment.file_type == 'video' %}
                                                <div class="attachment-preview mt-2">
                                                    <video controls class="img-thumbnail" style="max-width: 250px; max-height: 250px;">
                                                        <source src="{{ url_for('download_file', file_id=attachment.id) }}" type="video/mp4">
                                                        Ваш браузер не поддерживает видео.
                                                    </video>
                                                </div>
                                                {% elif attachment.file_type == 'audio' %}
                                                <div class="attachment-preview mt-2">
                                                    <audio controls style="max-width: 250px;">
                                                        <source src="{{ url_for('download_file', file_id=attachment.id) }}">
                                                        Ваш браузер не поддерживает аудио.
                                                    </audio>
                                                </div>
                                                {% endif %}
                                            {% endif %}
                                        </div>
                                        {% endif %}
                                        <div class="message-time text-end">
                                            <small class="{% if message.sender_id == user_id %}text-white-50{% else %}text-muted{% endif %}">
                                                {{ message.created_at }}
                                                {% if message.sender_id == user_id %}
                                                    {% if message.is_read %}
                                                    <i class="fas fa-check-double"></i>
                                                    {% else %}
                                                    <i class="fas fa-check"></i>
                                                    {% endif %}
                                                {% endif %}
                                            </small>
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                        {% else %}
                            <div class="text-center py-5">
                                <p class="text-muted">Нет сообщений. Начните общение!</p>
                            </div>
                        {% endif %}
                    </div>
                </div>
                <div class="card-footer">
                    <form action="{{ url_for('send_message', contact_id=contact.contact_id) }}" method="post" id="messageForm" enctype="multipart/form-data">
                        <div class="input-group">
                            <input type="text" name="content" class="form-control" placeholder="Введите сообщение..." required>
                            <label for="attachment" class="btn btn-outline-secondary" title="Прикрепить файл">
                                <i class="fas fa-paperclip"></i>
                                <input type="file" name="attachment" id="attachment" style="display: none;">
                            </label>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-paper-plane"></i> Отправить
                            </button>
                        </div>
                        <div id="attachment-preview" class="mt-2" style="display: none;">
                            <span class="badge bg-secondary"><span id="file-name"></span> <i class="fas fa-times" onclick="removeAttachment()"></i></span>
                        </div>
                    </form>
                </div>
                {% else %}
                <div class="card-body text-center py-5">
                    <h5 class="text-muted">Выберите контакт для начала общения</h5>
                    <a href="{{ url_for('contacts') }}" class="btn btn-primary mt-3">
                        <i class="fas fa-user-plus"></i> Управление контактами
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- Модальное окно для полноэкранного просмотра медиафайлов -->
<div class="modal fade" id="mediaModal" tabindex="-1" aria-labelledby="mediaModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-fullscreen">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="mediaModalLabel">Просмотр медиафайла</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
            </div>
            <div class="modal-body d-flex align-items-center justify-content-center">
                <div id="mediaContent" class="text-center"></div>
            </div>
        </div>
    </div>
</div>

<style>

    .message-sent {

        display: flex;

        justify-content: flex-end;

    }

    

    .message-received {

        display: flex;

        justify-content: flex-start;

    }

    

    .message-content {

        max-width: 70%;

        word-wrap: break-word;

    }

</style>

<script>

    document.addEventListener('DOMContentLoaded', function() {

        // Обработчик для открытия изображений и видео в полноэкранном режиме

        const mediaModal = new bootstrap.Modal(document.getElementById('mediaModal'));

        

        // Обработка прикрепления файлов

        const attachmentInput = document.getElementById('attachment');

        if (attachmentInput) {

            attachmentInput.addEventListener('change', function(e) {

                const fileName = e.target.files[0] ? e.target.files[0].name : '';

                if (fileName) {

                    document.getElementById('file-name').textContent = fileName;

                    document.getElementById('attachment-preview').style.display = 'block';

                } else {

                    document.getElementById('attachment-preview').style.display = 'none';

                }

            });

        }

        

        // Функция для удаления прикрепленного файла

        window.removeAttachment = function() {

            if (attachmentInput) {

                attachmentInput.value = '';

                document.getElementById('attachment-preview').style.display = 'none';

            }

        };

        // Прокрутка чата вниз при загрузке страницы

        const chatMessages = document.getElementById('chatMessages');

        if (chatMessages) {

            chatMessages.scrollTop = chatMessages.scrollHeight;

        }

        

        // Поиск по контактам

        const contactSearch = document.getElementById('contactSearch');

        if (contactSearch) {

            contactSearch.addEventListener('input', function() {

                const searchTerm = this.value.toLowerCase();

                const contacts = document.querySelectorAll('#contactsList .list-group-item');

                

                contacts.forEach(contact => {

                    const contactName = contact.querySelector('h6').textContent.toLowerCase();

                    if (contactName.includes(searchTerm)) {

                        contact.style.display = '';

                    } else {

                        contact.style.display = 'none';

                    }

                });

            });

        }

        

        // Автоматическое обновление счетчика непрочитанных сообщений

        function updateUnreadCount() {

            fetch('/get_unread_count')

                .then(response => response.json())

                .then(data => {

                    const unreadBadge = document.getElementById('unreadBadge');

                    if (unreadBadge) {

                        if (data.unread_count > 0) {

                            unreadBadge.textContent = data.unread_count;

                            unreadBadge.style.display = 'inline';

                        } else {

                            unreadBadge.style.display = 'none';

                        }

                    }

                })

                .catch(error => console.error('Ошибка при обновлении счетчика:', error));

        }

        

        // Обновление каждые 30 секунд

        setInterval(updateUnreadCount, 30000);

        

        // Инициализация автоматического обновления сообщений

        {% if contact %}

        initChatUpdater({{ contact.contact_id }});

        

        // Функция для инициализации обработчиков медиафайлов

        function initMediaHandlers() {

            // Обработка клика по изображениям

            document.querySelectorAll('.attachment-preview img').forEach(img => {

                if (!img.hasAttribute('data-media-initialized')) {

                    img.setAttribute('data-media-initialized', 'true');

                    img.style.cursor = 'pointer';

                    img.addEventListener('click', function(e) {

                        e.preventDefault();

                        const imgSrc = this.src;

                        const mediaContent = document.getElementById('mediaContent');

                        mediaContent.innerHTML = `<img src="${imgSrc}" class="img-fluid" alt="Изображение">`;

                        document.getElementById('mediaModalLabel').textContent = 'Просмотр изображения';

                        mediaModal.show();

                    });

                }

            });

            

            // Обработка клика по видео

            document.querySelectorAll('.attachment-preview video').forEach(video => {

                if (!video.hasAttribute('data-media-initialized')) {

                    video.setAttribute('data-media-initialized', 'true');

                    video.style.cursor = 'pointer';

                    video.addEventListener('click', function(e) {

                        e.preventDefault();

                        const videoSrc = this.querySelector('source').src || this.src;

                        const mediaContent = document.getElementById('mediaContent');

                        mediaContent.innerHTML = `<video controls class="img-fluid" autoplay><source src="${videoSrc}">Ваш браузер не поддерживает видео.</video>`;

                        document.getElementById('mediaModalLabel').textContent = 'Просмотр видео';

                        mediaModal.show();

                    });

                }

            });

        }

        

        // Вызываем функцию при загрузке страницы

        initMediaHandlers();

        

        // Добавляем обработчик для новых сообщений

        document.addEventListener('chatUpdated', function() {

            initMediaHandlers();

        });

        {% endif %}

        

        // Остановка обновления при уходе со страницы

        window.addEventListener('beforeunload', function() {

            stopChatUpdater();

        });

    });

</script>
{% endblock %}