{% extends "base.html" %}
{% block title %}发行管理{% endblock %}
{% block content %}
<div class="page-header">
    <h1>发行管理</h1>
</div>

<div class="tab-nav">
    <a href="{{ url_for('publish.index') }}" class="active"><i class="ph ph-paper-plane-tilt"></i>发行汽水作品</a>
    <a href="{{ url_for('publish.ai_create') }}"><i class="ph ph-waveform"></i>发行SUNO作品</a>
</div>

<div class="card-form">
    <h3>提交发行</h3>
    <form id="publish-form" method="POST" action="{{ url_for('publish.start') }}"
          data-works-url="{{ url_for('publish.works_json') }}">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <div class="form-row">
            <div class="form-group">
                <label>账号</label>
                <select name="account_id" id="account-select" required>
                    <option value="">请选择账号</option>
                    {% for acc in accounts %}
                    <option value="{{ acc.id }}" {% if acc.is_logged_in %}data-logged="1"{% endif %}>{{ acc.name }}{% if not acc.is_logged_in %} (未登录){% endif %}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="form-group" style="align-self: end;">
                <button type="button" id="reload-btn" class="btn"><i class="ph ph-arrows-counter-clockwise"></i> 刷新作品列表</button>
            </div>
        </div>

        <div id="works-tabs-container" class="tab-nav" style="display:none; margin-top: 1.5rem; margin-bottom: 16px;">
            <button type="button" class="works-tab active" data-type="1">我的作品</button>
            <button type="button" class="works-tab" data-type="2">歌曲</button>
        </div>

        <div id="works-area">
            <p style="color:#999;">选择账号后自动加载...</p>
        </div>

        <div class="form-group" style="margin-top: 1.5rem; display: flex; align-items: center; gap: 16px;">
            <button type="submit" class="btn btn-primary" id="submit-btn" disabled>提交发行 (0 首)</button>
            <span style="color: var(--text-muted); font-size: 13px; display: inline-flex; align-items: center; gap: 4px;">
                <i class="ph ph-info" style="font-size: 16px;"></i> 短信验证码需转发到 <code>/api/sms/webhook</code>
            </span>
        </div>
    </form>
</div>

<div class="card-form">
    <div style="display:flex; justify-content:space-between; align-items:center;">
        <h3 style="margin:0;">发行批次历史</h3>
        <span style="color:#666; font-size:13px;">共 {{ total_batches }} 个批次</span>
    </div>
    {% if batches %}
    <table class="data-table" style="margin-top:12px;">
        <thead>
            <tr><th>批次ID</th><th>账号</th><th>作品数量</th><th>成功/失败</th><th>状态</th><th>创建时间</th><th>操作</th></tr>
        </thead>
        <tbody>
        {% for b in batches %}
            <tr>
                <td>{{ b.id }}</td>
                <td>{{ b.account_name or b.account_id }}</td>
                <td>{{ b.total_count }}</td>
                <td>
                    <span style="color:#2563eb;">{{ b.completed_count }}</span>
                    /
                    <span style="color:#ef4444;">{{ b.failed_count }}</span>
                </td>
                <td><span class="badge badge-{{ b.status }}">{{ b.status }}</span></td>
                <td>{{ b.created_at[:16].replace('T', ' ') if b.created_at else '' }}</td>
                <td><a href="{{ url_for('publish.batch_detail', batch_id=b.id) }}" class="btn btn-sm">详情</a></td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    {% if total_pages > 1 %}
    <div style="display:flex; justify-content:center; align-items:center; gap:8px; margin-top:16px; font-size:13px;">
        {% if page > 1 %}
        <a href="{{ url_for('publish.index', page=page-1) }}" class="btn btn-sm">上一页</a>
        {% endif %}
        {% for p in range(1, total_pages + 1) %}
            {% if p == page %}
            <span style="padding:4px 10px; background:#1890ff; color:#fff; border-radius:4px; font-weight:600;">{{ p }}</span>
            {% elif p <= 3 or p >= total_pages - 2 or (p >= page - 2 and p <= page + 2) %}
            <a href="{{ url_for('publish.index', page=p) }}" class="btn btn-sm">{{ p }}</a>
            {% elif p == 4 or p == total_pages - 3 %}
            <span style="color:#999;">...</span>
            {% endif %}
        {% endfor %}
        {% if page < total_pages %}
        <a href="{{ url_for('publish.index', page=page+1) }}" class="btn btn-sm">下一页</a>
        {% endif %}
    </div>
    {% endif %}
    {% else %}
    <p style="color:#999; margin-top:12px;">暂无发行任务。</p>
    {% endif %}
</div>

{% endblock %}

{% block scripts %}
<script src="{{ url_for('static', filename='js/publish.js') }}"></script>
{% endblock %}
