{% extends "base.html" %}
{% block title %}汽水任务{% endblock %}
{% block content %}
<div class="page-header">
    <h1>汽水任务</h1>
</div>

<div class="task-form" style="max-width:none;">
    <h3>批量作词</h3>
    <div id="batch-form"
         data-batch-create-url="{{ url_for('lyrics.batch_create_multi') }}"
         data-index-url="{{ url_for('lyrics.index') }}">
        <div class="form-row">
            <div class="form-group">
                <label>账号</label>
                <select id="batch-account" required>
                    <option value="">-- 选择账号 --</option>
                    {% for acc in accounts %}
                    <option value="{{ acc.id }}">{{ acc.name }}{% if acc.is_logged_in %} (已登录){% endif %}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="form-group">
                <label>作词模型</label>
                <select id="batch-model">
                    <option value="gemini-3f" selected>织韵 (Gemini-3-Flash)</option>
                    <option value="xiyan-v1">夕言 (Doubao-Seed-1.8)</option>
                    <option value="lengjing-v1">棱镜 (Doubao-Seed-1.8)</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label>作词模板 <span style="font-weight:normal; color:#999; font-size:12px;">（可多选；点击"添加到队列"会按当前数量为每个所选模板各生成一个批次）</span></label>
            <div class="template-groups">
                {% set categories = [] %}
                {% set ns = namespace(by_cat={}) %}
                {% for t in templates %}
                    {% if t.category not in ns.by_cat %}
                        {% set _ = ns.by_cat.update({t.category: []}) %}
                    {% endif %}
                    {% set _ = ns.by_cat[t.category].append(t) %}
                {% endfor %}
                {% for category, items in ns.by_cat.items() %}
                <div class="template-cat">
                    <div class="template-cat-title">
                        {{ category }}
                        {% if items|length > 1 %}
                        <label class="template-cat-random">
                            <input type="checkbox" name="template_file" value="category:{{ category }}" data-name="{{ category }}(随机)" data-batch-count="2"> 随机
                        </label>
                        {% endif %}
                    </div>
                    <div class="template-grid">
                        {% for t in items %}
                        <label class="template-card">
                            <input type="checkbox" name="template_file" value="{{ t.path }}" data-name="{{ t.name }}" data-batch-count="{{ t.batch_count }}">
                            <div class="template-card-body">
                                <div class="template-card-name">{{ t.name }}</div>
                                <div class="template-card-desc">{{ t.description or '&nbsp;'|safe }}</div>
                                <div class="template-card-meta">{{ t.filename.removesuffix('.json') }}</div>
                            </div>
                        </label>
                        {% endfor %}
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>

        <div style="display:flex; gap:12px; align-items:flex-end; flex-wrap:wrap;">
            <div class="form-group" style="margin-bottom:0;">
                <label>数量</label>
                <input type="number" id="batch-count-input" value="2" min="1" max="50" style="width:70px;">
            </div>
            <div class="form-group" style="margin-bottom:0;">
                <label>语言</label>
                <select id="batch-language">
                    <option value="中文" selected>中文</option>
                    <option value="粤语">粤语</option>
                    <option value="英语">英语</option>
                    <option value="韩语">韩语</option>
                    <option value="日语">日语</option>
                </select>
            </div>
            <div class="form-group" style="margin-bottom:0;">
                <label>人声</label>
                <select id="batch-voice">
                    <option value="随机" selected>随机</option>
                    <option value="男声">男声</option>
                    <option value="女声">女声</option>
                </select>
            </div>
            <div class="form-group" style="margin-bottom:0;">
                <label>曲风</label>
                <select id="batch-style">
                    <option value="" selected>无</option>
                    <option value="Pop">Pop</option>
                    <option value="R&amp;B">R&amp;B</option>
                    <option value="民谣">民谣</option>
                    <option value="摇滚">摇滚</option>
                    <option value="Hip-hop">Hip-hop</option>
                    <option value="电子">电子</option>
                    <option value="古风">古风</option>
                    <option value="爵士">爵士</option>
                    <option value="Lo-fi">Lo-fi</option>
                    <option value="乡村">乡村</option>
                    <option value="民族">民族</option>
                    <option value="抒情">抒情</option>
                    <option value="儿歌">儿歌</option>
                    <option value="朋克">朋克</option>
                </select>
            </div>
            <div class="form-group" style="margin-bottom:0;">
                <label>话题</label>
                <select id="batch-hashtag">
                    <option value="">不选择话题</option>
                </select>
            </div>
            <div class="form-group" style="margin-bottom:0;">
                <label>&nbsp;</label>
                <button type="button" class="btn btn-primary" id="add-to-queue-btn">添加到队列</button>
            </div>
        </div>

        <!-- Batch queue display -->
        <div id="batch-queue-section" style="display:none; margin-top: 1rem; padding: 1rem; background: #f6f8fa; border-radius: 8px; border: 1px solid #e1e4e8;">
            <h4 style="margin: 0 0 0.5rem 0; font-size: 14px; color: #333;">待提交队列</h4>
            <table style="width:100%; font-size: 13px;" id="batch-queue-table">
                <thead>
                    <tr><th>#</th><th>模板</th><th>数量</th><th>语言</th><th>人声</th><th>曲风</th><th>话题</th><th>操作</th></tr>
                </thead>
                <tbody></tbody>
            </table>
            <div style="margin-top: 8px; font-size: 12px; color: #666;" id="queue-summary"></div>
        </div>



        <div style="margin-top: 1rem; padding: 1rem; background: #f0f7ff; border-radius: 8px; border: 1px solid #d0e3ff;">
            <label style="display: flex; align-items: center; gap: 8px; cursor: pointer; font-weight: bold;">
                <input type="checkbox" value="1" checked id="auto-compose-toggle">
                作词完成后自动作曲 + 导出到资产
            </label>
            <div id="compose-options" style="margin-top: 0.8rem;">
                <div class="form-group" style="margin-bottom: 0;">
                    <label>作曲模型</label>
                    <select id="compose-model-select">
                        <option value="7" selected>Sway v5.5</option>
                    </select>
                </div>
            </div>
        </div>

        <div style="margin-top: 1rem; padding: 1rem; background: {% if is_authorized %}#f0fff4{% else %}#fafafa{% endif %}; border-radius: 8px; border: 1px solid {% if is_authorized %}#b7ebc9{% else %}#e8e8e8{% endif %};">
            <label style="display: flex; align-items: center; gap: 8px; cursor: {% if is_authorized %}pointer{% else %}not-allowed{% endif %}; font-weight: bold; color: {% if is_authorized %}var(--text-primary){% else %}var(--text-muted){% endif %};">
                <input type="checkbox" value="1" id="auto-publish-toggle" {% if not is_authorized %}disabled{% endif %}>
                导出后自动发行 {% if not is_authorized %}<span class="badge badge-failed" style="font-weight: normal; margin-left: 6px;">赞助激活版专享</span>{% endif %}
            </label>
            <p style="margin: 0.5rem 0 0 26px; font-size: 12px; color: var(--text-muted);">
                {% if is_authorized %}需要已配置 SmsForwarder 接收验证码。每首歌发行时会等待短信验证。{% else %}自动把导出的歌曲安排到后台静默流程自动对接抖音发行（免费版需前往「发行管理」手动操作）。{% endif %}
            </p>
            <div id="publish-options" style="margin-top: 0.8rem; display: none;">
                <div class="form-row">
                    <div class="form-group" style="margin-bottom: 0;">
                        <label>授权类型</label>
                        <select id="batch-auth-type">
                            <option value="exclusive" selected>独家授权</option>
                            <option value="exclusive_agent">独家代理授权</option>
                        </select>
                    </div>
                    <div class="form-group" id="batch-region-group" style="margin-bottom: 0; display: none;">
                        <label>发行范围</label>
                        <select id="batch-auth-region">
                            <option value="1" selected>全球</option>
                            <option value="2">中国（含港澳台）</option>
                            <option value="3">仅中国大陆</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <button type="button" class="btn btn-primary btn-lg" id="submit-queue-btn" disabled>请先添加批次到队列</button>
    </div>
</div>

<div style="margin-top: 1.5rem; padding: 1rem; background: #f8f9fa; border-radius: 8px; font-size: 13px; color: #666;">
    <strong>说明</strong>
    每个模板内置 12-18 个随机变量（含<b>乐器编排</b>/<b>声线质感</b>/<b>编排密度</b>），同模板每次生成都不同。批量作词会按变量笛卡尔积均匀采样，并把组合直接写入 prompt，作曲模型读到风格描述时能落到具体音色。失败的歌词会自动重试一次。
</div>

{% if batches %}
<h2 style="margin-top: 2rem;">批量任务</h2>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>账号</th>
            <th>模板</th>
            <th>语言</th>
            <th>话题</th>
            <th>作词进度</th>
            <th>作曲/导出</th>
            <th>状态</th>
            <th>创建时间</th>
        </tr>
    </thead>
    <tbody>
        {% for b in batches %}
        <tr>
            <td><a href="{{ url_for('lyrics.batch_detail', batch_id=b.id) }}">{{ b.id }}</a></td>
            <td>{{ b.account_name or '-' }}</td>
            <td>{{ b.template_display }}</td>
            <td>{{ b.language or '中文' }}</td>
            <td>{{ ('#' + b.hashtag) if b.hashtag else '-' }}</td>
            <td>{{ b.completed_count }}/{{ b.total_count }}{% if b.failed_count %} <span style="color:#f5222d;">({{ b.failed_count }}失败)</span>{% endif %}</td>
            <td>{% if b.auto_compose %}{{ b.composed_count or 0 }}曲 / {{ b.exported_count or 0 }}导出{% else %}-{% endif %}</td>
            <td><span class="badge badge-{{ b.status }}">{{ b.status }}</span></td>
            <td>{{ b.created_at[:16] }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endif %}

{% endblock %}

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