{% extends "base.html" %}
{% block title %}发行批次 #{{ batch.id }}{% endblock %}
{% block content %}
<div class="page-header">
    <h1>📦 发行批次 #{{ batch.id }}</h1>
    <div style="display: flex; gap: 8px; align-items: center;">
        {% if batch.failed_count > 0 %}
        <form action="{{ url_for('publish.batch_retry', batch_id=batch.id) }}" method="POST" style="margin:0;">
            <button type="submit" class="btn btn-sm" style="background:#e67e22; border-color:#d35400; color:#fff;" onclick="return confirm('确定要重新提交此批次下所有失败的发行任务吗？');">🔄 重新提交失败任务</button>
        </form>
        {% endif %}
        <a href="{{ url_for('publish.index') }}" class="btn btn-sm">← 返回</a>
    </div>
</div>

<div class="task-form" style="max-width:none;">
    <div style="display:flex; gap:24px; flex-wrap:wrap; margin-bottom:1rem;">
        <div><strong>账号：</strong> {{ batch.account_name or batch.account_id }}</div>
        <div><strong>进度：</strong> <span>{{ batch.completed_count }}/{{ batch.total_count }}</span></div>
        <div><strong>失败：</strong> <span>{{ batch.failed_count }}</span></div>
        <div><strong>状态：</strong> <span class="badge badge-{{ batch.status }}">{{ batch.status }}</span></div>
        <div><strong>创建时间：</strong> {{ batch.created_at[:16].replace('T', ' ') if batch.created_at else '' }}</div>
    </div>

    {% if batch.error_message %}
    <div class="flash flash-error" style="margin-bottom:1rem;">{{ batch.error_message }}</div>
    {% endif %}

    <table class="data-table" style="margin-top:12px;">
        <thead>
            <tr><th>任务ID</th><th>作品/ClipID</th><th>标题</th><th>授权类型</th><th>当前阶段</th><th>状态</th><th>手机</th><th>创建时间</th><th>操作</th></tr>
        </thead>
        <tbody>
        {% for j in jobs %}
            <tr>
                <td>{{ j.id }}</td>
                <td>
                    {% if j.is_ai_created %}
                    <span style="color:#1890ff; font-weight:600;">🎼 AI</span> ({{ j.source_clip_id[:8] if j.source_clip_id else '-' }})
                    {% else %}
                    {{ j.issue_id or '-' }}
                    {% endif %}
                </td>
                <td>{{ j.title or '-' }}</td>
                <td>
                    {% if j.auth_type == 'exclusive_agent' %}代理
                    {% elif j.auth_type == 'exclusive' %}独家
                    {% else %}-{% endif %}
                </td>
                <td><code>{{ j.phase }}</code></td>
                <td><span class="badge badge-{{ j.status }}">{{ j.status }}</span></td>
                <td>{{ j.masked_phone or '-' }}</td>
                <td>{{ j.created_at[:16].replace('T', ' ') if j.created_at else '' }}</td>
                <td><a href="{{ url_for('publish.detail', job_id=j.id) }}" class="btn btn-sm">详情</a></td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}
