| | |
| | | {% 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="border-b border-gray-200 px-4 py-3 font-semibold dark:border-gray-800 sm:px-5">Действия ({{ actions|length }})</div> |
| | | {% if actions %} |
| | | <div class="divide-y divide-gray-100 dark:divide-gray-800"> |
| | | {% 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="flex flex-col gap-3 px-4 py-4 text-sm sm:flex-row sm:items-center sm:justify-between sm:px-5"> |
| | | <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> |
| | | {% if not a.enabled %}<span class="rounded-full bg-gray-100 px-2 py-0.5 text-xs text-gray-500 dark:bg-gray-800 dark:text-gray-400">Выключено</span>{% endif %} |
| | | </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 %} |
| | | </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> |
| | | <div class="px-4 py-6 text-center text-sm text-gray-500 sm:px-5">Действий пока нет. Добавьте первое выше.</div> |
| | | {% endif %} |
| | | </section> |
| | | </div> |
| | | |
| | | <script> |
| | |
| | | } |
| | | }); |
| | | |
| | | document.querySelectorAll('.rp-delete').forEach(btn => { |
| | | btn.addEventListener('click', async (e) => { |
| | | if (!confirm('Удалить это действие?')) return; |
| | | |
| | | const actionId = btn.dataset.actionId; |
| | | const chatId = btn.dataset.chatId; |
| | | |
| | | try { |
| | | const response = await fetch('/api/rp/delete', { |
| | | async function postRpAction(url, values) { |
| | | const response = await fetch(url, { |
| | | method: 'POST', |
| | | headers: {'Content-Type': 'application/x-www-form-urlencoded'}, |
| | | body: new URLSearchParams({action_id: actionId, chat_id: chatId}) |
| | | body: new URLSearchParams(values) |
| | | }); |
| | | const body = await response.text(); |
| | | let result; |
| | |
| | | } catch (err) { |
| | | throw new Error(`Сервер вернул не JSON (HTTP ${response.status}): ${body.slice(0, 240)}`); |
| | | } |
| | | |
| | | if (result.ok) { |
| | | btn.closest('.flex').remove(); |
| | | } else { |
| | | alert(result.msg || 'Ошибка при удалении'); |
| | | if (!response.ok || !result.ok) throw new Error(result.msg || `Ошибка HTTP ${response.status}`); |
| | | return result; |
| | | } |
| | | |
| | | document.querySelectorAll('.rp-toggle').forEach(btn => { |
| | | btn.addEventListener('click', async () => { |
| | | btn.disabled = true; |
| | | try { |
| | | await postRpAction('/api/rp/toggle', { |
| | | action_id: btn.dataset.actionId, |
| | | chat_id: btn.dataset.chatId, |
| | | enabled: btn.dataset.enabled |
| | | }); |
| | | location.reload(); |
| | | } catch (err) { |
| | | alert(err.message); |
| | | btn.disabled = false; |
| | | } |
| | | }); |
| | | }); |
| | | |
| | | document.querySelectorAll('.rp-delete').forEach(btn => { |
| | | btn.addEventListener('click', async (e) => { |
| | | if (!confirm('Удалить это действие?')) return; |
| | | |
| | | const actionId = btn.dataset.actionId; |
| | | const chatId = btn.dataset.chatId; |
| | | |
| | | try { |
| | | await postRpAction('/api/rp/delete', {action_id: actionId, chat_id: chatId}); |
| | | location.reload(); |
| | | } catch (err) { |
| | | alert('Ошибка сети: ' + err.message); |
| | | } |