# -*- coding: utf-8 -*-
"""图 4-11　输入任务名称和任务编码（按教材图意用 Python 重绘，未裁用课本照片）
要素：Project 任务视图窗口（任务选项卡、日程/属性组、「开始时间 2024年11月12日」字段），
下方任务表：任务模式 | 任务名称 | 工期 | 开始时间 | 完成时间 | 前置任务 | 资源名称，
27 行任务（1.教务管理系统 … 1.3.2后端开发），任务名称按层级缩进。
"""
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-11-输入任务名称和任务编码.png')

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


def tbl(ax, x, y, w, h, cols, rows, cw=None, fs=5.6, hfs=6.0, ind_col=None):
    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')

# 功能区分组
def group(x, w, lab):
    ax.add_patch(Rectangle((x, 79.5), w, 10.2, fill=False, ec=LINE, lw=0.7))
    ax.text(x + w / 2, 80.4, lab, fontsize=5.6, color='#4a5568', ha='center', va='center')

group(2.5, 9.5, '视图')
box(ax, 3.2, 87.6, 8.1, 4.4, '甘特图', fs=5.8, radius=0.5)
group(13.0, 12.0, '剪贴板')
box(ax, 13.6, 88.0, 10.8, 3.6, '粘贴  剪切  复制\n格式刷', fs=5.4, radius=0.5)
group(26.0, 13.5, '字体')
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)
group(40.5, 22.5, '日程')
box(ax, 41.1, 88.0, 21.3, 3.6, '手动安排  自动安排  检查  移动  模式', fs=5.4, radius=0.5)
group(64.0, 23.0, '属性')
box(ax, 64.6, 88.6, 8.0, 3.2, '开始时间', fs=5.4, radius=0.5)
box(ax, 73.0, 88.4, 13.4, 3.6, '2024年11月12日', fs=5.6, radius=0.5, ec=ACC)

# 指向任务表的两个箭头（原图自「开始时间」指向表格）
arrow(ax, (72.5, 88.2), (66.5, 76.4), color='#2d3748', lw=1.1)
arrow(ax, (72.0, 88.0), (62.0, 75.2), color='#2d3748', lw=1.1)

# 右侧添加新列提示条
box(ax, 60.0, 78.6, 38.6, 3.0, '将带有日期的任务添加到日程表', fc='#edf2f7', ec=LINE, fs=5.8, radius=0.4)

# 左侧视图栏
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')

# 任务表
rows = [[f'{i+1}', '◇', '　' * lv + name, '', '', '', '', '']
        for i, (name, lv) in enumerate(TASKS)]
tbl(ax, 13.0, 74.0, 85.6, 70.0, ['', '任务模式', '任务名称', '工期', '开始时间', '完成时间', '前置任务', '资源名称'],
    rows, cw=[0.045, 0.075, 0.33, 0.09, 0.15, 0.15, 0.13, 0.13], fs=5.5, hfs=5.8)

save(fig, OUT)
