{% extends "base.html" %}
{% block title %}批量作词 #{{ batch.id }}{% endblock %}
{% block content %}
<div class="page-header">
    <h1>批量作词 #{{ batch.id }}</h1>
    <a href="{{ url_for('lyrics.index') }}" class="btn btn-sm">返回列表</a>
</div>

{% if batch.auto_compose %}
<div class="phase-steps" id="phase-steps">
    <div class="step" data-phase="1">① 作词</div>
    <div class="step" data-phase="2">② 作曲+导出</div>
    {% if batch.auto_publish %}
    <div class="step" data-phase="3">③ 发行</div>
    {% endif %}
</div>
{% endif %}

<div class="card-form">
    <div class="form-row">
        <div class="form-group">
            <label>状态</label>
            <span class="badge badge-{{ batch.status }}" id="batch-status">{{ batch.status }}</span>
            {% if batch.status in ('pending', 'running') %}
            <form method="post" action="{{ url_for('lyrics.batch_reset', batch_id=batch.id) }}"
                  onsubmit="return confirm('确认将此批次标记为失败？卡住的记录会释放，但不会重新执行。');"
                  style="display:inline; margin-left:0.8rem;">
                <button type="submit" class="btn btn-sm btn-danger">标记失败</button>
            </form>
            {% endif %}
        </div>
        <div class="form-group">
            <label>账号</label>
            <span>{{ batch.account_name or '-' }}</span>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group">
            <label>作词</label>
            <span id="batch-progress">{{ batch.completed_count }} / {{ batch.total_count }}</span>
            {% if batch.failed_count %}
            <span style="color: #f5222d; margin-left: 4px;">({{ batch.failed_count }}失败)</span>
            {% endif %}
        </div>
        {% if batch.auto_compose %}
        <div class="form-group">
            <label>作曲</label>
            <span id="batch-composed">{{ batch.composed_count or 0 }} 首</span>
        </div>
        <div class="form-group">
            <label>导出</label>
            <span id="batch-exported">{{ batch.exported_count or 0 }} 首</span>
            <span style="font-size:11px; color:#999; margin-left:4px;">(每首可生成多个音频)</span>
        </div>
        {% endif %}
        {% if batch.auto_publish %}
        <div class="form-group">
            <label>发行</label>
            <span id="batch-published">{{ batch.published_count or 0 }} 首</span>
        </div>
        {% endif %}
    </div>

    {% if batch.status == 'running' or batch.status == 'pending' %}
    <div class="progress-bar" style="margin: 1rem 0;">
        <div class="progress-fill" id="progress-fill" style="width: {{ (batch.completed_count / batch.total_count * 100) if batch.total_count else 0 }}%"></div>
    </div>
    {% endif %}

    <div class="form-row">
        <div class="form-group">
            <label>模板</label>
            <span>{% if batch.template_file.startswith('category:') %}{{ batch.template_file[9:] }}(随机){% else %}{{ batch.template_file.split('/')[-1].split('\\')[-1] }}{% endif %}</span>
        </div>
        <div class="form-group">
            <label>语言</label>
            <span>{{ batch.language or '中文' }}</span>
        </div>
        <div class="form-group">
            <label>作词模型</label>
            <span>{{ batch.model }}</span>
        </div>
        {% if batch.hashtag %}
        <div class="form-group">
            <label>话题</label>
            <span style="color:#1890ff;">#{{ batch.hashtag }}</span>
        </div>
        {% endif %}
    </div>

    {% if batch.auto_compose %}
    <div class="form-row">
        <div class="form-group">
            <label>作曲模型</label>
            <span>{{ batch.compose_model }}</span>
        </div>
        {% if batch.auto_publish %}
        <div class="form-group">
            <label>授权类型</label>
            <span>{{ '独家授权' if batch.auth_type == 'exclusive' else '独家代理授权' }}</span>
        </div>
        {% endif %}
    </div>
    {% endif %}

    {% if batch.error_message %}
    <div class="form-group">
        <label>错误</label>
        <p style="color: #f5222d;">{{ batch.error_message }}</p>
    </div>
    {% endif %}

    <div class="form-row">
        <div class="form-group">
            <label>创建时间</label>
            <span>{{ batch.created_at[:19] }}</span>
        </div>
        {% if batch.finished_at %}
        <div class="form-group">
            <label>完成时间</label>
            <span>{{ batch.finished_at[:19] }}</span>
        </div>
        {% endif %}
    </div>
</div>

{% if generations %}
<h2 style="margin-top: 2rem;">生成结果</h2>
<table>
    <thead>
        <tr>
            <th>#</th>
            <th>标题</th>
            <th>作词</th>
            {% if batch.auto_compose %}<th>作曲</th><th>导出</th>{% endif %}
            <th>备注</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        {% for g in generations %}
        <tr>
            <td>{{ loop.index }}</td>
            <td>{{ g.title or '-' }}</td>
            <td>
                {% if g.status == 'completed' %}
                    <span class="badge badge-completed">completed</span>
                {% elif g.status == 'failed' %}
                    <span class="badge badge-failed">failed</span>
                {% else %}
                    <span class="badge badge-{{ g.status }}">{{ g.status }}</span>
                {% endif %}
            </td>
            {% if batch.auto_compose %}
            <td>
                {% if g.status != 'completed' %}
                    <span style="color:#ccc;">-</span>
                {% elif g.compose_display == 'completed' %}
                    <span class="badge badge-completed">completed</span>
                {% elif g.compose_display == 'timeout' %}
                    <span class="badge" style="background:#fff1f0; color:#cf1322; border:1px solid #ffa39e;">timeout</span>
                {% elif g.compose_display == 'failed' %}
                    <span class="badge badge-failed">failed</span>
                {% elif g.compose_display == 'skipped' %}
                    <span class="badge" style="background:#fff7e6; color:#d46b08; border:1px solid #ffd591;">skipped</span>
                {% elif not g.compose_display %}
                    <span style="color:#ccc;">待处理</span>
                {% endif %}
            </td>
            <td>
                {% if g.status != 'completed' or g.compose_display not in ('completed',) %}
                    <span style="color:#ccc;">-</span>
                {% elif g.export_status == 'completed' %}
                    <span class="badge badge-completed">completed</span>
                {% elif g.export_status == 'failed' %}
                    <span class="badge badge-failed">failed</span>
                {% elif not g.export_status %}
                    <span style="color:#ccc;">待处理</span>
                {% endif %}
            </td>
            {% endif %}
            <td style="max-width:220px; font-size:12px;">
                {% if g.status == 'failed' and g.error_message %}
                    <span style="color:#f5222d;" title="{{ g.error_message }}">{{ (g.error_message or '')[:60] }}{% if (g.error_message or '')|length > 60 %}...{% endif %}</span>
                {% elif g.compose_note %}
                    <span style="color:#d46b08;">{{ g.compose_note }}</span>
                {% elif g.compose_display == 'failed' %}
                    <span style="color:#f5222d;">作曲失败</span>
                {% elif g.compose_display == 'timeout' %}
                    <span style="color:#cf1322;">作曲超时(服务器可能仍在处理)</span>
                {% elif g.export_status == 'failed' %}
                    <span style="color:#f5222d;">导出失败</span>
                {% endif %}
            </td>
            <td>
                {% if g.status == 'completed' %}
                <a href="{{ url_for('lyrics.detail', lyrics_id=g.id) }}" class="btn btn-sm">查看</a>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

{% if batch.status in ('completed', 'failed') and batch.auto_compose %}
<div style="margin-top: 1rem; display: flex; gap: 8px;">
    {% set has_compose_failed = generations | selectattr('compose_display', 'equalto', 'failed') | list | length > 0 %}
    {% set has_compose_timeout = generations | selectattr('compose_display', 'equalto', 'timeout') | list | length > 0 %}
    {% set has_compose_pending = generations | selectattr('compose_display', 'equalto', '') | selectattr('status', 'equalto', 'completed') | list | length > 0 %}
    {% set has_export_failed = generations | selectattr('export_status', 'equalto', 'failed') | list | length > 0 %}
    {% set has_lyrics_failed = generations | selectattr('status', 'equalto', 'failed') | list | length > 0 %}
    {% set has_lyrics_pending = generations | selectattr('status', 'equalto', 'pending') | list | length > 0 %}
    {% if has_lyrics_failed or has_lyrics_pending %}
    <form method="post" action="{{ url_for('lyrics.batch_retry', batch_id=batch.id) }}" style="display:inline"
          onsubmit="return confirm('将重试本批次所有失败/挂起的作词条目（自动截断超过500字的 prompt）。继续？');">
        <input type="hidden" name="phase" value="lyrics">
        <button class="btn btn-sm" style="background:#eb2f96; color:#fff; border-color:#eb2f96;">重试作词</button>
    </form>
    {% endif %}
    {% if has_compose_failed or has_compose_pending or has_compose_timeout %}
    <form method="post" action="{{ url_for('lyrics.batch_retry', batch_id=batch.id) }}" style="display:inline">
        <input type="hidden" name="phase" value="compose">
        <button class="btn btn-sm" style="background:#fa8c16; color:#fff; border-color:#fa8c16;">重试作曲+导出</button>
    </form>
    {% endif %}
    {% if has_export_failed %}
    <form method="post" action="{{ url_for('lyrics.batch_retry', batch_id=batch.id) }}" style="display:inline">
        <input type="hidden" name="phase" value="export">
        <button class="btn btn-sm" style="background:#1890ff; color:#fff; border-color:#1890ff;">重试导出</button>
    </form>
    {% endif %}
    {% if batch.auto_publish and (batch.exported_count or 0) > (batch.published_count or 0) %}
    <form method="post" action="{{ url_for('lyrics.batch_retry', batch_id=batch.id) }}" style="display:inline">
        <input type="hidden" name="phase" value="publish">
        <button class="btn btn-sm" style="background:#fa8c16; color:#fff; border-color:#fa8c16;">重试发行</button>
    </form>
    {% endif %}
</div>
{% endif %}
{% endif %}
{% endblock %}

{% block scripts %}
<script type="application/json" id="page-data">
{
    "statusUrl": "{{ url_for('lyrics.batch_status', batch_id=batch.id) }}",
    "completedCount": {{ batch.completed_count or 0 }},
    "totalCount": {{ batch.total_count or 1 }},
    "composedCount": {{ batch.composed_count or 0 }},
    "exportedCount": {{ batch.exported_count or 0 }},
    "publishedCount": {{ batch.published_count or 0 }}
}
</script>
<script src="{{ url_for('static', filename='js/lyrics_batch_detail.js') }}"></script>
{% endblock %}
