# -*- coding: utf-8 -*-
"""图 4-12　降级任务（按教材图意用 Python 重绘，未裁用课本照片）
要素：Project 任务视图窗口，功能区「日程」组中的**降级任务**按钮被高亮并有鼠标指针指向；
下方任务表：任务模式 | 任务名称 | 资源名称 | 添加新列，27 行任务按 WBS 层级缩进，
摘要任务前带 ◀ 折叠标记（末行为 1.3.1.4评教页面）。
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, box, arrow, line, INK, NAV, ACC, FILL, LINE, save
from matplotlib.patches import Rectangle

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', 'assets', '图4-12-降级任务.png')

# 教材图 4-12 中的 27 行任务（lay=层级, sum=是否摘要任务）
TASKS = [
    ('1.教务管理系统', 0, 1), ('1.1系统需求', 1, 1), ('1.1.1需求收集', 2, 1),
    ('1.1.1.1调研问卷设计', 3, 0), ('1.1.1.2访谈', 3, 0), ('1.1.1.3需求讨论会', 3, 0),
    ('1.1.2需求分析', 2, 1), ('1.1.2.1功能需求分析', 3, 0), ('1.1.2.2非功能需求分析', 3, 0),
    ('1.1.2.3需求规格说明书', 3, 0), ('1.1.3需求确认', 2, 0), ('1.1.4需求变更', 2, 0),
    ('1.2系统设计', 1, 1), ('1.2.1系统架构设计', 2, 0), ('1.2.2数据库设计', 2, 1),
    ('1.2.2.1数据库模型设计', 3, 0), ('1.2.2.2数据库表结构设计', 3, 0), ('1.2.2.3数据库关系和约束', 3, 0),
    ('1.2.3前端设计', 2, 0), ('1.2.4后端设计', 2, 0), ('1.2.5设计评审与确认', 2, 0),
    ('1.3系统开发', 1, 1), ('1.3.1前端开发', 2, 1), ('1.3.1.1登录页面', 3, 0),
    ('1.3.1.2课程列表页面', 3, 0), ('1.3.1.3成绩查询页面', 3, 0), ('1.3.1.4评教页面', 3, 0),
]


def tbl(ax, x, y, w, h, cols, rows, cw=None, fs=5.6, hfs=6.0):
    t = ax.table(cellText=rows, colLabels=cols, colWidths=cw,
                 bbox=[x / 100, (y - h) / 100, w / 100, h / 100], cellLoc='left', zorder=4)
    t.auto_set_font_size(False)
    for (r, c), cell in t.get_celld().items():
        cell.set_linewidth(0.45)
        cell.set_edgecolor('#9bb1c9')
        cell.PAD = 0.012
        if r == 0:
            cell.set_facecolor(NAV)
            cell.set_text_props(color='white', fontweight='bold', fontsize=hfs)
        else:
            cell.set_facecolor('white' if r % 2 else '#f7fbff')
            cell.set_text_props(fontsize=fs, color=INK)
    return t


setup()
fig, ax = canvas(11.0, 8.2)

# 窗口 / 标题栏 / 选项卡
ax.add_patch(Rectangle((1, 3), 98, 95, fill=False, ec='#4a5568', lw=1.3))
ax.text(3, 95.4, '教务管理系统-范围管理实验.mpp  -  Project Professional', fontsize=7.4, color=INK, va='center')
cx = 4.0
for name, act in [('文件', 0), ('任务', 1), ('资源', 0), ('报表', 0), ('项目', 0), ('视图', 0),
                  ('帮助', 0), ('甘特图格式', 0)]:
    w = 4.2 if len(name) <= 2 else 8.4
    box(ax, cx, 91.5, w, 3.2, name, fc=(FILL if act else 'white'), ec=(ACC if act else LINE),
        fs=6.8, bold=bool(act), radius=0.6, lw=1.0)
    cx += w + 0.6
ax.text(cx + 2, 93.1, '操作说明搜索', fontsize=6.2, color='#4a5568', va='center')

# 功能区：视图 / 剪贴板 / 字体 / 日程（含被点选的「降级任务」）/ 插入 / 属性
ax.add_patch(Rectangle((2.5, 79.5), 9.5, 10.2, fill=False, ec=LINE, lw=0.7))
ax.text(7.25, 80.4, '视图', fontsize=5.6, color='#4a5568', ha='center', va='center')
box(ax, 3.2, 87.6, 8.1, 4.4, '甘特图', fs=5.8, radius=0.5)

ax.add_patch(Rectangle((13.0, 79.5), 12.0, 10.2, fill=False, ec=LINE, lw=0.7))
ax.text(19.0, 80.4, '剪贴板', fontsize=5.6, color='#4a5568', ha='center', va='center')
box(ax, 13.6, 88.0, 10.8, 3.6, '粘贴  剪切  复制\n格式刷', fs=5.4, radius=0.5)

ax.add_patch(Rectangle((26.0, 79.5), 13.5, 10.2, fill=False, ec=LINE, lw=0.7))
ax.text(32.75, 80.4, '字体', fontsize=5.6, color='#4a5568', ha='center', va='center')
box(ax, 26.6, 88.0, 5.4, 3.6, '等线 11', fs=5.4, radius=0.5)
box(ax, 32.4, 88.0, 6.6, 3.6, 'B  I  U  A', fs=5.4, radius=0.5)

ax.add_patch(Rectangle((40.5, 79.5), 24.5, 10.2, fill=False, ec=LINE, lw=0.7))
ax.text(52.75, 80.4, '日程', fontsize=5.6, color='#4a5568', ha='center', va='center')
box(ax, 41.1, 88.4, 9.4, 3.8, '降级任务', fc=FILL, ec=ACC, fs=6.0, bold=True, radius=0.5, lw=1.8)
box(ax, 51.0, 88.0, 13.6, 3.4, '0% 25% 50% 75% 100%', fs=5.0, radius=0.5)

ax.add_patch(Rectangle((66.0, 79.5), 13.0, 10.2, fill=False, ec=LINE, lw=0.7))
ax.text(72.5, 80.4, '插入', fontsize=5.6, color='#4a5568', ha='center', va='center')
box(ax, 66.6, 88.0, 11.8, 3.6, '任务  摘要  里程碑\n可交付结果', fs=5.2, radius=0.5)

ax.add_patch(Rectangle((80.0, 79.5), 17.0, 10.2, fill=False, ec=LINE, lw=0.7))
ax.text(88.5, 80.4, '属性', fontsize=5.6, color='#4a5568', ha='center', va='center')

# 鼠标指针指向「降级任务」按钮
from matplotlib.patches import Polygon
ax.add_patch(Polygon([(47.4, 86.4), (47.4, 83.0), (48.3, 83.9), (48.9, 82.8),
                      (49.5, 83.1), (48.9, 84.2), (50.0, 84.3)], closed=True,
                     fc='#2d3748', ec='white', lw=0.6, zorder=6))

# 左侧视图栏
ax.add_patch(Rectangle((1.6, 4), 10.7, 74, fill=False, ec=LINE, lw=0.8))
for i, v in enumerate(['甘特图', '跟踪甘特图', '任务窗体', '任务分配状况', '任务工作表',
                       '日程表', '日历', '网络图', '工作组规划器']):
    if v == '任务工作表':
        box(ax, 1.9, 74.4 - i * 7.6, 10.1, 5.4, v, fc=FILL, ec=ACC, fs=5.4, radius=0.4)
    else:
        ax.text(6.9, 71.7 - i * 7.6, v, fontsize=5.4, color=INK, ha='center', va='center')

# 说明条
box(ax, 66.0, 77.4, 31.6, 3.0, '将带有日期的任务添加到日程表', fc='#edf2f7', ec=LINE, fs=5.8, radius=0.4)

# 任务表（层级缩进 + 摘要任务 ◀ 折叠标记）
rows = [[f'{i+1}', '◇', '　' * lv + ('◀ ' if sm else '') + name, '']
        for i, (name, lv, sm) in enumerate(TASKS)]
tbl(ax, 13.0, 74.5, 85.6, 70.5,
    ['', '任务模式', '任务名称', '资源名称'], rows,
    cw=[0.045, 0.08, 0.44, 0.24], fs=5.5, hfs=5.8)

save(fig, OUT)
