{% extends 'base.html' %}
{% block title %}运营面板 - 操作日志{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-3">
  <h3>操作日志</h3>
  <div class="btn-group">
    <a class="btn btn-outline-secondary" href="{% url 'admin_panel:dashboard' %}">返回概览</a>
    <a class="btn btn-outline-primary" href="?actor={{ actor }}&action={{ action }}&target={{ target }}&start={{ start }}&end={{ end }}&export=csv">导出 CSV</a>
  </div>
</div>

<form method="get" class="card mb-3">
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-2">
        <label class="form-label">操作人</label>
        <input type="text" name="actor" value="{{ actor }}" class="form-control" placeholder="用户名">
      </div>
      <div class="col-md-2">
        <label class="form-label">动作</label>
        <input type="text" name="action" value="{{ action }}" class="form-control" placeholder="如 upgrade_plan">
      </div>
      <div class="col-md-3">
        <label class="form-label">目标</label>
        <input type="text" name="target" value="{{ target }}" class="form-control" placeholder="域名或对象">
      </div>
      <div class="col-md-2">
        <label class="form-label">开始时间</label>
        <input type="text" name="start" value="{{ start }}" class="form-control" placeholder="YYYY-MM-DD">
      </div>
      <div class="col-md-2">
        <label class="form-label">结束时间</label>
        <input type="text" name="end" value="{{ end }}" class="form-control" placeholder="YYYY-MM-DD">
      </div>
      <div class="col-md-1 d-flex align-items-end">
        <button class="btn btn-primary w-100" type="submit">筛选</button>
      </div>
    </div>
  </div>
</form>

<div class="table-responsive">
  <table class="table table-sm align-middle">
    <thead>
      <tr>
        <th>时间</th>
        <th>操作人</th>
        <th>动作</th>
        <th>目标</th>
        <th>详情</th>
      </tr>
    </thead>
    <tbody>
      {% for r in page_obj.object_list %}
      <tr>
        <td>{{ r.created_at }}</td>
        <td>{% if r.actor %}{{ r.actor.username }}{% else %}<span class="text-muted">-</span>{% endif %}</td>
        <td><span class="badge text-bg-secondary">{{ r.action }}</span></td>
        <td>{{ r.target }}</td>
        <td>
          {% if r.detail %}
          <code class="small">{{ r.detail|truncatechars:200 }}</code>
          {% else %}
          <span class="text-muted">(无)</span>
          {% endif %}
        </td>
      </tr>
      {% empty %}
      <tr><td colspan="5" class="text-muted">暂无数据</td></tr>
      {% endfor %}
    </tbody>
  </table>
</div>

<nav>
  <ul class="pagination">
    {% if page_obj.has_previous %}
      <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}&actor={{ actor }}&action={{ action }}&target={{ target }}&start={{ start }}&end={{ end }}">上一页</a></li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">上一页</span></li>
    {% endif %}
    <li class="page-item active"><span class="page-link">{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</span></li>
    {% if page_obj.has_next %}
      <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}&actor={{ actor }}&action={{ action }}&target={{ target }}&start={{ start }}&end={{ end }}">下一页</a></li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">下一页</span></li>
    {% endif %}
  </ul>
</nav>
{% endblock %}