from pathlib import Path

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.shared import Inches, Pt, RGBColor


WORKING = Path("/tmp/vwcg-1297-docx-evidence/manual-working.docx")
OUTPUT = Path("/Users/guodongwei/Documents/zhct/zhctprompt/work_store/manual/VWCG-1297/VWCG-1297-operation-page-manual.docx")
SCREENSHOT = Path("/Users/guodongwei/Documents/zhct/zhctprompt/work_store/manual/VWCG-1297/screenshots/operation-ring-center-1920x1080.png")


def set_run_font(run, size=10.5, bold=False, color=None):
    run.font.name = "Hiragino Sans GB"
    run._element.rPr.rFonts.set(qn("w:eastAsia"), "Hiragino Sans GB")
    run.font.size = Pt(size)
    run.font.bold = bold
    if color:
        run.font.color.rgb = RGBColor(*color)


def set_cell_shading(cell, fill):
    tc_pr = cell._tc.get_or_add_tcPr()
    shading = OxmlElement("w:shd")
    shading.set(qn("w:fill"), fill)
    tc_pr.append(shading)


document = Document(WORKING)

title = document.add_paragraph()
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
title.paragraph_format.space_after = Pt(6)
set_run_font(title.add_run("大屏运营页移植修复操作说明"), 18, True, (239, 92, 35))

meta = document.add_table(rows=2, cols=2)
meta.autofit = False
meta.columns[0].width = Inches(3.1)
meta.columns[1].width = Inches(3.1)
values = (
    ("云效任务：VWCG-1297", "工程：store/public/screen"),
    ("页面：127.0.0.1:8081/#/operation?type=1", "日期：2026-08-31"),
)
for row_index, row in enumerate(meta.rows):
    for col_index, cell in enumerate(row.cells):
        set_cell_shading(cell, "F3F6FA")
        paragraph = cell.paragraphs[0]
        paragraph.paragraph_format.space_after = Pt(0)
        set_run_font(paragraph.add_run(values[row_index][col_index]), 8.5)

heading = document.add_paragraph()
heading.paragraph_format.space_before = Pt(8)
heading.paragraph_format.space_after = Pt(3)
set_run_font(heading.add_run("操作步骤"), 12, True, (25, 74, 126))

steps = [
    "确认大屏前端开发服务已在本机 8081 端口运行。",
    "打开运营大屏地址并等待页面完成加载。",
    "检查顶部、左右数据区和中间菜品区均正常显示。",
    "无菜品或排行数据时，确认显示空状态且页面不白屏。",
    "检查就餐人数圆环总人数保持居中，字号随屏幕宽度按 1vw 缩放。",
]
for index, text in enumerate(steps, 1):
    paragraph = document.add_paragraph()
    paragraph.paragraph_format.left_indent = Inches(0.15)
    paragraph.paragraph_format.space_after = Pt(1)
    set_run_font(paragraph.add_run(f"{index}. {text}"), 9.5)

heading = document.add_paragraph()
heading.paragraph_format.space_before = Pt(5)
heading.paragraph_format.space_after = Pt(3)
set_run_font(heading.add_run("修复后页面"), 12, True, (25, 74, 126))

figure = document.add_paragraph()
figure.alignment = WD_ALIGN_PARAGRAPH.CENTER
figure.paragraph_format.space_after = Pt(2)
figure.add_run().add_picture(str(SCREENSHOT), width=Inches(6.05))

caption = document.add_paragraph()
caption.alignment = WD_ALIGN_PARAGRAPH.CENTER
caption.paragraph_format.space_after = Pt(4)
set_run_font(caption.add_run("图 1  1920×1080 下圆环总人数保持居中"), 8, False, (90, 90, 90))

heading = document.add_paragraph()
heading.paragraph_format.space_before = Pt(2)
heading.paragraph_format.space_after = Pt(2)
set_run_font(heading.add_run("验收结论"), 12, True, (25, 74, 126))

result = document.add_paragraph()
result.paragraph_format.space_after = Pt(0)
set_run_font(
    result.add_run("构建成功；页面接口 HTTP 200；浏览器控制台 0 错误、0 警告；圆环总人数在 1920×1080 与 1366×768 下保持同心，字号为 1vw。"),
    9.5,
)

document.save(OUTPUT)
print(OUTPUT)
