| | |
| | | {% block header %}RP Actions · {{ chat.title or chat.chat_id }}{% endblock %} |
| | | {% block content %} |
| | | |
| | | <div class="space-y-6"> |
| | | <div class="mx-auto w-full max-w-4xl space-y-4 sm:space-y-6"> |
| | | <section class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900 md:p-5"> |
| | | <h2 class="mb-4 font-semibold">Добавить новое действие</h2> |
| | | <form method="post" action="/api/rp/add" enctype="multipart/form-data" class="space-y-4" id="rp-form"> |
| | |
| | | <input name="response_text" type="text" required placeholder="например: Привет, {user}!" class="mt-1 w-full rounded-lg border border-gray-300 bg-transparent px-3 py-2 dark:border-gray-700"> |
| | | </label> |
| | | <label class="block text-sm">GIF (опционально) |
| | | <input name="gif" type="file" accept=".gif,image/gif" class="mt-1 w-full rounded-lg border border-gray-300 bg-transparent px-3 py-2 dark:border-gray-700"> |
| | | <input name="gif" type="file" accept=".gif,image/gif" class="mt-1 block w-full min-w-0 rounded-lg border border-gray-300 bg-transparent px-3 py-2 text-sm dark:border-gray-700"> |
| | | </label> |
| | | <div> |
| | | <button type="submit" class="rounded-lg bg-brand-600 px-4 py-2.5 text-sm font-medium text-white hover:bg-brand-700">Добавить действие</button> |
| | | <button type="submit" class="w-full rounded-lg bg-brand-600 px-4 py-3 text-sm font-medium text-white hover:bg-brand-700 sm:w-auto sm:py-2.5">Добавить действие</button> |
| | | </div> |
| | | </form> |
| | | <div id="rp-message" class="mt-3 hidden rounded-lg px-3 py-2 text-sm"></div> |
| | | </section> |
| | | |
| | | {% if actions %} |
| | | <section class="rounded-xl border border-gray-200 bg-white shadow-sm dark:border-gray-800 dark:bg-gray-900"> |
| | | <div class="border-b border-gray-200 px-5 py-3 font-semibold dark:border-gray-800">Существующие действия ({{ actions|length }})</div> |
| | | <div class="divide-y divide-gray-100 dark:divide-gray-800"> |
| | | <div class="border-b border-gray-200 px-4 py-3 font-semibold dark:border-gray-800 sm:px-5">Действия (<span id="rp-action-count">{{ actions|length }}</span>)</div> |
| | | <div id="rp-actions-list" class="divide-y divide-gray-100 dark:divide-gray-800"> |
| | | {% if actions %} |
| | | {% for a in actions %} |
| | | <div class="flex items-center justify-between gap-4 px-5 py-3 text-sm"> |
| | | <div class="min-w-0 flex-1"> |
| | | <div class="font-medium text-gray-900 dark:text-white">{{ a.trigger }}</div> |
| | | <div class="mt-1 text-gray-600 dark:text-gray-300">{{ a.response_text }}</div> |
| | | <div class="rp-action flex flex-col gap-3 px-4 py-4 text-sm sm:flex-row sm:items-center sm:justify-between sm:px-5" data-action-id="{{ a.id }}"> |
| | | <div class="min-w-0 flex-1 break-words"> |
| | | <div class="flex flex-wrap items-center gap-2 font-medium text-gray-900 dark:text-white"> |
| | | <span>{{ a.trigger }}</span> |
| | | <span class="rp-disabled-badge rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-500 dark:bg-gray-800 dark:text-gray-400 {% if a.enabled %}hidden{% endif %}">Выключено</span> |
| | | </div> |
| | | <div class="mt-1 whitespace-pre-wrap text-gray-600 dark:text-gray-300">{{ a.response_text }}</div> |
| | | {% if a.gif_file_id %} |
| | | <div class="mt-1 text-xs text-gray-500">GIF прикреплена</div> |
| | | {% endif %} |
| | | </div> |
| | | <button type="button" class="shrink-0 rounded-lg border border-red-300 px-3 py-1 text-xs text-red-600 hover:bg-red-50 dark:border-red-800 dark:text-red-300 dark:hover:bg-red-950/40 rp-delete" data-action-id="{{ a.id }}" data-chat-id="{{ chat.chat_id }}">Удалить</button> |
| | | <div class="grid grid-cols-2 gap-2 sm:flex sm:shrink-0"> |
| | | <button type="button" class="rounded-lg border border-gray-300 px-3 py-2 text-xs hover:bg-gray-100 dark:border-gray-700 dark:hover:bg-gray-800 rp-toggle" data-action-id="{{ a.id }}" data-chat-id="{{ chat.chat_id }}" data-enabled="{{ 0 if a.enabled else 1 }}">{{ "Выключить" if a.enabled else "Включить" }}</button> |
| | | <button type="button" class="rounded-lg border border-red-300 px-3 py-2 text-xs text-red-600 hover:bg-red-50 dark:border-red-800 dark:text-red-300 dark:hover:bg-red-950/40 rp-delete" data-action-id="{{ a.id }}" data-chat-id="{{ chat.chat_id }}">Удалить</button> |
| | | </div> |
| | | </div> |
| | | {% endfor %} |
| | | {% else %} |
| | | <div id="rp-empty" class="px-4 py-6 text-center text-sm text-gray-500 sm:px-5">Действий пока нет. Добавьте первое выше.</div> |
| | | {% endif %} |
| | | </div> |
| | | </section> |
| | | {% else %} |
| | | <section class="rounded-xl border border-gray-200 bg-white p-6 text-center shadow-sm dark:border-gray-800 dark:bg-gray-900"> |
| | | <div class="text-gray-500">Нет созданных действий. Добавьте первое!</div> |
| | | </section> |
| | | {% endif %} |
| | | </div> |
| | | |
| | | <script> |
| | |
| | | msgEl.className = 'mt-3 rounded-lg bg-green-50 px-3 py-2 text-sm text-green-700 dark:bg-green-950/40 dark:text-green-300'; |
| | | msgEl.textContent = 'Действие добавлено!'; |
| | | form.reset(); |
| | | setTimeout(() => location.reload(), 1000); |
| | | setTimeout(() => { |
| | | msgEl.classList.add('hidden'); |
| | | msgEl.textContent = ''; |
| | | }, 3000); |
| | | } else { |
| | | msgEl.className = 'mt-3 rounded-lg bg-red-50 px-3 py-2 text-sm text-red-700 dark:bg-red-950/40 dark:text-red-300'; |
| | | msgEl.textContent = result.msg || 'Ошибка при добавлении действия'; |
| | |
| | | } |
| | | }); |
| | | |
| | | async function postRpAction(url, values) { |
| | | const response = await fetch(url, { |
| | | method: 'POST', |
| | | headers: {'Content-Type': 'application/x-www-form-urlencoded'}, |
| | | body: new URLSearchParams(values) |
| | | }); |
| | | const body = await response.text(); |
| | | let result; |
| | | try { |
| | | result = JSON.parse(body); |
| | | } catch (err) { |
| | | throw new Error(`Сервер вернул не JSON (HTTP ${response.status}): ${body.slice(0, 240)}`); |
| | | } |
| | | if (!response.ok || !result.ok) throw new Error(result.msg || `Ошибка HTTP ${response.status}`); |
| | | return result; |
| | | } |
| | | |
| | | function updateActionCount() { |
| | | document.getElementById('rp-action-count').textContent = document.querySelectorAll('.rp-action').length; |
| | | if (!document.querySelector('.rp-action') && !document.getElementById('rp-empty')) { |
| | | const empty = document.createElement('div'); |
| | | empty.id = 'rp-empty'; |
| | | empty.className = 'px-4 py-6 text-center text-sm text-gray-500 sm:px-5'; |
| | | empty.textContent = 'Действий пока нет. Добавьте первое выше.'; |
| | | document.getElementById('rp-actions-list').appendChild(empty); |
| | | } |
| | | } |
| | | |
| | | document.querySelectorAll('.rp-toggle').forEach(btn => { |
| | | btn.addEventListener('click', async () => { |
| | | btn.disabled = true; |
| | | try { |
| | | const row = btn.closest('.rp-action'); |
| | | const enabled = btn.dataset.enabled === '1'; |
| | | await postRpAction('/api/rp/toggle', { |
| | | action_id: btn.dataset.actionId, |
| | | chat_id: btn.dataset.chatId, |
| | | enabled: btn.dataset.enabled |
| | | }); |
| | | btn.dataset.enabled = enabled ? '0' : '1'; |
| | | btn.textContent = enabled ? 'Включить' : 'Выключить'; |
| | | row.querySelector('.rp-disabled-badge').classList.toggle('hidden', enabled); |
| | | } catch (err) { |
| | | alert(err.message); |
| | | btn.disabled = false; |
| | | } |
| | | }); |
| | | }); |
| | | |
| | | document.querySelectorAll('.rp-delete').forEach(btn => { |
| | | btn.addEventListener('click', async (e) => { |
| | | if (!confirm('Удалить это действие?')) return; |
| | |
| | | const chatId = btn.dataset.chatId; |
| | | |
| | | try { |
| | | const response = await fetch('/api/rp/delete', { |
| | | method: 'POST', |
| | | headers: {'Content-Type': 'application/x-www-form-urlencoded'}, |
| | | body: new URLSearchParams({action_id: actionId, chat_id: chatId}) |
| | | }); |
| | | const body = await response.text(); |
| | | let result; |
| | | try { |
| | | result = JSON.parse(body); |
| | | } catch (err) { |
| | | throw new Error(`Сервер вернул не JSON (HTTP ${response.status}): ${body.slice(0, 240)}`); |
| | | } |
| | | |
| | | if (result.ok) { |
| | | btn.closest('.flex').remove(); |
| | | } else { |
| | | alert(result.msg || 'Ошибка при удалении'); |
| | | } |
| | | await postRpAction('/api/rp/delete', {action_id: actionId, chat_id: chatId}); |
| | | btn.closest('.rp-action').remove(); |
| | | updateActionCount(); |
| | | } catch (err) { |
| | | alert('Ошибка сети: ' + err.message); |
| | | } |