# -*- coding: utf-8 -*-
"""《软件项目管理》16 周 HTML 课件生成器（仿《第八章生成式人工智能》模板布局）。
用法: python3 _build.py
结构: index.html（课程首页·16 次大课卡片）
      lectureN/N.M.html（第 N 次课第 M 页）+ lectureN/index.html（本课目录）
说明: 第 1 次课内容来自 _content1/_content2（48 页已完成）；
      第 2–16 次课当前为占位页（仅 index.html，slides 待制作）。
"""
import os, html as _h

ROOT = os.path.dirname(os.path.abspath(__file__))
HOME_TITLE = '软件项目管理'
HOME_SUB = '广东金融学院 · 计算机学院'
HOME_PILL = '16 周 × 每周 1 次课（2 学时）｜ 32 学时 / 2 学分 ｜ 平时30%（作业20＋课堂讨论和练习10）＋ 期末70%（第18周统考）'

ROMAN = ['Ⅰ','Ⅱ','Ⅲ','Ⅳ','Ⅴ','Ⅵ','Ⅶ','Ⅷ','Ⅸ','Ⅹ','Ⅺ','Ⅻ','ⅩⅢ','ⅩⅣ','ⅩⅤ','ⅩⅥ']

def esc(s):
    return _h.escape(s, quote=False)

LECTURES = [
 dict(no=1, title='第1章 软件项目管理概述——华为鸿蒙OS开发项目管理实践案例', week='第1周',
      desc='项目/软件项目 → 软件项目管理（成败·环境·认证）→ 价值驱动的知识体系；以鸿蒙案例贯穿全书。', status='done'),
 dict(no=2, title='第2章 软件项目启动', week='第2周',
      desc='立项（建议书·可行性研究·评估与决策）、项目经理与组织结构、识别干系人、制定项目章程、项目启动会议。', status='todo'),
 dict(no=3, title='第3章 软件项目采购管理（一）', week='第3周',
      desc='采购概述与敏捷应用、规划采购管理（自制-外购分析、采购管理计划/SOW、供方选择标准）。', status='todo'),
 dict(no=4, title='第3章 软件项目采购管理（二）', week='第4周',
      desc='实施采购（招标/投标/评标）、控制采购、合同类型与合同管理过程。', status='todo'),
 dict(no=5, title='第4章 软件项目范围管理（一）', week='第5周',
      desc='范围概念与敏捷应用、规划范围/需求管理计划、收集需求（访谈/问卷/原型等）。', status='todo'),
 dict(no=6, title='第4章 软件项目范围管理（二）', week='第6周',
      desc='定义范围、创建 WBS、确认范围（验收）、控制范围（蔓延/镀金）、交付绩效域。', status='todo'),
 dict(no=7, title='第5章 软件项目进度管理（一）', week='第7周',
      desc='进度概念与敏捷应用、规划进度管理、定义活动、排列活动顺序（PDM 网络图）。', status='todo'),
 dict(no=8, title='第5章 软件项目进度管理（二）', week='第8周',
      desc='估算活动持续时间（三点估算）、制订进度计划：CPM 正逆推、时差、关键路径、进度压缩（计算专题）。', status='todo'),
 dict(no=9, title='第5章 软件项目进度管理（三）', week='第9周',
      desc='控制进度、开发方法和生命周期绩效域、进度综合案例＋ProjectLibre 演示。', status='todo'),
 dict(no=10, title='第6章 软件项目成本管理（一）', week='第10周',
      desc='成本概念与敏捷应用、规划成本管理、估算成本（类比/参数/自下而上、储备分析）。', status='todo'),
 dict(no=11, title='第6章 软件项目成本管理（二）', week='第11周',
      desc='制订成本预算（基准）、控制成本：挣值 EVM 全套公式与预测、度量绩效域。', status='todo'),
 dict(no=12, title='第7章 软件项目质量管理', week='第12周',
      desc='规划/管理/控制质量（七工具等）、配置管理、度量与交付绩效域。', status='todo'),
 dict(no=13, title='第8章 软件项目资源管理', week='第13周',
      desc='规划资源/估算活动资源/获取资源、团队建设（塔克曼）与团队管理（冲突五策略）、控制资源、团队绩效域。', status='todo'),
 dict(no=14, title='第9章 软件项目干系人管理与沟通管理', week='第14周',
      desc='干系人识别/参与规划-管理-监督；沟通规划/管理/监督、渠道数；干系人绩效域（一章浓缩）。', status='todo'),
 dict(no=15, title='第10章 软件项目风险管理', week='第15周',
      desc='规划风险、识别、定性与定量分析（概率-影响矩阵/EMV）、威胁/机会应对策略、实施与监督、不确定性绩效域。', status='todo'),
 dict(no=16, title='案例分析：综合案例讲解＋第11章整合管理串讲＋全书复习答疑', week='第16周',
      desc='综合案例串联 1–10 章；整合管理（项目管理计划、指导与管理、监控、整体变更控制 CCB、收尾）；期末复习与题型说明。', status='todo'),
]

def slide_page(no, i, total, title, body, pt_prev, pt_next, href_prev, href_next, label):
    prev_block = ('<span class="nav-btn nav-disabled">◀</span>' if not href_prev
                  else '<a class="nav-btn" href="%s" title="上一页">◀</a>' % href_prev)
    next_block = ('<span class="nav-btn nav-disabled">▶</span>' if not href_next
                  else '<a class="nav-btn" href="%s" title="下一页">▶</a>' % href_next)
    quiz_extra = '<script src="../quiz.js"></script>' if ('class="quiz' in body) or ('quiz-opt' in body) or ('data-action=' in body) else ''
    return f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Slide {no}.{i} — {esc(title)}</title>
<link rel="stylesheet" href="../styles.css">
</head>
<body class="slide-page">

<div class="logo-bar"><img src="../logo.png" alt="Logo"><span class="logo-label">{label}</span></div>

<div class="slide-breadcrumb">
  <a href="../index.html">主页</a> / <a href="index.html">第{no}次课</a> / <span>{no}.{i}</span>
</div>

<div class="single-slide-page">
  <div class="single-slide-wrapper">
    <div class="slide-card">
      <div class="slide-number">Slide {no}.{i}</div>
      <div class="slide-title">{title}</div>
      <div class="slide-content">
        {body}
      </div>
    </div>
  </div>
  <div class="single-nav-bar">
    {prev_block}
    <div><span class="nav-label prev-label">{esc(pt_prev)}</span></div>
    <span class="nav-info">{no}.{i} / {total}</span>
    <div><span class="nav-label next-label">{esc(pt_next)}</span></div>
    {next_block}
  </div>
  <div class="kbd-hint">键盘 <kbd>←</kbd> <kbd>→</kbd> 或鼠标滚轮翻页</div>
</div>

<script>
document.addEventListener('keydown',e=>{{
  if(e.key==='ArrowRight'&&'{href_next}')window.location.href='{href_next}';
  if(e.key==='ArrowLeft'&&'{href_prev}')window.location.href='{href_prev}';
}});
let _wl=false;
document.addEventListener('wheel',e=>{{
  e.preventDefault();
  if(_wl)return;_wl=true;
  if(e.deltaY>0&&'{href_next}')window.location.href='{href_next}';
  else if(e.deltaY<0&&'{href_prev}')window.location.href='{href_prev}';
  setTimeout(()=>_wl=false,500);
}},{{passive:false}});
</script>
{quiz_extra}
</body>
</html>'''

def build_lecture1():
    import sys
    sys.path.insert(0, ROOT)
    from _content1 import SLIDES as S1
    from _content2 import SLIDES as S2
    slides = S1 + S2
    total = len(slides)
    assert total == 52, total
    folder = os.path.join(ROOT, 'lecture1')
    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'1.{prev}.html' if prev >= 1 else ''
        href_next = f'1.{nxt}.html' if nxt <= total else ''
        with open(os.path.join(folder, f'1.{i}.html'), 'w', encoding='utf-8') as f:
            f.write(slide_page(1, i, total, title, body, pt_prev, pt_next, href_prev, href_next,
                               '软件项目管理 · 第 1 次课'))
    groups = [
        ('一、封面 · 课程导学 · 破冰', 1, 4),
        ('二、案例：华为鸿蒙 OS 开发项目管理实践（含失败案例对比）', 5, 10),
        ('三、1.1 项目与软件项目', 11, 15),
        ('四、1.2 软件项目管理：定义 · 成败 · 环境 · 职业认证', 16, 29),
        ('五、1.3 价值驱动的软件项目管理知识体系', 30, 47),
        ('六、课堂练习 · 本章小结 · 作业预告', 48, 52),
    ]
    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]
            items.append(f'    <a class="toc-item" href="1.{i}.html"><span class="toc-num">1.{i}</span><span class="toc-title">{esc(title)}</span><span class="toc-arrow">→</span></a>')
    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>第1次课 — 目录</title>
<link rel="stylesheet" href="../styles.css">
<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;}}
</style>
</head>
<body>

<div class="logo-bar">
  <img src="../logo.png" alt="Logo">
  <span class="logo-label">软件项目管理 · 第 1 次课</span>
</div>

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

<div class="toc-page">
  <h2>🕒 第 1 次课（第 1 周）：软件项目管理概述——华为鸿蒙 OS 开发项目管理实践案例</h2>
  <div class="toc-subtitle">{total} 张幻灯片 · 点击任意标题直接跳转（键盘 ← → 或滚轮翻页）</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('lecture1 built:', total, 'slides + toc')

def placeholder_page(no, week, title, desc, slides_target):
    return f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>第{no}次课 — 未开放观看</title>
<link rel="stylesheet" href="../styles.css">
</head>
<body>

<div class="logo-bar">
  <img src="../logo.png" alt="Logo">
  <span class="logo-label">软件项目管理 · 第 {no} 次课</span>
</div>

<div class="home-container" style="text-align:center;">
  <div class="home-hero">
    <h1>未开放观看</h1>
    <div class="subtitle">第 {no} 次课（{esc(week)}）：{esc(title)}</div>
    <div style="margin-top:14px;color:#718096;">👩‍🏫 授课教师：周宇文 ｜ 联系电话/微信：13580390715</div>
  </div>
  <div style="margin-top:10px;">
    <a href="../index.html" style="display:inline-flex;align-items:center;gap:8px;border-radius:12px;padding:12px 34px;text-decoration:none;color:#fff;background:linear-gradient(135deg,#2b6cb0,#3182ce);font-size:1.05rem;">← 返回主页</a>
  </div>
</div>
</body>
</html>'''

def build_placeholders():
    targets = {3:24,4:24,5:24,6:24,7:24,8:24,9:22,10:24,11:24,12:30,13:30,14:30,15:30,16:32}
    for L in LECTURES:
        if L['no'] == 1 or L['status'] != 'todo':
            continue
        folder = os.path.join(ROOT, f'lecture{L["no"]}')
        os.makedirs(folder, exist_ok=True)
        with open(os.path.join(folder, 'index.html'), 'w', encoding='utf-8') as f:
            f.write(placeholder_page(L['no'], L['week'], L['title'], L['desc'],
                                     targets.get(L['no'], 26)))
    print('placeholders built:', 15)

def build_home():
    cards = []
    for L in LECTURES:
        n = L['no']
        if n == 1:
            link, status_line = 'lecture1/index.html', '📋 查看全部 52 页 →'
        else:
            link, status_line = f'lecture{n}/index.html', '🔒 未开放观看 →'
        cards.append(f'''    <a class="lecture-card" href="{link}">
      <div class="card-banner">{ROMAN[n - 1]}</div>
      <div class="card-body">
        <h3>{esc(L['title'])}</h3>
        <div class="card-time">{esc(L['week'])} · 90 分钟（2 学时）</div>
        <div class="card-desc">{esc(L['desc'])}</div>
        <span class="lecture-overview-link">{status_line}</span>
        <div class="card-arrow">→</div>
      </div>
    </a>''')
    with open(os.path.join(ROOT, '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>{HOME_TITLE} — 16 次大课</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>

<div class="logo-bar">
  <img src="logo.png" alt="Logo">
  <span class="logo-label">软件项目管理 · 16 次大课</span>
</div>

<div class="home-container">
  <div class="home-hero">
    <h1>{HOME_TITLE}</h1>
    <div class="subtitle">{HOME_SUB}</div>
    <div class="subtitle">👩‍🏫 授课教师：周宇文 ｜ 联系电话/微信：13580390715</div>
    <span class="audience">{HOME_PILL}</span>
  </div>

  <div class="lecture-cards">
{chr(10).join(cards)}
  </div>
</div>
</body>
</html>''')
    print('home built')

if __name__ == '__main__':
    build_lecture1()
    build_placeholders()
    build_home()
