{% extends "base.html" %} {% block title %} 黑名单管理 - Amrita Dashboard{%
endblock %} {% block extra_css %}
<style>
  .blacklist-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
  }

  body.dark-mode .blacklist-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: 15px;
    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;
  }

  body.dark-mode .action-btn {
    background: var(--accent-color);
  }

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

  body.dark-mode .action-btn:hover {
    background: #8a2be2;
  }

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

  .action-btn.danger {
    background: #e74c3c;
  }

  body.dark-mode .action-btn.danger {
    background: #c0392b;
  }

  .action-btn.danger:hover {
    background: #c0392b;
  }

  body.dark-mode .action-btn.danger:hover {
    background: #a53125;
  }

  .tabs-container {
    display: flex;
    margin-bottom: 20px;
    border-bottom: 1px solid #ddd;
  }

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

  .tab {
    padding: 10px 20px;
    cursor: pointer;
    border-bottom: 3px solid transparent;
    transition: all 0.3s;
    color: #555;
  }

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

  .tab.active {
    border-bottom-color: var(--primary-color);
    color: var(--primary-color);
    font-weight: 500;
  }

  body.dark-mode .tab.active {
    border-bottom-color: var(--accent-color);
    color: var(--accent-color);
  }

  .tab-content {
    display: none;
  }

  .tab-content.active {
    display: block;
  }

  .table-container {
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    overflow: hidden;
  }

  body.dark-mode .table-container {
    background: var(--dark-card-bg);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  }

  .table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #eee;
  }

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

  .table-title {
    font-size: 16px;
    font-weight: 600;
  }

  body.dark-mode .table-title {
    color: var(--dark-text-color);
  }

  .batch-actions {
    display: flex;
    gap: 10px;
    align-items: center;
  }

  .select-all {
    display: flex;
    align-items: center;
    margin-right: 15px;
  }

  .select-all input {
    margin-right: 5px;
  }

  .batch-btn {
    background: #6c757d;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.3s;
  }

  body.dark-mode .batch-btn {
    background: #5a6268;
  }

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

  body.dark-mode .batch-btn:hover {
    background: #505a60;
  }

  .batch-btn.danger {
    background: #e74c3c;
  }

  body.dark-mode .batch-btn.danger {
    background: #c0392b;
  }

  .batch-btn.danger:hover {
    background: #c0392b;
  }

  body.dark-mode .batch-btn.danger:hover {
    background: #a53125;
  }

  .table {
    width: 100%;
    border-collapse: collapse;
  }

  .table th,
  .table td {
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid #eee;
  }

  body.dark-mode .table th,
  body.dark-mode .table td {
    border-bottom: 1px solid var(--dark-border-color);
  }

  .table th {
    background-color: #f8f9fa;
    font-weight: 600;
    color: #555;
  }

  body.dark-mode .table th {
    background-color: var(--dark-sidebar-bg);
    color: var(--dark-text-color);
  }

  .table tbody tr {
    transition: background 0.3s;
  }

  .table tbody tr:hover {
    background-color: #f8f9fa;
  }

  body.dark-mode .table tbody tr:hover {
    background-color: rgba(255, 255, 255, 0.05);
  }

  .table tbody tr.selected {
    background-color: rgba(138, 43, 226, 0.1);
  }

  .id-cell {
    font-family: monospace;
    font-size: 14px;
  }

  .reason-cell {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .time-cell {
    color: #777;
    font-size: 14px;
  }

  .action-cell {
    text-align: center;
  }

  .action-icon {
    color: #e74c3c;
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: background 0.3s;
  }

  .action-icon:hover {
    background: rgba(231, 76, 60, 0.1);
  }

  .table-actions {
    display: flex;
    gap: 5px;
  }

  .table-action-btn {
    background: none;
    border: none;
    color: #777;
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: background 0.3s;
    font-size: 14px;
  }

  body.dark-mode .table-action-btn {
    color: #aaa;
  }

  .table-action-btn:hover {
    background: #e9ecef;
    color: var(--primary-color);
  }

  body.dark-mode .table-action-btn:hover {
    background: var(--dark-sidebar-hover);
    color: var(--accent-color);
  }

  .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    gap: 10px;
  }

  .page-btn {
    background: #f8f9fa;
    border: 1px solid #ddd;
    color: #333;
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
  }

  body.dark-mode .page-btn {
    background: var(--dark-sidebar-bg);
    border: 1px solid var(--dark-border-color);
    color: var(--dark-text-color);
  }

  .page-btn:hover {
    background: #e9ecef;
  }

  body.dark-mode .page-btn:hover {
    background: var(--dark-sidebar-hover);
  }

  .page-btn.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
  }

  body.dark-mode .page-btn.active {
    background: var(--accent-color);
    border-color: var(--accent-color);
  }

  .page-btn.disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .add-form {
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    padding: 20px;
    margin-bottom: 30px;
  }

  body.dark-mode .add-form {
    background: var(--dark-card-bg);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  }

  .form-header {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 20px;
    color: var(--primary-color);
  }

  body.dark-mode .form-header {
    color: var(--accent-color);
  }

  .form-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 15px;
    color: var(--primary-color);
  }

  .form-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
    margin-bottom: 20px;
  }

  .form-group {
    display: flex;
    flex-direction: column;
    margin-bottom: 15px;
    margin-top: 5px;
  }

  .form-group .form-input,
  .form-group .form-select {
    width: 100%;
    box-sizing: border-box;
    vertical-align: top;
  }

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

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

  .form-input,
  .form-select,
  .form-textarea {
    padding: 10px 12px;
    border: 1px solid #ddd;
    border-radius: 5px;
    font-size: 14px;
    background: white;
    color: #333;
  }

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

  .form-input:focus,
  .form-select:focus,
  .form-textarea:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(186, 85, 211, 0.25);
  }

  .form-textarea {
    min-height: 100px;
    resize: vertical;
  }

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

  .form-btn {
    padding: 10px 20px;
    border-radius: 5px;
    border: none;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.3s;
  }

  .form-btn.primary {
    background: var(--primary-color);
    color: white;
  }

  body.dark-mode .form-btn.primary {
    background: var(--accent-color);
  }

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

  body.dark-mode .form-btn.primary:hover {
    background: #8a2be2;
  }

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

  body.dark-mode .form-btn.secondary {
    background: #5a6268;
  }

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

  body.dark-mode .form-btn.secondary:hover {
    background: #505a60;
  }

  .form-submit {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 15px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    align-self: flex-end;
    transition: background 0.3s;
  }

  .form-submit:hover {
    background: var(--accent-color);
  }

  .no-data,
  .empty-state {
    text-align: center;
    padding: 50px 20px;
    color: #777;
  }

  body.dark-mode .no-data,
  body.dark-mode .empty-state {
    color: #aaa;
  }

  .no-data i,
  .empty-icon {
    font-size: 48px;
    margin-bottom: 15px;
    color: #ddd;
  }

  body.dark-mode .no-data i,
  body.dark-mode .empty-icon {
    color: #555;
  }

  .no-data p {
    font-size: 18px;
    margin-bottom: 20px;
  }

  @media (max-width: 768px) {
    .blacklist-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 15px;
    }

    .actions-container {
      width: 100%;
      margin-top: 15px;
    }

    .form-grid {
      grid-template-columns: 1fr;
    }

    .table-container {
      overflow-x: auto;
    }

    .table {
      min-width: 600px;
    }

    .table-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 10px;
    }

    .batch-actions {
      width: 100%;
      justify-content: space-between;
    }
  }

  @media (max-width: 480px) {
    .tabs-container {
      overflow-x: auto;
      white-space: nowrap;
    }

    .tab {
      padding: 10px 15px;
    }

    .table {
      display: block;
      overflow-x: auto;
    }

    .action-btn {
      padding: 6px 10px;
      font-size: 14px;
      margin-bottom: 10px;
      margin-top: 5px;
    }
  }

  @media (min-width: 769px) {
    .form-grid {
      align-items: end;
    }

    .form-group {
      display: inline-flex;
      flex-direction: column;
    }

    .form-input,
    .form-select {
      align-self: stretch;
    }
  }
</style>
{% endblock %} {% block content %}
<div class="blacklist-header">
  <h1 class="page-title">黑名单管理</h1>
  <div class="actions-container">
    <button class="action-btn" onclick="refreshBlacklist()">
      <i class="fas fa-sync-alt"></i> 刷新列表
    </button>
  </div>
</div>
<div class="add-form">
  <h3 class="form-title">添加黑名单</h3>
  <form id="add-blacklist-form">
    <div class="form-grid">
      <div class="form-group">
        <label class="form-label">类型</label>
        <select class="form-select" name="type" required>
          <option value="user">用户</option>
          <option value="group">群组</option>
        </select>
      </div>
      <div class="form-group">
        <label class="form-label">ID</label>
        <input
          type="text"
          class="form-input"
          name="id"
          placeholder="请输入ID"
          required
        />
      </div>
      <div class="form-group">
        <label class="form-label">原因</label>
        <input
          type="text"
          class="form-input"
          name="reason"
          placeholder="请输入原因"
          required
        />
      </div>
    </div>
    <button type="submit" class="form-submit">添加</button>
  </form>
</div>
<div class="tabs-container">
  <div class="tab active" data-tab="user-blacklist">
    用户黑名单 ({{ user_blacklist|length }})
  </div>
  <div class="tab" data-tab="group-blacklist">
    群组黑名单 ({{ group_blacklist|length }})
  </div>
</div>
<div class="tab-content active" id="user-blacklist">
  <div class="table-container">
    <div class="table-header">
      <div class="table-title">用户黑名单列表</div>
      <div class="batch-actions">
        <div class="select-all">
          <input
            type="checkbox"
            id="select-all-users"
            onchange="toggleSelectAll('user')"
          />
          <label for="select-all-users">全选</label>
        </div>
        <button class="action-btn danger" onclick="removeSelected('user')">
          <i class="fas fa-trash"></i> 批量移除
        </button>
      </div>
    </div>
    {% if user_blacklist %}
    <table class="table">
      <thead>
        <tr>
          <th width="5%"></th>
          <th width="20%">用户ID</th>
          <th width="30%">原因</th>
          <th width="20%">添加时间</th>
        </tr>
      </thead>
      <tbody>
        {% for user in user_blacklist %}
        <tr>
          <td>
            <input
              type="checkbox"
              class="user-checkbox"
              value="{{ user.id }}"
            />
          </td>
          <td class="id-cell">{{ user.id }}</td>
          <td class="reason-cell" title="{{ user.reason }}">
            {{ user.reason }}
          </td>
          <td class="time-cell">{{ user.added_time }}</td>
          <td class="action-cell">
            <i
              class="fas fa-trash action-icon"
              onclick="removeFromBlacklist('user', '{{ user.id }}')"
              title="移除黑名单"
            ></i>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div class="empty-state">
      <div class="empty-icon">
        <i class="fas fa-ban"></i>
      </div>
      <p>暂无用户黑名单记录</p>
    </div>
    {% endif %}
  </div>
</div>
<div class="tab-content" id="group-blacklist">
  <div class="table-container">
    <div class="table-header">
      <div class="table-title">群组黑名单列表</div>
      <div class="batch-actions">
        <div class="select-all">
          <input
            type="checkbox"
            id="select-all-groups"
            onchange="toggleSelectAll('group')"
          />
          <label for="select-all-groups">全选</label>
        </div>
        <button class="action-btn danger" onclick="removeSelected('group')">
          <i class="fas fa-trash"></i> 批量移除
        </button>
      </div>
    </div>
    {% if group_blacklist %}
    <table class="table">
      <thead>
        <tr>
          <th width="5%"></th>
          <th width="20%">群组ID</th>
          <th width="30%">原因</th>
          <th width="20%">添加时间</th>
        </tr>
      </thead>
      <tbody>
        {% for group in group_blacklist %}
        <tr>
          <td>
            <input
              type="checkbox"
              class="group-checkbox"
              value="{{ group.id }}"
            />
          </td>
          <td class="id-cell">{{ group.id }}</td>
          <td class="reason-cell" title="{{ group.reason }}">
            {{ group.reason }}
          </td>
          <td class="time-cell">{{ group.added_time }}</td>
          <td class="action-cell">
            <i
              class="fas fa-trash action-icon"
              onclick="removeFromBlacklist('group', '{{ group.id }}')"
              title="移除黑名单"
            ></i>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div class="empty-state">
      <div class="empty-icon">
        <i class="fas fa-ban"></i>
      </div>
      <p>暂无群组黑名单记录</p>
    </div>
    {% endif %}
  </div>
</div>
{% endblock %} {% block scripts %}
<script>
  document.querySelectorAll(".tab").forEach((tab) => {
    tab.addEventListener("click", function () {
      document
        .querySelectorAll(".tab")
        .forEach((t) => t.classList.remove("active"));
      document
        .querySelectorAll(".tab-content")
        .forEach((c) => c.classList.remove("active"));

      this.classList.add("active");
      document
        .getElementById(this.getAttribute("data-tab"))
        .classList.add("active");
    });
  });

  document
    .getElementById("add-blacklist-form")
    .addEventListener("submit", async function (e) {
      e.preventDefault();

      const formData = new FormData(this);
      const type = formData.get("type");
      const id = formData.get("id");
      const reason = formData.get("reason");

      try {
        Swal.fire({
          title: "添加中...",
          text: "正在添加黑名单",
          allowOutsideClick: false,
          didOpen: () => {
            Swal.showLoading();
          },
        });

        const response = await fetch("/api/blacklist/add", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + (await getOnetimeToken()),
          },
          body: JSON.stringify({
            type: type,
            id: id,
            reason: reason,
          }),
        });

        if (response.ok) {
          Swal.close();
          Swal.fire({
            title: "添加成功",
            text: "已成功添加到黑名单",
            icon: "success",
            timer: 1500,
            showConfirmButton: false,
          }).then(() => {
            location.reload();
          });
        } else {
          const error = await response.text();
          throw new Error(error || "添加失败");
        }
      } catch (error) {
        console.error("添加黑名单失败:", error);
        Swal.close();
        Swal.fire({
          title: "添加失败",
          text: error.message || "无法添加到黑名单，请检查输入",
          icon: "error",
          timer: 2000,
        });
      }
    });

  function toggleSelectAll(type) {
    const checkboxes = document.querySelectorAll(`.${type}-checkbox`);
    const selectAll = document.getElementById(`select-all-${type}s`);

    checkboxes.forEach((checkbox) => {
      checkbox.checked = selectAll.checked;
      toggleRowSelection(checkbox);
    });
  }

  function toggleRowSelection(checkbox) {
    const row = checkbox.closest("tr");
    if (checkbox.checked) {
      row.classList.add("selected");
    } else {
      row.classList.remove("selected");

      const type = checkbox.classList.contains("user-checkbox")
        ? "user"
        : "group";
      document.getElementById(`select-all-${type}s`).checked = false;
    }
  }

  async function removeFromBlacklist(type, id) {
    try {
      Swal.fire({
        title: "确认移除",
        text: `确定要将${
          type === "user" ? "用户" : "群组"
        } ${id} 从黑名单中移除吗？`,
        icon: "warning",
        showCancelButton: true,
        confirmButtonText: "确定移除",
        cancelButtonText: "取消",
      }).then(async (result) => {
        if (result.isConfirmed) {
          Swal.fire({
            title: "移除中...",
            text: "正在从黑名单中移除",
            allowOutsideClick: false,
            didOpen: () => {
              Swal.showLoading();
            },
          });

          const response = await fetch(`/api/blacklist/remove/${type}/${id}`, {
            method: "POST",
            headers: { Authorization: "Bearer " + (await getOnetimeToken()) },
          });

          if (response.ok) {
            Swal.close();
            Swal.fire({
              title: "移除成功",
              text: "已成功从黑名单中移除",
              icon: "success",
              timer: 1500,
              showConfirmButton: false,
            }).then(() => {
              location.reload();
            });
          } else {
            throw new Error("移除失败");
          }
        }
      });
    } catch (error) {
      console.error("移除黑名单失败:", error);
      Swal.close();
      Swal.fire({
        title: "移除失败",
        text: "无法从黑名单中移除",
        icon: "error",
        timer: 2000,
      });
    }
  }

  async function removeSelected(type) {
    const checkboxes = document.querySelectorAll(`.${type}-checkbox:checked`);

    if (checkboxes.length === 0) {
      Swal.fire({
        title: "请选择",
        text: "请至少选择一项进行移除",
        icon: "warning",
        timer: 1500,
        showConfirmButton: false,
      });
      return;
    }

    const ids = Array.from(checkboxes).map((checkbox) => checkbox.value);

    try {
      Swal.fire({
        title: "确认批量移除",
        text: `确定要将选中的 ${ids.length} 个${
          type === "user" ? "用户" : "群组"
        }从黑名单中移除吗？`,
        icon: "warning",
        showCancelButton: true,
        confirmButtonText: "确定移除",
        cancelButtonText: "取消",
      }).then(async (result) => {
        if (result.isConfirmed) {
          Swal.fire({
            title: "移除中...",
            text: `正在批量移除 ${ids.length} 个${
              type === "user" ? "用户" : "群组"
            }`,
            allowOutsideClick: false,
            didOpen: () => {
              Swal.showLoading();
            },
          });

          const response = await fetch(`/api/blacklist/remove-batch/${type}`, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              Authorization: "Bearer " + (await getOnetimeToken()),
            },
            body: JSON.stringify({ ids: ids }),
          });

          if (response.ok) {
            Swal.close();
            Swal.fire({
              title: "批量移除成功",
              text: `已成功移除 ${ids.length} 个${
                type === "user" ? "用户" : "群组"
              }`,
              icon: "success",
              timer: 1500,
              showConfirmButton: false,
            }).then(() => {
              location.reload();
            });
          } else {
            throw new Error("批量移除失败");
          }
        }
      });
    } catch (error) {
      console.error("批量移除黑名单失败:", error);
      Swal.close();
      Swal.fire({
        title: "移除失败",
        text: "无法从黑名单中移除选中的项目",
        icon: "error",
        timer: 2000,
      });
    }
  }

  function refreshBlacklist() {
    Swal.fire({
      title: "刷新中...",
      text: "正在获取最新黑名单信息",
      allowOutsideClick: false,
      didOpen: () => {
        Swal.showLoading();
      },
    });

    location.reload();
  }

  document.addEventListener("DOMContentLoaded", function () {
    document
      .querySelectorAll(".user-checkbox, .group-checkbox")
      .forEach((checkbox) => {
        checkbox.addEventListener("change", function () {
          toggleRowSelection(this);
        });
      });
  });
</script>
{% endblock %}
