# -*- coding: utf-8 -*-
"""图 5-35　更新多个实际任务的完成进度（按教材图意 Python 重绘，未裁用课本照片）
Project「视图」选项卡 →「数据」组「表格」下拉选「跟踪」，表格区显示实际开始/实际完成/完成百分比等列，
右侧跟踪甘特图叠加灰色基准条与实际进度。
"""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, box, line, arrow, 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', '图5-35-更新多个实际任务的完成进度.png')

ROWS = [
 ('1.教务管理系统', '11年11月12日', 'NA', '11%', '0%', '7.62 个工作日', '38 个工作日', 0),
 ('1.1系统需求', '11年11月12日', 'NA', '81%', '0%', '17 个工作日', '4 个工作日', 1),
 ('1.1.1需求收集', '11年11月12日', '11年11月22日', '100%', '0%', '9 个工作日', '0 个工作日', 2),
 ('1.1.1.1调研问卷设计', '11年11月12日', '11年11月15日', '100%', '0%', '4 个工作日', '0 个工作日', 3),
 ('1.1.1.2访谈', '11年11月16日', '11年11月19日', '100%', '0%', '2 个工作日', '0 个工作日', 3),
 ('1.1.1.3需求讨论会', '2024年11月20日', '2024年11月22日', '100%', '0%', '3 个工作日', '0 个工作日', 3),
 ('1.1.2需求分析', '11年11月25日', 'NA', '67%', '0%', '8 个工作日', '4 个工作日', 2),
 ('1.1.2.1功能需求分析', '2024年11月25日', '2024年12月2日', '100%', '0%', '4 个工作日', '0 个工作日', 3),
 ('1.1.2.2非功能需求分析', '2024年11月29日', 'NA', '67%', '0%', '4 个工作日', '2 个工作日', 3),
 ('1.1.2.3需求规格说明书', 'NA', 'NA', '0%', '0%', '0 个工作日', '2 个工作日', 3),
]

setup()
fig, ax = canvas(11.6, 5.2, xlim=(0, 100), ylim=(6, 100))
ax.add_patch(Rectangle((1, 8), 98, 90, fill=True, fc='white', ec='#4a5568', lw=1.1, zorder=1))
ax.text(2.4, 95.2, '教务管理系统-进度管理实验.mpp  -  Project Professional', fontsize=6.0, color=INK, va='center')

cx = 3.0
for name, act in [('文件', 0), ('任务', 0), ('资源', 0), ('报表', 0), ('项目', 0), ('视图', 1), ('帮助', 0)]:
    w = 4.0 if len(name) <= 2 else 6.0
    box(ax, cx, 89.6, w, 3.6, name, fc=(FILL if act else 'white'), ec=(ACC if act else LINE),
        fs=5.6, bold=bool(act), radius=0.4, lw=0.9)
    cx += w + 0.4

ax.add_patch(Rectangle((2, 74), 96, 13.5, fill=True, fc='#f7fafc', ec='#cbd5e0', lw=0.8, zorder=2))
for name, x in [('甘特图', 3.5), ('任务分配状况', 10.5), ('资源使用状况', 19.0), ('资源工作表', 28.5), ('表格', 38.0)]:
    ax.text(x, 84.0, name, fontsize=4.6, color='#4a5568', va='center', zorder=3)
# 下拉菜单
ax.add_patch(Rectangle((45.0, 57.0), 17.0, 16.0, fill=True, fc='white', ec='#a0aec0', lw=0.9, zorder=7))
menu = ['日程', '甘特图', '资源工作表', '跟踪', '其他表格']
for i, m in enumerate(menu):
    yy = 71.0 - i * 3.2
    if m == '跟踪':
        ax.add_patch(Rectangle((45.2, yy - 1.6), 16.6, 3.2, fill=True, fc='#bee3f8', ec='none', zorder=8))
    ax.text(46.2, yy, m, fontsize=4.6, color=INK, va='center', zorder=9)
ax.text(54.0, 75.2, '下拉表格下的“跟踪”', fontsize=4.8, color='#c53030', zorder=9)
arrow(ax, (54.0, 74.2), (54.0, 72.0), color='#c53030', lw=0.9)

# 表格
tx, top, tw = 3.0, 72.0, 66.0
cols = [('任务名称', 0, 22), ('实际开始', 22, 11), ('实际完成', 33, 11), ('完成\n百分比', 44, 7),
        ('实际完成\n百分比', 51, 7), ('实际工期', 58, 5.0), ('剩余工期', 63, 3.0)]
hh, rh = 4.6, 4.4
n = len(ROWS)
th = hh + rh * n
ax.add_patch(Rectangle((tx, top - th), tw, th, fill=True, fc='white', ec='#4a5568', lw=0.9, zorder=3))
ax.add_patch(Rectangle((tx, top - hh), tw, hh, fill=True, fc='#edf2f7', ec='#4a5568', lw=0.7, zorder=4))
for name, xx, w in cols:
    ax.text(tx + xx + 0.6, top - hh / 2, name, fontsize=4.0, color=INK, va='center', zorder=5)
    ax.plot([tx + xx, tx + xx], [top - th, top], color='#cbd5e0', lw=0.5, zorder=5)
for i, r in enumerate(ROWS):
    yy = top - hh - rh * (i + 1) + rh / 2
    bold = r[7] <= 1
    ax.text(tx + 0.6 + r[7] * 1.8, yy, r[0], fontsize=4.0, color=INK, va='center',
            zorder=5, fontweight='bold' if bold else 'normal')
    for j, xx in enumerate([22, 33, 44, 51, 58, 63], start=1):
        ax.text(tx + xx + 0.6, yy, r[j], fontsize=3.8, color=INK, va='center', zorder=5)
    ax.plot([tx, tx + tw], [yy - rh / 2, yy - rh / 2], color='#f1f5f9', lw=0.4, zorder=4)

# 跟踪甘特
gx, gw = 70.0, 28.0
ax.add_patch(Rectangle((gx, top - th), gw, th, fill=True, fc='white', ec='#4a5568', lw=0.9, zorder=3))
for i in range(n):
    yy = top - hh - rh * (i + 1) + rh / 2
    ax.plot([gx, gx + gw], [yy - rh / 2, yy - rh / 2], color='#f1f5f9', lw=0.4, zorder=4)
    w = gw * (0.9 - i * 0.045) if i < 7 else gw * 0.12
    ax.add_patch(Rectangle((gx + 0.8, yy - 0.9), max(w, 0.5), 1.8, fc='#cbd5e0', ec='#a0aec0', lw=0.3, zorder=5))
    prog = float(ROWS[i][3].rstrip('%')) / 100.0
    if prog > 0:
        ax.add_patch(Rectangle((gx + 0.8, yy - 0.9), max(w * prog, 0.2), 1.8, fc='#2b6cb0', ec='none', zorder=6))
    if prog == 1.0 and i < 8:
        ax.text(gx + 0.8 + w + 0.4, yy, '100%', fontsize=3.4, color='#4a5568', va='center', zorder=6)
    if prog == 0.67 and i < 9:
        ax.text(gx + 0.8 + w + 0.4, yy, '67%', fontsize=3.4, color='#4a5568', va='center', zorder=6)
save(fig, OUT)
