# -*- coding: utf-8 -*-
"""图 8-7　资源概述报告 —— 教材第 201 页

Project“报表”功能下的“资源概述”报表。按「图内要素」①～⑦ 逐项还原：
① 选项卡：文件、任务、资源、报表（选中，加下划线）、项目、视图、帮助、报表设计，
   右侧“操作说明搜索”框
② 功能区两组：项目——比较项目；查看报表——新建报表、仪表板、资源（下拉箭头展开、深色
   高亮）、成本、进行中、入门、自定义、最近
③ 左侧报表列表：甘特图、跟踪甘特图、里程碑日期总…、里程碑总成、任务窗体、任务分配状况、
   任务工作表、日程表、日历，以及底部一个被下边缘裁切的图标
④ 报表主标题“资源概述”＋横贯分隔线
⑤ 资源统计／所有工时资源的工时状态：柱状图（0—800 工时，每 100 一档），10 种资源；
   图例：实际工时、剩余工时、基线工时（黑点连线）；张军实际工时近 700，基线折线在
   “后端开发人员-陈豪”处升至约 200 工时形成峰值
⑥ 工作状态／所有工时资源的工时完成百分比：柱状图（0%—80%，每 10% 一档），柱顶标数值
   9%／70%／75%，其余 0%
⑦ 资源状态／所有工时资源的剩余工时：黑底白字表头四列（名称、开始时间、完成时间、
   剩余工时），三行数据
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, save, caption, NAV, INK  # noqa: E402

import matplotlib.pyplot as plt  # noqa: E402
from matplotlib.patches import Polygon, Rectangle  # noqa: E402

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图8-7-资源概述报告.png')

GRID = '#cbd5e0'
FRAME = '#a0aec0'
BG = '#f7fafc'
SEL = '#d6e4f5'
BLACK = '#1a1a1a'
GREY = '#4a5568'
BAR1 = '#1a202c'          # 实际工时（黑柱）
BAR2 = '#4a5568'          # 剩余工时（深灰柱，与原书同为深色柱）

TABS = ['文件', '任务', '资源', '报表', '项目', '视图', '帮助']
RES = ['项目经理-张军', '分析人员-王飞', '分析人员-李明', '系统架构师-王浩', 'UI设计师-赵强',
       '前端开发人员-李兴', '后端开发人员-陈豪', '后端开发人员-陈旭', '后端开发人员-赵峰',
       '测试人员-孙晓']
ACT = [700, 56, 96, 48, 60, 72, 88, 54, 66, 50]           # 实际工时
REM = [616, 24, 32, 0, 0, 0, 0, 0, 0, 0]                   # 剩余工时
BASE = [480, 60, 40, 30, 20, 60, 200, 100, 60, 40]         # 基线工时（陈豪处峰值 200）
PCT = [9, 70, 75, 0, 0, 0, 0, 0, 0, 0]                     # 工时完成百分比
TABLE = [('项目经理-张军', '2024年11月12日', '2025年3月4日', '616 工时'),
         ('分析人员-王飞', '2024年11月18日', '2024年12月11日', '24 工时'),
         ('分析人员-李明', '2024年11月12日', '2024年12月17日', '32 工时')]

# 图表区
CX0, CX1 = 23.2, 96.0
# 第一块
C1_Y0, C1_Y1 = 52.0, 71.0
# 第二块
C2_Y0, C2_Y1 = 28.6, 44.4


def rect(ax, x0, y0, x1, y1, fc='white', ec=GRID, lw=0.6, z=2):
    ax.add_patch(Rectangle((x0, y0), x1 - x0, y1 - y0, facecolor=fc,
                           edgecolor=ec, linewidth=lw, zorder=z))


def txt(ax, x, y, s, fs=6.2, ha='left', va='center', color=INK, bold=False, z=6):
    ax.text(x, y, s, fontsize=fs, ha=ha, va=va, color=color, zorder=z,
            fontweight='bold' if bold else 'normal')


def btn(ax, x, y_top, w, h, label, fs=5.0, dark=False):
    rect(ax, x, y_top - h, x + w, y_top,
         fc=(NAV if dark else 'white'), ec=(NAV if dark else '#94a3b8'),
         lw=(0.9 if dark else 0.7), z=3)
    txt(ax, x + w / 2, y_top - h / 2, label, fs=fs, ha='center',
        color=('white' if dark else INK), bold=dark, z=5)
    if label == '资源':                        # 下拉箭头（展开中）
        ax.add_patch(Polygon([[x + w - 1.9, y_top - h / 2 + 0.35],
                              [x + w - 0.7, y_top - h / 2 + 0.35],
                              [x + w - 1.3, y_top - h / 2 - 0.55]],
                             closed=True, facecolor='white', edgecolor='none', zorder=6))


def draw_chrome(ax):
    ax.add_patch(Rectangle((0.5, 5.0), 99.0, 93.8, facecolor='white',
                           edgecolor='#2d3748', linewidth=1.2, zorder=1))
    # 标题栏
    rect(ax, 0.62, 95.9, 99.38, 98.6, fc='#f7fafc', ec='#cbd5e0', lw=0.6, z=2)
    txt(ax, 1.9, 97.25, '教务管理系统-资源管理实验.mpp   —   Project Professional',
        fs=5.4, color=GREY, z=3)
    txt(ax, 98.6, 97.25, '—   □   ×', fs=5.8, ha='right', color=GREY, z=3)

    # 选项卡
    rect(ax, 0.62, 92.4, 99.38, 95.9, fc='white', ec=FRAME, lw=0.7, z=1)
    x = 1.9
    for t in TABS:
        active = (t == '报表')
        txt(ax, x, 94.15, t, fs=6.2, ha='center', bold=active,
            color=(NAV if active else INK), z=6)
        if active:
            ax.plot([x - 2.2, x + 2.2], [92.75, 92.75], color=NAV, lw=2.2, zorder=6)
        x += 5.3
    txt(ax, 40.0, 94.15, '报表设计', fs=6.2, color=GREY, z=6)
    rect(ax, 80.0, 93.0, 98.4, 95.2, fc='white', ec='#cbd5e0', lw=0.6, z=4)
    ax.add_patch(plt.Circle((82.0, 94.1), 0.42, fill=False, ec='#718096', lw=0.7, zorder=5))
    ax.plot([82.35, 82.9], [93.75, 93.25], color='#718096', lw=0.7, zorder=5)
    txt(ax, 84.0, 94.1, '操作说明搜索', fs=5.0, color='#718096', z=5)

    # 功能区
    rect(ax, 0.62, 82.0, 99.38, 92.4, fc=BG, ec=FRAME, lw=0.7, z=1)
    ax.plot([12.2, 12.2], [82.6, 91.6], color='#e2e8f0', lw=0.8, zorder=2)
    txt(ax, 2.0, 83.1, '项目', fs=4.8, color='#718096', z=5)
    btn(ax, 2.2, 91.2, 8.4, 6.4, '比较项目', fs=5.0)

    txt(ax, 13.2, 83.1, '查看报表', fs=4.8, color='#718096', z=5)
    items = ['新建报表', '仪表板', '资源', '成本', '进行中', '入门', '自定义', '最近']
    for k, lab in enumerate(items):
        btn(ax, 13.6 + k * 9.6, 91.2, 8.8, 6.4, lab, fs=4.9, dark=(lab == '资源'))


def draw_report_list(ax):
    rect(ax, 0.62, 8.0, 14.0, 82.0, fc='white', ec=FRAME, lw=0.7, z=2)
    items = ['甘特图', '跟踪甘特图', '里程碑日期总…', '里程碑总成', '任务窗体',
             '任务分配状况', '任务工作表', '日程表', '日历']
    h = 7.5
    for i, it in enumerate(items):
        yt = 82.0 - i * h
        ax.plot([0.62, 14.0], [yt - h, yt - h], color=GRID, lw=0.6, zorder=4)
        # 图标（报表缩略图样式）
        rect(ax, 1.5, yt - h + 1.1, 5.6, yt - 1.1, fc='white', ec='#94a3b8', lw=0.5, z=4)
        ax.plot([2.0, 5.6], [yt - 2.3, yt - 2.3], color='#a0aec0', lw=0.5, zorder=5)
        ax.plot([2.0, 5.0], [yt - 3.3, yt - 3.3], color='#a0aec0', lw=0.5, zorder=5)
        ax.plot([2.0, 6.4], [yt - 4.3, yt - 4.3], color='#cbd5e0', lw=0.5, zorder=5)
        txt(ax, 7.6, yt - h / 2, it, fs=5.2, z=6)
    # 底部被裁切的图标
    rect(ax, 1.5, 8.2, 5.6, 12.1, fc='white', ec='#94a3b8', lw=0.5, z=4)


def chart1(ax):
    n = len(RES)
    plot_w = CX1 - CX0
    cw = plot_w / n
    vmax = 800
    y_of = lambda v: C1_Y0 + (C1_Y1 - C1_Y0) * v / vmax

    # 纵轴刻度与网格
    for v in range(0, vmax + 1, 100):
        y = y_of(v)
        ax.plot([CX0, CX1], [y, y], color='#eef2f7', lw=0.4, zorder=2)
        if v:
            txt(ax, CX0 - 0.8, y, str(v), fs=3.9, ha='right', color=GREY, z=5)
    txt(ax, CX0 - 0.8, C1_Y0, '0', fs=3.9, ha='right', color=GREY, z=5)
    ax.plot([CX0, CX0], [C1_Y0, C1_Y1], color=GREY, lw=0.7, zorder=4)
    ax.plot([CX0, CX1], [C1_Y0, C1_Y0], color=GREY, lw=0.7, zorder=4)

    bw = cw * 0.26
    for i, (a, r) in enumerate(zip(ACT, REM)):
        cx = CX0 + cw * (i + 0.5)
        ax.add_patch(Rectangle((cx - bw - 0.45, C1_Y0), bw, y_of(a) - C1_Y0,
                               facecolor=BAR1, edgecolor='none', zorder=5))
        if r:
            ax.add_patch(Rectangle((cx + 0.45, C1_Y0), bw, y_of(r) - C1_Y0,
                                   facecolor=BAR2, edgecolor='none', zorder=5))
        txt(ax, cx, C1_Y0 - 1.2, RES[i], fs=4.1, ha='center', color=INK, z=6)

    # 基线：黑点折线
    xs = [CX0 + cw * (i + 0.5) for i in range(n)]
    ys = [y_of(b) for b in BASE]
    ax.plot(xs, ys, color=BLACK, lw=0.8, zorder=6)
    ax.plot(xs, ys, linestyle='none', marker='o', markersize=1.1, color=BLACK, zorder=7)

    # 图例
    lx, ly = 60.0, C1_Y1 - 2.2
    ax.add_patch(Rectangle((lx - 1.0, ly - 2.2), 39.0, 4.4, facecolor='white',
                           edgecolor='none', zorder=6))
    ax.add_patch(Rectangle((lx, ly - 0.7), 1.5, 1.5, facecolor=BAR1, edgecolor='none', zorder=7))
    txt(ax, lx + 2.2, ly, '实际工时', fs=4.3, z=7)
    ax.add_patch(Rectangle((lx + 11.5, ly - 0.7), 1.5, 1.5, facecolor=BAR2, edgecolor='none', zorder=7))
    txt(ax, lx + 13.7, ly, '剩余工时', fs=4.3, z=7)
    ax.plot([lx + 23.0, lx + 27.0], [ly, ly], color=BLACK, lw=0.8, zorder=7)
    ax.plot([lx + 24.0, lx + 26.0], [ly, ly], linestyle='none',
            marker='o', markersize=1.2, color=BLACK, zorder=8)
    txt(ax, lx + 28.0, ly, '基线工时', fs=4.3, z=7)


def chart2(ax):
    n = len(RES)
    plot_w = CX1 - CX0
    cw = plot_w / n
    vmax = 80
    y_of = lambda v: C2_Y0 + (C2_Y1 - C2_Y0) * v / vmax

    for v in range(0, vmax + 1, 10):
        y = y_of(v)
        ax.plot([CX0, CX1], [y, y], color='#eef2f7', lw=0.4, zorder=2)
        txt(ax, CX0 - 0.8, y, f'{v}%', fs=3.9, ha='right', color=GREY, z=5)
    ax.plot([CX0, CX0], [C2_Y0, C2_Y1], color=GREY, lw=0.7, zorder=4)
    ax.plot([CX0, CX1], [C2_Y0, C2_Y0], color=GREY, lw=0.7, zorder=4)

    bw = cw * 0.42
    for i, p in enumerate(PCT):
        cx = CX0 + cw * (i + 0.5)
        ax.add_patch(Rectangle((cx - bw / 2, C2_Y0), bw, max(y_of(p) - C2_Y0, 0.12),
                               facecolor=BAR1, edgecolor='none', zorder=5))
        txt(ax, cx, C2_Y0 - 1.2, RES[i], fs=4.1, ha='center', color=INK, z=6)
        if p:
            txt(ax, cx, y_of(p) + 1.0, f'{p}%', fs=3.9, ha='center', bold=True, z=7)
        else:
            txt(ax, cx, C2_Y0 + 1.0, '0%', fs=3.6, ha='center', color=GREY, z=7)

    # 图例
    lx, ly = 76.0, C2_Y1 - 2.2
    ax.add_patch(Rectangle((lx - 1.0, ly - 2.0), 22.0, 4.0, facecolor='white',
                           edgecolor='none', zorder=6))
    ax.add_patch(Rectangle((lx, ly - 0.7), 1.5, 1.5, facecolor=BAR1, edgecolor='none', zorder=7))
    txt(ax, lx + 2.2, ly, '工时完成百分比', fs=4.3, z=7)


def chart3(ax):
    cols = [('名称', 26.0), ('开始时间', 21.0), ('完成时间', 21.0), ('剩余工时', 12.5)]
    x0, top = 17.5, 21.0
    hh = 3.5
    rh = 3.7
    total = sum(w for _, w in cols)
    # 表头（黑底白字）
    rect(ax, x0, top - hh, x0 + total, top, fc=BLACK, ec=BLACK, lw=0.8, z=4)
    cx = x0
    for name, w in cols:
        txt(ax, cx + 0.9, top - hh / 2, name, fs=4.6, color='white', bold=True, z=6)
        cx += w
    # 数据行
    for r, row in enumerate(TABLE):
        yt = top - hh - r * rh
        rect(ax, x0, yt - rh, x0 + total, yt, fc='white', ec=GRID, lw=0.5, z=3)
        cx = x0
        for k, ((name, w), val) in enumerate(zip(cols, row)):
            txt(ax, cx + 0.9, yt - rh / 2, val, fs=4.6, z=6)
            cx += w
            if k < len(cols) - 1:
                ax.plot([cx, cx], [yt - rh, yt], color=GRID, lw=0.5, zorder=4)
    # 表格外框
    ax.add_patch(Rectangle((x0, top - hh - rh * len(TABLE)), total, hh + rh * len(TABLE),
                           fill=False, edgecolor=BLACK, lw=0.9, zorder=6))


def main():
    setup()
    fig, ax = canvas(15.2, 10.6, xlim=(0, 100), ylim=(0, 100))
    draw_chrome(ax)
    draw_report_list(ax)

    # 报表页面
    rect(ax, 14.6, 6.0, 99.38, 82.0, fc='white', ec=FRAME, lw=0.8, z=2)
    txt(ax, 17.0, 79.6, '资源概述', fs=12.0, va='top', bold=True, color=BLACK, z=6)
    ax.plot([17.0, 97.0], [76.6, 76.6], color=BLACK, lw=0.9, zorder=6)

    # ⑤ 资源统计
    txt(ax, 17.0, 75.1, '资源统计', fs=6.8, bold=True, z=6)
    txt(ax, 17.0, 73.3, '所有工时资源的工时状态。', fs=5.2, color=GREY, z=6)
    chart1(ax)

    # ⑥ 工作状态
    txt(ax, 17.0, 48.6, '工作状态', fs=6.8, bold=True, z=6)
    txt(ax, 17.0, 46.8, '所有工时资源的工时完成百分比。', fs=5.2, color=GREY, z=6)
    chart2(ax)

    # ⑦ 资源状态
    txt(ax, 17.0, 25.0, '资源状态', fs=6.8, bold=True, z=6)
    txt(ax, 17.0, 23.2, '所有工时资源的剩余工时。', fs=5.2, color=GREY, z=6)
    chart3(ax)

    caption(ax, '图 8-7　资源概述报告', fs=10.5, y=1.2)
    save(fig, OUT)


if __name__ == '__main__':
    main()
