{% extends "base.html" %} {% block title %}提示词预设管理 - Amrita Dashboard{%
endblock %} {% block topbar_title %}提示词预设管理{% endblock %} {% block
extra_css %}
<style>
  .prompts-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
  }

  body.dark-mode .prompts-header {
    border-bottom: 1px solid var(--dark-border-color);
  }

  .page-title {
    font-size: 24px;
    font-weight: 600;
    color: var(--primary-color);
  }

  body.dark-mode .page-title {
    color: var(--accent-color);
  }

  .actions-container {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    margin-top: 5px;
  }

  .action-btn {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background 0.3s;
  }

  .action-btn:hover {
    background: var(--accent-color);
  }

  .action-btn i {
    margin-right: 5px;
  }

  .action-btn.secondary {
    background: #6c757d;
    color: white;
  }

  .action-btn.secondary:hover {
    background: #5a6268;
  }

  .search-box {
    position: relative;
    flex-grow: 1;
    max-width: 300px;
  }

  .search-input {
    width: 100%;
    padding: 8px 12px 8px 35px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }

  body.dark-mode .search-input {
    border: 1px solid var(--dark-border-color);
    background: var(--dark-card-bg);
    color: var(--dark-text-color);
  }

  .search-icon {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #777;
  }

  body.dark-mode .search-icon {
    color: #aaa;
  }

  .prompts-container {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
  }

  .prompt-section {
    flex: 1;
    min-width: 300px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    padding: 20px;
    border: 1px solid #eee;
  }

  body.dark-mode .prompt-section {
    background: var(--dark-card-bg);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    border: 1px solid var(--dark-border-color);
  }

  .section-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 15px;
    color: var(--text-color);
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
  }

  body.dark-mode .section-title {
    color: var(--accent-color);
    border-bottom: 1px solid var(--dark-border-color);
  }

  .prompt-card {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 15px;
    border: 1px solid #e9ecef;
    position: relative;
  }

  body.dark-mode .prompt-card {
    background: #2d2d2d;
    border: 1px solid var(--dark-border-color);
  }

  .prompt-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  .prompt-name {
    font-weight: 600;
    color: var(--primary-color);
    word-break: break-word;
  }

  body.dark-mode .prompt-name {
    color: var(--accent-color);
  }

  .prompt-actions {
    display: flex;
    gap: 8px;
  }

  .prompt-btn {
    padding: 5px 10px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: background 0.3s;
    display: flex;
    align-items: center;
  }

  .prompt-btn i {
    margin-right: 3px;
  }

  .btn-edit {
    background: #3498db;
    color: white;
  }

  .btn-edit:hover {
    background: #2980b9;
  }

  .btn-delete {
    background: #e74c3c;
    color: white;
  }

  .btn-delete:hover {
    background: #c0392b;
  }

  .prompt-content {
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 14px;
    max-height: 150px;
    overflow-y: auto;
    padding: 10px;
    background: white;
    border-radius: 4px;
    border: 1px solid #eee;
  }

  body.dark-mode .prompt-content {
    background: #252525;
    border: 1px solid var(--dark-border-color);
    color: var(--dark-text-color);
  }

  .modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }

  .modal-content {
    background-color: var(--bg-color);
    margin: 5% auto;
    padding: 20px;
    border: none;
    border-radius: 10px;
    width: 90%;
    max-width: 800px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    position: relative;
    max-height: 80vh;
    overflow-y: auto;
  }

  body.dark-mode .modal-content {
    background-color: var(--dark-bg-color);
  }

  .close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
    line-height: 1;
    position: absolute;
    right: 15px;
    top: 10px;
  }

  .close:hover,
  .close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
  }

  body.dark-mode .close:hover,
  body.dark-mode .close:focus {
    color: #fff;
  }

  .form-group {
    margin-bottom: 15px;
  }

  .form-label {
    display: block;
    margin-bottom: 5px;
    font-weight: 500;
    color: #555;
  }

  body.dark-mode .form-label {
    color: #aaa;
  }

  .form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-sizing: border-box;
  }

  body.dark-mode .form-control {
    border: 1px solid var(--dark-border-color);
    background: var(--dark-card-bg);
    color: var(--dark-text-color);
  }

  .form-control-full {
    height: 200px;
    resize: vertical;
  }

  .form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 20px;
  }

  .alert {
    padding: 12px 15px;
    border-radius: 4px;
    margin-bottom: 20px;
    display: none;
  }

  .alert-success {
    background-color: #d4edda;
    border: 1px solid #c3e6cb;
    color: #155724;
  }

  .alert-danger {
    background-color: #f8d7da;
    border: 1px solid #f5c6cb;
    color: #721c24;
  }

  body.dark-mode .alert-success {
    background-color: rgba(40, 167, 69, 0.1);
    border: 1px solid rgba(40, 167, 69, 0.2);
    color: #28a745;
  }

  body.dark-mode .alert-danger {
    background-color: rgba(220, 53, 69, 0.1);
    border: 1px solid rgba(220, 53, 69, 0.2);
    color: #dc3545;
  }

  @media (max-width: 768px) {
    .prompts-container {
      flex-direction: column;
    }

    .prompt-section {
      min-width: auto;
    }

    .prompts-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 15px;
    }

    .actions-container {
      width: 100%;
      justify-content: space-between;
    }
  }
</style>
{% endblock %} {% block content %}
<div class="config-container">
  <div class="alert alert-success" id="success-alert" style="display: none">
    操作成功！
  </div>

  <div class="alert alert-danger" id="error-alert" style="display: none">
    操作失败，请重试。
  </div>

  <div class="prompts-header">
    <h1 class="page-title">提示词预设管理</h1>
    <div class="actions-container">
      <button class="action-btn" onclick="openCreateModal('group')">
        <i class="fas fa-plus"></i> 创建群组提示词
      </button>
      <button class="action-btn secondary" onclick="openCreateModal('private')">
        <i class="fas fa-plus"></i> 创建私聊提示词
      </button>
      <button class="action-btn secondary" onclick="refreshPrompts()">
        <i class="fas fa-sync-alt"></i> 刷新
      </button>
    </div>
  </div>

  <div class="prompts-container">
    <div class="prompt-section">
      <h2 class="section-title">群组提示词</h2>
      <div id="group-prompts-container">
        {% for prompt in group_prompts %}
        <div class="prompt-card" data-name="{{ prompt.name|lower }}">
          <div class="prompt-header">
            <div class="prompt-name">{{ prompt.name }}</div>
            <div class="prompt-actions">
              <button
                class="prompt-btn btn-edit"
                onclick="openEditModal('group', '{{ prompt.name }}')"
              >
                <i class="fas fa-edit"></i> 编辑
              </button>
              <button
                class="prompt-btn btn-delete"
                onclick="deletePrompt('group', '{{ prompt.name }}')"
              >
                <i class="fas fa-trash"></i> 删除
              </button>
            </div>
          </div>
          <div class="prompt-content">{{ prompt.text }}</div>
        </div>
        {% endfor %}
      </div>
    </div>

    <div class="prompt-section">
      <h2 class="section-title">私聊提示词</h2>
      <div id="private-prompts-container">
        {% for prompt in private_prompts %}
        <div class="prompt-card" data-name="{{ prompt.name|lower }}">
          <div class="prompt-header">
            <div class="prompt-name">{{ prompt.name }}</div>
            <div class="prompt-actions">
              <button
                class="prompt-btn btn-edit"
                onclick="openEditModal('private', '{{ prompt.name }}')"
              >
                <i class="fas fa-edit"></i> 编辑
              </button>
              <button
                class="prompt-btn btn-delete"
                onclick="deletePrompt('private', '{{ prompt.name }}')"
              >
                <i class="fas fa-trash"></i> 删除
              </button>
            </div>
          </div>
          <div class="prompt-content">{{ prompt.text }}</div>
        </div>
        {% endfor %}
      </div>
    </div>
  </div>

  <!-- 创建/编辑提示词的模态框 -->
  <div id="promptModal" class="modal">
    <div class="modal-content">
      <span class="close" onclick="closeModal()">&times;</span>
      <h2 id="modalTitle">创建提示词</h2>
      <form id="promptForm">
        <input type="hidden" id="promptType" name="type" />
        <input type="hidden" id="promptName" name="name" />
        <div class="form-group">
          <label class="form-label" for="promptNameInput">提示词名称</label>
          <input
            type="text"
            class="form-control"
            id="promptNameInput"
            name="name"
            required
          />
        </div>
        <div class="form-group">
          <label class="form-label" for="promptText">提示词内容</label>
          <textarea
            class="form-control form-control-full"
            id="promptText"
            name="text"
            required
          ></textarea>
        </div>
        <div class="form-actions">
          <button
            type="button"
            class="btn btn-secondary"
            onclick="closeModal()"
          >
            取消
          </button>
          <button type="submit" class="btn btn-primary">保存</button>
        </div>
      </form>
    </div>
  </div>
</div>
{% endblock %} {% block scripts %}
<script>
  let editingPrompt = null;

  function openCreateModal(promptType) {
    document.getElementById("modalTitle").textContent = `创建${
      promptType === "group" ? "群组" : "私聊"
    }提示词`;
    document.getElementById("promptForm").reset();
    document.getElementById("promptType").value = promptType;
    document.getElementById("promptName").value = "";
    editingPrompt = null;
    document.getElementById("promptModal").style.display = "block";
  }

  function openEditModal(promptType, promptName) {
    // 使用fetch获取提示词详细信息
    fetch(`/api/chat/prompts`)
      .then((response) => response.json())
      .then((data) => {
        if (data.success) {
          const promptsList =
            promptType === "group" ? data.prompts.group : data.prompts.private;
          const prompt = promptsList.find((p) => p.name === promptName);

          if (prompt) {
            document.getElementById("modalTitle").textContent = `编辑${
              promptType === "group" ? "群组" : "私聊"
            }提示词`;
            document.getElementById("promptType").value = promptType;
            document.getElementById("promptName").value = prompt.name;
            document.getElementById("promptNameInput").value = prompt.name;
            document.getElementById("promptText").value = prompt.text;

            // 如果是default提示词，禁用名称输入框
            const nameInput = document.getElementById("promptNameInput");
            if (prompt.name === "default") {
              nameInput.disabled = true;
              nameInput.title = "default提示词不允许改名";
            } else {
              nameInput.disabled = false;
              nameInput.title = "";
            }

            editingPrompt = { type: promptType, name: prompt.name };
            document.getElementById("promptModal").style.display = "block";
          } else {
            showAlert("danger", "未找到提示词");
          }
        } else {
          showAlert("danger", "获取提示词列表失败");
        }
      })
      .catch((error) => {
        console.error("获取提示词详情失败:", error);
        showAlert("danger", "获取提示词详情失败: " + error.message);
      });
  }

  function closeModal() {
    document.getElementById("promptModal").style.display = "none";
    // 重置名称输入框状态
    const nameInput = document.getElementById("promptNameInput");
    nameInput.disabled = false;
    nameInput.title = "";
  }

  document
    .getElementById("promptForm")
    .addEventListener("submit", async function (e) {
      e.preventDefault();

      const formData = new FormData(this);
      const promptData = Object.fromEntries(formData.entries());
      const promptType = promptData.type;
      const promptName = promptData.name;
      const newPromptName = document.getElementById("promptNameInput").value;
      const promptText = promptData.text;

      try {
        let response;
        if (editingPrompt) {
          // 更新提示词，包含名称信息
          response = await fetch(
            `/api/chat/prompts/${promptType}/${encodeURIComponent(promptName)}`,
            {
              method: "PUT",
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                text: promptText,
                name: newPromptName,
              }),
            },
          );
        } else {
          // 创建提示词
          response = await fetch(`/api/chat/prompts/${promptType}`, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({ name: newPromptName, text: promptText }),
          });
        }

        const result = await response.json();

        if (result.success) {
          showAlert(
            "success",
            result.message || (editingPrompt ? "提示词已更新" : "提示词已创建"),
          );
          location.reload();
        } else {
          showAlert("danger", result.message || "操作失败");
        }
      } catch (error) {
        console.error("保存提示词失败:", error);
        showAlert("danger", "保存失败: " + error.message);
      }
    });

  async function deletePrompt(promptType, promptName) {
    if (promptName === "default") {
      showAlert("danger", "不能删除default提示词");
      return;
    }

    if (
      !confirm(
        `确定要删除${
          promptType === "group" ? "群组" : "私聊"
        }提示词 "${promptName}" 吗？此操作不可撤销。`,
      )
    ) {
      return;
    }

    try {
      const response = await fetch(
        `/api/chat/prompts/${promptType}/${encodeURIComponent(promptName)}`,
        {
          method: "DELETE",
        },
      );

      const result = await response.json();

      if (result.success) {
        showAlert("success", result.message || "提示词已删除");
        location.reload();
      } else {
        showAlert("danger", result.message || "删除失败");
      }
    } catch (error) {
      console.error("删除提示词失败:", error);
      showAlert("danger", "删除失败: " + error.message);
    }
  }

  async function refreshPrompts() {
    try {
      location.reload();
    } catch (error) {
      console.error("刷新提示词列表失败:", error);
      showAlert("danger", "刷新失败: " + error.message);
    }
  }

  function showAlert(type, message) {
    const successAlert = document.getElementById("success-alert");
    const errorAlert = document.getElementById("error-alert");

    successAlert.style.display = "none";
    errorAlert.style.display = "none";

    if (type === "success") {
      successAlert.textContent = message;
      successAlert.style.display = "block";
    } else {
      errorAlert.textContent = message;
      errorAlert.style.display = "block";
    }

    setTimeout(() => {
      successAlert.style.display = "none";
      errorAlert.style.display = "none";
    }, 3000);
  }

  // 点击模态框外部关闭
  window.onclick = function (event) {
    const modal = document.getElementById("promptModal");
    if (event.target === modal) {
      closeModal();
    }
  };

  // ESC键关闭模态框
  document.addEventListener("keydown", function (event) {
    if (event.key === "Escape") {
      closeModal();
    }
  });
</script>
{% endblock %}
