# -*- coding: utf-8 -*-
"""第2章《软件项目启动》HTML 课件生成器（v3：28 → 48 页扩充版）。
用法: python3 _build2.py
结构: lecture2/2.1.html ~ 2.48.html + lecture2/index.html（13 个板块的目录）
内容: _content2a.py(1–16) + _content2b.py(17–32) + _content2c.py(33–48)
依据: 《第2章-课件扩充方案（28→48页）.md》
说明: ★ = 课后拓展页（供课后复盘与作业），共 8 页（15/18/19/21/33/35/41/45）
"""
import os, sys

ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, ROOT)

from _build import slide_page, esc, CSS_VER

from _content2a import SLIDES_A
from _content2b import SLIDES_B
from _content2c import SLIDES_C

SLIDES = SLIDES_A + SLIDES_B + SLIDES_C
STAR_PAGES = {15, 18, 19, 21, 33, 35, 41, 45}

# 13 个板块（与扩充方案一致）
GROUPS = [
    ('一、封面 · 学习导航 · 启动五问', 1, 3),
    ('二、案例导入：智能库存管理系统', 4, 6),
    ('三、01 软件项目启动概述', 7, 9),
    ('四、02 项目立项（建议书 · 可研 · 决策）', 10, 12),
    ('五、项目建议书', 13, 15),
    ('六、项目可行性研究', 16, 21),
    ('七、项目评估与决策', 22, 23),
    ('八、03 项目准备工作 · 项目经理', 24, 26),
    ('九、组织结构选择（含 PMO）', 27, 30),
    ('十、04 识别项目干系人（含方格 · RACI）', 31, 35),
    ('十一、05 制定项目章程', 36, 41),
    ('十二、06 项目启动大会', 42, 45),
    ('十三、课堂练习 · 本章小结 · 作业', 46, 48),
]


def build():
    total = len(SLIDES)
    assert total == 48, f'页数应为 48，实际 {total}'
    folder = os.path.join(ROOT, 'lecture2')
    os.makedirs(folder, exist_ok=True)
    for i, (pt, title, body) in enumerate(SLIDES, start=1):
        prev, nxt = i - 1, i + 1
        pt_prev = SLIDES[prev - 1][0] if prev >= 1 else ''
        pt_next = SLIDES[nxt - 1][0] if nxt <= total else ''
        href_prev = f'2.{prev}.html' if prev >= 1 else ''
        href_next = f'2.{nxt}.html' if nxt <= total else ''
        star = '★' if i in STAR_PAGES else ''
        with open(os.path.join(folder, f'2.{i}.html'), 'w', encoding='utf-8') as f:
            f.write(slide_page(2, i, total, f'{title}{star}', body, pt_prev, pt_next,
                               href_prev, href_next, '软件项目管理 · 第 2 次课'))

    items = []
    for name, s, e in GROUPS:
        items.append(f'    <div class="toc-section">{esc(name)} <em>第 {s}-{e} 页</em></div>')
        for i in range(s, e + 1):
            pt, title, _ = SLIDES[i - 1]
            star = ' <span class="toc-star">★课后</span>' if i in STAR_PAGES else ''
            items.append(f'    <a class="toc-item" href="2.{i}.html"><span class="toc-num">2.{i}</span>'
                         f'<span class="toc-title">{esc(title)}{star}</span><span class="toc-arrow">→</span></a>')
    n_star = len(STAR_PAGES)
    with open(os.path.join(folder, 'index.html'), 'w', encoding='utf-8') as f:
        f.write(f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>第2次课 — 目录</title>
<link rel="stylesheet" href="../styles.css?v={CSS_VER}">
<style>
.toc-section{{margin:24px 0 8px;padding:8px 14px;background:linear-gradient(135deg,#1a365d,#2b6cb0);color:#fff;border-radius:8px;font-size:1.05rem;font-weight:700;letter-spacing:.5px;}}
.toc-section:first-of-type{{margin-top:12px;}}
.toc-section em{{font-style:normal;font-weight:400;opacity:.85;font-size:.9rem;margin-left:6px;}}
.toc-star{{display:inline-block;margin-left:8px;font-size:.72em;color:#744210;background:#fefcbf;border-radius:8px;padding:1px 7px;vertical-align:1px;}}
</style>
</head>
<body>

<div class="logo-bar">
  <a class="brand-link" href="../index.html" title="返回课程主页（16 次课总目录）"><img src="../logo.png" alt="Logo"></a>
  <span class="logo-label">软件项目管理 · 第 2 次课</span>
</div>

<div class="breadcrumb">
  <a href="../index.html">← 返回主页</a>
</div>

<div class="toc-page">
  <h2>🕒 第 2 次课（第 2 周）：软件项目启动</h2>
  <div class="toc-subtitle">{total} 张幻灯片 · 13 个板块 · 其中 <b>{n_star} 页为 ★课后拓展</b>（供课后复盘与作业）</div>
  <div class="toc-subtitle">点击任意标题直接跳转（键盘 ← → 或滚轮翻页；任意页按 <b>Esc</b> 可回到本目录）</div>
  <div class="toc-subtitle">👩‍🏫 授课教师：周宇文 ｜ 联系电话/微信：13580390715</div>

  <div class="toc-progress">
    <span>0/{total}</span>
    <div class="progress-bar"><div class="progress-fill" style="width:0%"></div></div>
  </div>

  <div class="toc-list">

{chr(10).join(items)}

  </div>
</div>
</body>
</html>''')
    print(f'lecture2 v3 built: {total} slides（含 {n_star} 页★课后）+ toc（{len(GROUPS)} 板块）')
    print('  ★课后页:', '、'.join(f'2.{i}' for i in sorted(STAR_PAGES)))


if __name__ == '__main__':
    build()
