# -*- coding: utf-8 -*-
"""图 5-34　更新单个实际任务的完成进度（按教材图意 Python 重绘，未裁用课本照片）
Project 任务表 + 「更新任务」对话框（完成百分比 100%、实际工期 2 个工作日、剩余工期 0 个工作日），
右侧跟踪甘特图：灰色条形为进度基准，叠加实际进度与实际完成百分比标注。
"""
import os, sys, datetime as dt
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
import matplotlib.dates as mdates

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', 'assets', '图5-34-更新单个实际任务的完成进度.png')

TASKS = [
    ('1.教务管理系统', '65.57 个工作日', '2024年11月12日', '2025年2月11日', '11%'),
    ('1.1系统需求', '17.57 个工作日', '2024年11月12日', '2024年12月5日', '81%'),
    ('1.1.1需求收集', '11.57 个工作日', '2024年11月12日', '2024年11月27日', '100%'),
    ('1.1.1.1调研问卷设计', '8.57 个工作日', '2024年11月12日', '2024年11月22日', '100%'),
    ('1.1.1.2访谈', '2 个工作日', '2024年11月21日', '2024年11月22日', '100%'),
    ('1.1.1.3需求讨论会', '1 个工作日', '2024年11月26日', '2024年11月26日', '100%'),
    ('1.1.2需求分析', '6 个工作日', '2024年11月27日', '2024年12月4日', '50%'),
]
setup()
fig, ax = canvas(11.4, 5.2, xlim=(0, 100), ylim=(2, 100))

# 窗口
ax.add_patch(Rectangle((1, 9), 98, 89, fill=True, fc='white', ec='#4a5568', lw=1.1, zorder=1))
ax.text(2.4, 95.4, '教务管理系统-进度管理实验.mpp  -  Project Professional', fontsize=6.2, color=INK, va='center')

# 选项卡 + 功能区
cx = 3.0
for name, act in [('文件', 0), ('任务', 1), ('资源', 0), ('报表', 0), ('项目', 0), ('视图', 0), ('帮助', 0)]:
    w = 4.0 if len(name) <= 2 else 6.0
    box(ax, cx, 90.0, w, 3.6, name, fc=(FILL if act else 'white'), ec=(ACC if act else LINE),
        fs=5.8, bold=bool(act), radius=0.4, lw=0.9)
    cx += w + 0.4
ax.add_patch(Rectangle((2, 76), 96, 11.5, fill=True, fc='#f7fafc', ec='#cbd5e0', lw=0.8, zorder=2))
for name, x in [('粘贴', 3.5), ('剪切', 8), ('复制', 12.5), ('格式刷', 17), ('字体', 26),
                ('日程', 44), ('跟踪时标记', 60), ('任务', 76), ('插入', 86)]:
    ax.text(x, 84.0, name, fontsize=4.8, color='#4a5568', va='center', zorder=3)
ax.text(60, 79.0, '更新任务', fontsize=4.6, color=INK, ha='center', zorder=3)

# 任务表
tx, top = 3.0, 74.0
tw = 60.0
cols = [('任务名称', 0, 24), ('工期', 24, 14), ('开始时间', 38, 12), ('完成时间', 50, 12)]
hh, rh = 4.2, 4.6
n = len(TASKS)
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.8, top - hh / 2, name, fontsize=4.8, color=INK, va='center', zorder=5)
    ax.plot([tx + xx, tx + xx], [top - th, top], color='#cbd5e0', lw=0.6, zorder=5)
for i, (name, dur, st, fn, pct) in enumerate(TASKS):
    yy = top - hh - rh * (i + 1)
    ax.text(tx + 0.8, yy + rh / 2, name, fontsize=4.6, color=INK, va='center', zorder=5)
    ax.text(tx + 24.8, yy + rh / 2, dur, fontsize=4.4, color=INK, va='center', zorder=5)
    ax.text(tx + 38.8, yy + rh / 2, st, fontsize=4.2, color=INK, va='center', zorder=5)
    ax.text(tx + 50.8, yy + rh / 2, fn, fontsize=4.2, color=INK, va='center', zorder=5)
    ax.plot([tx, tx + tw], [yy, yy], color='#f1f5f9', lw=0.4, zorder=4)

# 右侧跟踪甘特
gx, gw = 64.0, 34.0
ax.add_patch(Rectangle((gx, top - th), gw, th, fill=True, fc='white', ec='#4a5568', lw=0.9, zorder=3))
d0, d1 = mdates.date2num(dt.date(2024, 11, 11)), mdates.date2num(dt.date(2024, 12, 3))
def X(day):
    return gx + (mdates.date2num(day) - d0) / (d1 - d0) * gw
base = [('2024-11-12', '2025-02-11'), ('2024-11-12', '2024-12-05'), ('2024-11-12', '2024-11-27'),
        ('2024-11-12', '2024-11-22'), ('2024-11-21', '2024-11-22'), ('2024-11-26', '2024-11-26'),
        ('2024-11-27', '2024-12-04')]
for i, (s, e) in enumerate(base):
    yy = top - hh - rh * (i + 1) + rh / 2
    xs, xe = X(dt.date.fromisoformat(s)), X(dt.date.fromisoformat(e))
    xs, xe = max(xs, gx + 0.2), min(xe, gx + gw - 0.2)
    ax.add_patch(Rectangle((xs, yy - 0.9), max(xe - xs, 0.4), 1.8, fc='#cbd5e0', ec='#a0aec0', lw=0.4, zorder=4))
    if i >= 2:   # 实际进度（前 5 行已完成/进行中）
        ax.add_patch(Rectangle((xs, yy - 0.9), max(xe - xs, 0.4) * (0.0 if i >= 5 else 1.0), 1.8,
                               fc='#2b6cb0', ec='none', zorder=5))
    ax.text(xe + 0.8, yy, TASKS[i][4], fontsize=3.8, color='#4a5568', va='center', zorder=6)
ax.text(gx + 9.0, 30.0, '灰色条形图为进度基准', fontsize=4.6, color='#c53030', zorder=6)
arrow(ax, (gx + 12.0, 31.2), (gx + 16.0, top - th + rh * 5.6), color='#c53030', lw=0.9)

# 更新任务对话框
dx, dy, dw, dh = 13, 12, 56, 52
ax.add_patch(Rectangle((dx, dy), dw, dh, fill=True, fc='#f7fafc', ec='#2d3748', lw=1.3, zorder=8))
ax.add_patch(Rectangle((dx, dy + dh - 5.4), dw, 5.4, fill=True, fc=NAV, ec=NAV, zorder=9))
ax.text(dx + 1.6, dy + dh - 2.7, '更新任务', fontsize=5.8, color='white', va='center', zorder=10)
ax.text(dx + dw - 2.8, dy + dh - 2.7, '×', fontsize=7.4, color='white', va='center', zorder=10)

ax.text(dx + 2, dy + dh - 10, '名称：', fontsize=5.0, color=INK, va='center', zorder=11)
ax.add_patch(Rectangle((dx + 9, dy + dh - 12.4), 26, 4.6, fill=True, fc='white', ec='#a0aec0', lw=0.8, zorder=10))
ax.text(dx + 9.8, dy + dh - 10, '1.1.1.2访谈', fontsize=5.0, color=INK, va='center', zorder=11)
ax.text(dx + 37, dy + dh - 10, '工期：2 个工作日', fontsize=5.0, color=INK, va='center', zorder=11)

ax.text(dx + 2, dy + dh - 18, '完成百分比(C)：', fontsize=4.8, color=INK, va='center', zorder=11)
ax.add_patch(Rectangle((dx + 21, dy + dh - 20.2), 7, 4.4, fill=True, fc='white', ec='#a0aec0', lw=0.8, zorder=10))
ax.text(dx + 22, dy + dh - 18, '100%', fontsize=4.8, color=INK, va='center', zorder=11)
ax.text(dx + 30, dy + dh - 18, '实际工期(A)：', fontsize=4.8, color=INK, va='center', zorder=11)
ax.add_patch(Rectangle((dx + 44, dy + dh - 20.2), 10, 4.4, fill=True, fc='white', ec='#a0aec0', lw=0.8, zorder=10))
ax.text(dx + 45, dy + dh - 18, '2 个工作日', fontsize=4.8, color=INK, va='center', zorder=11)
ax.text(dx + 2, dy + dh - 25, '剩余工期(R)：', fontsize=4.8, color=INK, va='center', zorder=11)
ax.add_patch(Rectangle((dx + 21, dy + dh - 27.2), 7, 4.4, fill=True, fc='white', ec='#a0aec0', lw=0.8, zorder=10))
ax.text(dx + 22, dy + dh - 25, '0 个工作日', fontsize=4.6, color=INK, va='center', zorder=11)

ax.text(dx + 2, dy + dh - 33, '实际', fontsize=4.8, color='#4a5568', va='center', zorder=11)
ax.text(dx + 2, dy + dh - 38, '开始时间：2024年11月21日', fontsize=4.6, color=INK, va='center', zorder=11)
ax.text(dx + 2, dy + dh - 43, '完成时间：2024年11月22日', fontsize=4.6, color=INK, va='center', zorder=11)
ax.text(dx + 32, dy + dh - 33, '当前', fontsize=4.8, color='#4a5568', va='center', zorder=11)
ax.text(dx + 32, dy + dh - 38, '开始时间：2024年11月21日', fontsize=4.6, color=INK, va='center', zorder=11)
ax.text(dx + 32, dy + dh - 43, '完成时间：2024年11月22日', fontsize=4.6, color=INK, va='center', zorder=11)

for txt, bx, fw in [('帮助(H)', dx + 3, 10), ('备注(N)…', dx + 15, 11), ('确定', dx + 36, 9), ('取消', dx + 46, 9)]:
    ax.add_patch(Rectangle((bx, dy + 3), fw, 4.2, fill=True, fc='white', ec='#a0aec0', lw=0.8, zorder=12))
    ax.text(bx + fw / 2, dy + 5.1, txt, fontsize=4.6, color=INK, ha='center', va='center', zorder=13)

save(fig, OUT)
