#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""在v7末尾加一页“月报后续事项归纳”四象限。"""
import os
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.enum.shapes import MSO_SHAPE
from pptx.oxml.ns import qn
from lxml import etree

ROOT = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
SRC = os.path.join(ROOT, '经营分析/月报/2026年10月销售部工作汇报_草稿_20261108_v7.pptx')
LOGO = '/Users/liuxinyuan/work/公司资料/logo_transparent.png'

NAVY = RGBColor(0x0B, 0x2A, 0x4A)
BLUE = RGBColor(0x1F, 0x6F, 0xEB)
LIGHT = RGBColor(0xEA, 0xF2, 0xFB)
PALE = RGBColor(0xF5, 0xF8, 0xFC)
GREY = RGBColor(0x5B, 0x6B, 0x7F)
MIDGREY = RGBColor(0x8A, 0x99, 0xAA)
RED = RGBColor(0xC0, 0x39, 0x2B)
AMBER = RGBColor(0xB7, 0x79, 0x1F)
GREEN = RGBColor(0x1E, 0x84, 0x49)
WHITE = RGBColor(0xFF, 0xFF, 0xFF)


def set_run(run, size=8, color=NAVY, bold=False):
    run.font.size = Pt(size)
    run.font.bold = bold
    run.font.color.rgb = color
    run.font.name = 'Times New Roman'
    rPr = run._r.get_or_add_rPr()
    ea = rPr.find(qn('a:ea'))
    if ea is None:
        ea = etree.SubElement(rPr, qn('a:ea'))
    ea.set('typeface', '宋体')
    cs = rPr.find(qn('a:cs'))
    if cs is None:
        cs = etree.SubElement(rPr, qn('a:cs'))
    cs.set('typeface', '宋体')


def add_text(slide, x, y, w, h, text, size=8, color=NAVY, bold=False,
             align=PP_ALIGN.LEFT, anchor=MSO_ANCHOR.TOP, line_spacing=1.10):
    box = slide.shapes.add_textbox(Inches(x), Inches(y), Inches(w), Inches(h))
    tf = box.text_frame
    tf.word_wrap = True
    tf.margin_left = tf.margin_right = tf.margin_top = tf.margin_bottom = 0
    tf.vertical_anchor = anchor
    for i, line in enumerate(str(text).split('\n')):
        p = tf.paragraphs[0] if i == 0 else tf.add_paragraph()
        p.alignment = align
        p.line_spacing = line_spacing
        r = p.add_run(); r.text = line
        set_run(r, size, color, bold)
    return box


def add_rect(slide, x, y, w, h, fill, line=None, radius=False):
    shp = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE if radius else MSO_SHAPE.RECTANGLE,
                                 Inches(x), Inches(y), Inches(w), Inches(h))
    shp.fill.solid(); shp.fill.fore_color.rgb = fill
    if line:
        shp.line.color.rgb = line; shp.line.width = Pt(0.75)
    else:
        shp.line.fill.background()
    if radius:
        try:
            shp.adjustments[0] = 0.08
        except Exception:
            pass


prs = Presentation(SRC)
blank = prs.slide_layouts[1]
s = prs.slides.add_slide(blank)
for shape in list(s.shapes):
    if shape.is_placeholder:
        shape._element.getparent().remove(shape._element)

add_rect(s, 0, 0, 13.333, 0.06, BLUE)
if os.path.exists(LOGO):
    s.shapes.add_picture(LOGO, Inches(0.48), Inches(0.22), height=Inches(0.34))
add_text(s, 0.48, 0.60, 12.0, 0.42, '月报后续事项归纳', size=19, color=NAVY, bold=True)
add_text(s, 0.48, 1.03, 12.3, 0.28,
         '四类清单｜截至 2026-11-08｜详细进展见 P8–P19',
         size=9.2, color=GREY)

cards = [
    ('卡点项目', RED, [
        '圣湘呼六：4℃7d -19% vs 15%，CY5/空白起线（赵云浩/研发，11/30）',
        '圣湘呼吸道NA：4种背景菌污染，重产洁净批次（赵云浩/质量，11/30）',
        '圣湘公卫：1500/4000ml采购预期，质量协议/放行标准未定（刘子研/质量，11/30）',
        '明日达829/M2244：冻干待测；低浓度复现但复测不稳，客诉未闭环（赵云浩，11/30）',
        '科华Tth：3靶标不起线，改引探重测；M4211-3资料未齐（刘子研，12/31）',
        '天隆ATP/万孚POCT/明德冻干：意愿低/未上机/堆叠层数待验证（11/30）',
    ]),
    ('资料进度', AMBER, [
        '圣湘 16S/18S覆盖菌种 + 出厂放行标准（刘子研/质量，11/30）',
        '科华 M4211-3 COA/MSDS/稳定性数据（刘子研，11/30）',
        '艾德 肿瘤SNP Takara对标/多批次数据（春芳/何弦，11/30）',
        '佰奥莱博 CAPA + 大小标签整改（刘子研/质量，11/30）',
        '天隆 003006 出货标准/货号清单（刘子研，11/30）',
        '销售拓展部10月月会纪要待补（销售支持，待定）',
    ]),
    ('管理改善', GREEN, [
        '测试单→销售单：Q&A知识库11/30；二单15组/25组（刘欣/吴云/韩远怀）',
        '产品线测试单：每周一更新；负向产品线经理说明（产品经理/区域经理）',
        '大客户过程：日反馈/拉通表/周中周末短会（刘新元/刘子研/赵云浩）',
        '团队60%：刘欣8/11；生命科学0/4、0/5，按人补位（11/30）',
        '回款：前10户对账—承诺—回款；60天以上经理上门（11/30）',
        '2027计划V2：11月底出第二版；存量60–80%、突破10–30%、探索10%',
    ]),
    ('其他', BLUE, [
        '年度目标：3,828.70/6,650万，57.6%；缺口2,821.30（12/31）',
        '11月目标：638.58万；大客户200.74/拓展343.74/科研94.10（11/30）',
        '之江：003006定版；2.5万T合同，2万T已发，100T小样/5000T待（11/30）',
        '安图：E10 40ml+500ml备货，价格谈判（赵云浩，11/30）',
        '11月重点拜访/新客户：明日达/万孚/安图/圣湘/普泰/科蒂亚/新产业/艾德等',
    ]),
]

positions = [(0.48, 1.42), (6.80, 1.42), (0.48, 4.12), (6.80, 4.12)]
for (title, color, items), (x, y) in zip(cards, positions):
    add_rect(s, x, y, 6.05, 2.55, WHITE, line=RGBColor(0xD9, 0xE2, 0xEE), radius=True)
    add_rect(s, x, y, 0.07, 2.55, color)
    add_text(s, x + 0.22, y + 0.12, 5.6, 0.25, title, size=10.8, color=color, bold=True)
    add_text(s, x + 0.22, y + 0.42, 5.65, 2.02, ['• ' + it for it in items], size=7.8, color=NAVY, line_spacing=1.12)

add_rect(s, 0.48, 6.82, 12.35, 0.12, WHITE)
add_text(s, 0.48, 6.88, 12.2, 0.18,
         '注：本页是归纳页；逐项卡点、进度、责任人仍以 P8–P19 明细为准。',
         size=7.0, color=MIDGREY)
add_rect(s, 0.48, 7.02, 12.35, 0.012, RGBColor(0xD9, 0xE2, 0xEE))
add_text(s, 0.48, 7.10, 9.9, 0.22, '来源：10月月报、Q3承接页、11月月会纪要、赵云浩/刘子研11月W1周报。', size=7.0, color=MIDGREY)
add_text(s, 10.55, 7.10, 2.25, 0.22, '守护生命健康，缔造美好生活', size=7.5, color=BLUE, bold=True, align=PP_ALIGN.RIGHT)

# insert before end slide (currently last)
sldIdLst = prs.slides._sldIdLst
new_id = sldIdLst[-1]
sldIdLst.remove(new_id)
sldIdLst.insert(len(sldIdLst) - 1, new_id)

prs.save(SRC)
print('saved in place', SRC)
print('slides', len(prs.slides))
