{% extends 'base.html' %}
{% block title %}运营面板 - 域名管理{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-3">
  <h3>域名管理</h3>
  <div>
    <a class="btn btn-outline-secondary" href="{% url 'admin_panel:dashboard' %}">返回概览</a>
    <a class="btn btn-outline-secondary" href="/admin/domains/domain/">在 Django Admin 查看</a>
  </div>
</div>

<form method="get" class="row g-2 mb-3">
  <div class="col-md-4">
    <input type="text" name="q" value="{{ q }}" class="form-control" placeholder="搜索域名" />
  </div>
  <div class="col-md-3">
    <input type="number" name="user_id" value="{{ user_id }}" class="form-control" placeholder="用户ID（可选）" />
  </div>
  <div class="col-md-3">
    <select name="status" class="form-select">
      <option value="">全部状态</option>
      {% for val,label in status_choices %}
        <option value="{{ val }}" {% if status == val %}selected{% endif %}>{{ label }}</option>
      {% endfor %}
    </select>
  </div>
  <div class="col-md-2">
    <button class="btn btn-primary" type="submit">筛选</button>
  </div>
</form>

<div class="table-responsive">
  <table class="table table-striped">
    <thead>
      <tr>
        <th>域名</th>
        <th>用户</th>
        <th>套餐</th>
        <th>状态</th>
        <th>ServerID</th>
        <th>更新时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      {% for d in domains %}
      <tr>
        <td>{{ d.name }}</td>
        <td>{{ d.user.username }}</td>
        <td>{% if d.current_plan %}{{ d.current_plan.name }}{% else %}-{% endif %}</td>
        <td>
          {% if d.status == 'active' %}
            <span class="badge bg-success">正常</span>
          {% elif d.status == 'pending_dns' %}
            <span class="badge bg-info">等待DNS</span>
          {% elif d.status == 'suspended' %}
            <span class="badge bg-warning">暂停</span>
          {% else %}
            <span class="badge bg-secondary">已删除</span>
          {% endif %}
        </td>
        <td>{% if d.edge_server_id %}{{ d.edge_server_id }}{% else %}-{% endif %}</td>
        <td>{{ d.updated_at|date:'Y-m-d H:i' }}</td>
        <td class="text-nowrap">
          <a class="btn btn-sm btn-outline-primary me-2" href="{% url 'domains:detail' d.id %}" target="_blank">查看详情</a>
          <a class="btn btn-sm btn-outline-secondary me-2" href="{% url 'admin_panel:domain_switch_plan' d.id %}">切换套餐</a>
          <a class="btn btn-sm btn-outline-success me-2" href="{% url 'admin_panel:domain_grant_traffic' d.id %}">流量赠送</a>
          <form action="{% url 'admin_panel:domain_toggle_suspend' d.id %}" method="post" class="d-inline" onsubmit="return confirm('确定切换暂停/恢复吗？');">
            {% csrf_token %}
            <input type="text" name="confirm" class="form-control form-control-sm d-inline-block me-2" style="width: 160px;" placeholder="输入 CONFIRM" required>
            <button class="btn btn-sm btn-outline-warning">暂停/恢复</button>
          </form>
          <form action="{% url 'admin_panel:domain_delete' d.id %}" method="post" class="d-inline ms-2" onsubmit="return confirm('确认删除该域名？将标记为已删除');">
            {% csrf_token %}
            <input type="text" name="confirm" class="form-control form-control-sm d-inline-block me-2" style="width: 160px;" placeholder="输入 DELETE" required>
            <button class="btn btn-sm btn-outline-danger">删除</button>
          </form>
        </td>
      </tr>
      {% empty %}
      <tr><td colspan="7" class="text-center">暂无域名</td></tr>
      {% endfor %}
    </tbody>
  </table>
 </div>
{% endblock %}