{% extends "base.html" %}
{% block title %}发行详情 #{{ job.id }}{% endblock %}
{% block content %}
<div class="page-header">
    <h1>发行任务 #{{ job.id }} — {{ job.title or '未命名' }}
        {% if job.is_ai_created %}
        <span style="font-size:13px; padding:2px 8px; background:#e6f7ff;
                     color:#1890ff; border-radius:4px; margin-left:8px;">🎼 SUNO 作品</span>
        {% endif %}
    </h1>
    <a href="{{ url_for('publish.index') }}" class="btn btn-sm">返回列表</a>
</div>

{% if job.is_ai_created and job.source_clip_id %}
<div class="card-form" style="margin-top:0;">
    <div style="display:flex; align-items:center; gap:12px;">
        <span style="color:#666;">来源 Suno Clip:</span>
        <code style="font-size:12px;">{{ job.source_clip_id }}</code>
    </div>
</div>
{% endif %}

<div class="card-form">
    <h3>运行状态</h3>
    <div class="form-row">
        <div class="form-group">
            <label>状态</label>
            <span class="badge badge-{{ job.status }}" id="status-badge"
                  data-status-url="{{ url_for('publish.status', job_id=job.id) }}">{{ job.status }}</span>
            {% if job.status not in ('completed', 'failed') %}
            <form method="post" action="{{ url_for('publish.reset', job_id=job.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>
            <code id="phase-text">{{ job.phase }}</code>
        </div>
        <div class="form-group">
            <label>手机号</label>
            <span id="masked-phone">{{ job.masked_phone or '-' }}</span>
        </div>
    </div>

    {% if job.error_message %}
    <div class="form-group" id="error-container">
        <label>错误信息</label>
        <p style="color: #f5222d; background: #fff1f0; padding: 0.5rem; border-radius: 4px;">{{ job.error_message }}</p>
    </div>
    {% endif %}
    <div class="form-group" id="js-error-container" style="display: none;">
        <label>错误信息</label>
        <p id="js-error-text" style="color: #f5222d; background: #fff1f0; padding: 0.5rem; border-radius: 4px;"></p>
    </div>
</div>

<div class="card-form">
    <h3>发行成果</h3>
    <div class="form-row">
        <div class="form-group">
            <label>专辑 ID (Album ID)</label>
            <strong id="album-id">{{ job.album_id or '-' }}</strong>
        </div>
        <div class="form-group">
            <label>歌曲 IDs (Song IDs)</label>
            <div id="song-ids">
                {% if job.song_ids_parsed %}
                    {% for sid in job.song_ids_parsed %}
                        <code style="display:block; margin-bottom:2px;">{{ sid }}</code>
                    {% endfor %}
                {% else %}
                    <span>-</span>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<div class="form-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;">
    <div class="card-form" style="margin-top: 0;">
        <h3>基本信息</h3>
        <div class="form-group">
            <label>账号 ID</label>
            <span>{{ job.account_id }}</span>
        </div>
        <div class="form-group">
            <label>作品标题</label>
            <span>{{ job.title or '-' }}</span>
        </div>
        <div class="form-group">
            <label>签约模式</label>
            <span>
                {% if job.auth_type == 'exclusive_agent' %}独家代理授权
                {% elif job.auth_type == 'exclusive' %}独家授权（3年）
                {% else %}{{ job.auth_type or '-' }}{% endif %}
            </span>
        </div>
        {% if job.auth_type == 'exclusive_agent' %}
        <div class="form-group">
            <label>授权范围</label>
            <span>
                {% if job.auth_region == 1 %}全球
                {% elif job.auth_region == 2 %}中国（含港澳台）
                {% elif job.auth_region == 3 %}仅中国大陆
                {% else %}{{ job.auth_region or '-' }}{% endif %}
            </span>
        </div>
        {% endif %}
        <div class="form-group">
            <label>Issue ID</label>
            <code>{{ job.issue_id }}</code>
        </div>
        <div class="form-group">
            <label>Task ID</label>
            <code>{{ job.task_id }}</code>
        </div>
        <div class="form-group">
            <label>Work ID</label>
            <code>{{ job.work_id or '-' }}</code>
        </div>
    </div>

    <div class="card-form" style="margin-top: 0;">
        <h3>合同与追溯</h3>
        <div class="form-group">
            <label>合同包 ID (Contract Package)</label>
            <code>{{ job.contract_package_id or '-' }}</code>
        </div>
        <div class="form-group">
            <label>合同 ID (Contract ID)</label>
            <code>{{ job.contract_id or '-' }}</code>
        </div>
        <div class="form-group">
            <label>序列号 (Serial Number)</label>
            <code>{{ job.serial_number or '-' }}</code>
        </div>
        <div class="form-group">
            <label>签署 Token (Letsign)</label>
            <code style="word-break: break-all;">{{ job.letsign_token or '-' }}</code>
        </div>
    </div>
</div>

<div class="card-form">
    <h3>技术元数据</h3>
    <div class="form-row">
        <div class="form-group">
            <label>Cover URI</label>
            <code style="font-size: 11px;">{{ job.cover_uri or '-' }}</code>
        </div>
        <div class="form-group">
            <label>VOD CID</label>
            <code>{{ job.vod_cid or '-' }}</code>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group">
            <label>Artist ID</label>
            <code>{{ job.artist_id or '-' }}</code>
        </div>
        <div class="form-group">
            <label>Meta Artist ID</label>
            <code>{{ job.meta_artist_id or '-' }}</code>
        </div>
    </div>
</div>

<div class="card-form">
    <h3>时间戳</h3>
    <div class="form-row">
        <div class="form-group">
            <label>创建时间</label>
            <span>{{ job.created_at[:19] if job.created_at else '-' }}</span>
        </div>
        <div class="form-group">
            <label>开始时间</label>
            <span>{{ job.started_at[:19] if job.started_at else '-' }}</span>
        </div>
        <div class="form-group">
            <label>完成时间</label>
            <span>{{ job.finished_at[:19] if job.finished_at else '-' }}</span>
        </div>
    </div>
</div>
{% endblock %}

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