<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ exam.title }} - 打印版</title>
    <style>
        @media print { .no-print { display: none !important; } body { margin: 0; } }
        body { font-family: 'SimSun', 'STSong', serif; color: #000; background: #fff; max-width: 210mm; margin: 0 auto; padding: 20mm 15mm; font-size: 14px; line-height: 1.8; }
        .header { text-align: center; border-bottom: 3px double #000; padding-bottom: 15px; margin-bottom: 20px; }
        .header h1 { font-size: 22px; margin: 0 0 8px 0; letter-spacing: 2px; }
        .header .info { font-size: 12px; color: #333; }
        .header .info span { margin: 0 15px; }
        .student-info { border: 1px solid #000; padding: 10px 15px; margin-bottom: 20px; font-size: 13px; }
        .student-info span { margin-right: 40px; }
        .student-info .blank { display: inline-block; width: 120px; border-bottom: 1px solid #000; }
        .section-title { font-size: 16px; font-weight: bold; margin: 25px 0 15px 0; padding-left: 5px; border-left: 4px solid #000; }
        .question { margin-bottom: 18px; page-break-inside: avoid; }
        .question .q-num { font-weight: bold; }
        .question .q-score { float: right; font-size: 12px; color: #555; }
        .options { margin: 8px 0 0 25px; }
        .options .opt { margin-bottom: 4px; }
        .answer-area { margin: 8px 0 0 25px; }
        .answer-line { border-bottom: 1px solid #ccc; height: 30px; margin-bottom: 2px; }
        .footer { text-align: center; margin-top: 30px; font-size: 12px; color: #666; border-top: 1px solid #ccc; padding-top: 10px; }
        .print-btn { position: fixed; top: 20px; right: 20px; padding: 10px 24px; background: #3b82f6; color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; z-index: 100; }
        .print-btn:hover { background: #2563eb; }
    </style>
</head>
<body>
    <button class="print-btn no-print" onclick="window.print()">打印 / 导出PDF</button>
    <div class="header">
        <h1>{{ exam.title }}</h1>
        <div class="info">
            <span>科目：{{ exam.subject }}</span>
            <span>考试时长：{{ exam.duration }}分钟</span>
            <span>满分：{{ exam.total_score }}分</span>
            <span>出题人：{{ exam.creator.name if exam.creator else '' }}</span>
        </div>
    </div>
    <div class="student-info">
        <span>姓名：<span class="blank"></span></span>
        <span>考号：<span class="blank"></span></span>
        <span>得分：<span class="blank"></span></span>
    </div>

    {% set ns = namespace(choice_idx=0, fill_idx=0, text_idx=0) %}
    {% set choices = [] %}
    {% set fills = [] %}
    {% set texts = [] %}
    {% for q in questions %}
        {% if q.type == 'choice' %}{% if choices.append(q) %}{% endif %}
        {% elif q.type == 'fill' %}{% if fills.append(q) %}{% endif %}
        {% else %}{% if texts.append(q) %}{% endif %}
        {% endif %}
    {% endfor %}

    {% if choices|length > 0 %}
    <div class="section-title">一、选择题（共{{ choices|length }}题）</div>
    {% for q in choices %}
    <div class="question">
        <span class="q-num">{{ loop.index }}.</span> {{ q.content }}
        <span class="q-score">（{{ q.score }}分）</span>
        {% if q.options %}
        <div class="options">
            {% for opt in q.options %}
            <div class="opt">{{ ['A','B','C','D'][loop.index0] }}. {{ opt }}</div>
            {% endfor %}
        </div>
        {% endif %}
    </div>
    {% endfor %}
    {% endif %}

    {% if fills|length > 0 %}
    <div class="section-title">二、填空题（共{{ fills|length }}题）</div>
    {% for q in fills %}
    <div class="question">
        <span class="q-num">{{ loop.index }}.</span> {{ q.content }}
        <span class="q-score">（{{ q.score }}分）</span>
        <div class="answer-area">
            <div class="answer-line"></div>
        </div>
    </div>
    {% endfor %}
    {% endif %}

    {% if texts|length > 0 %}
    <div class="section-title">三、解答题（共{{ texts|length }}题）</div>
    {% for q in texts %}
    <div class="question">
        <span class="q-num">{{ loop.index }}.</span> {{ q.content }}
        <span class="q-score">（{{ q.score }}分）</span>
        <div class="answer-area">
            {% for i in range(8) %}
            <div class="answer-line"></div>
            {% endfor %}
        </div>
    </div>
    {% endfor %}
    {% endif %}

    <div class="footer">--- 试卷结束 ---</div>
</body>
</html>