# -*- coding: utf-8 -*-
"""
Chookoo 产品网站内容填写模板 - Excel 生成器
运行: python generate_template.py
输出: 产品网站内容填写模板.xlsx
"""
import sys
sys.stdout.reconfigure(encoding="utf-8")
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter
from _build_data import slides, hp, lp, sd, article_pages, app_fields, help_articles

wb = Workbook()
thin = Side(style="thin", color="CCCCCC")
bd = Border(left=thin, top=thin, right=thin, bottom=thin)
hf = Font(name="Microsoft YaHei", size=11, bold=True, color="FFFFFF")
hfl = PatternFill(start_color="2F6BFF", end_color="2F6BFF", fill_type="solid")
sf = Font(name="Microsoft YaHei", size=10, bold=True, color="FFFFFF")
sfl = PatternFill(start_color="4B5563", end_color="4B5563", fill_type="solid")
nf = Font(name="Microsoft YaHei", size=10)
bf = Font(name="Microsoft YaHei", size=10, bold=True)
xf = Font(name="Microsoft YaHei", size=10, color="888888")
rf = Font(name="Microsoft YaHei", size=10, color="DC2626", bold=True)
inp = PatternFill(start_color="FEF3C7", end_color="FEF3C7", fill_type="solid")
rev = PatternFill(start_color="DBEAFE", end_color="DBEAFE", fill_type="solid")
wa = Alignment(wrap_text=True, vertical="center", horizontal="left")
ca = Alignment(wrap_text=True, vertical="center", horizontal="center")

def hdr(ws, row, cols):
    for c in range(1, cols+1):
        cl = ws.cell(row=row, column=c); cl.font = hf; cl.fill = hfl; cl.alignment = ca; cl.border = bd

def sec(ws, row, cols, txt=""):
    ws.merge_cells(start_row=row, start_column=1, end_row=row, end_column=cols)
    cl = ws.cell(row=row, column=1, value=txt); cl.font = sf; cl.fill = sfl; cl.alignment = Alignment(vertical="center")
    for c in range(1, cols+1): ws.cell(row=row, column=c).border = bd

def rw(ws, r, vals, fill_cols=None, ft="inp"):
    for c, v in enumerate(vals, 1):
        cl = ws.cell(row=r, column=c, value=v); cl.font = nf; cl.alignment = wa; cl.border = bd
        if fill_cols and c in fill_cols: cl.fill = inp if ft == "inp" else rev

def sw(ws, widths):
    for i, w in enumerate(widths, 1): ws.column_dimensions[get_column_letter(i)].width = w

# ========== Sheet 0: 填写说明 ==========
ws0 = wb.active; ws0.title = "填写说明"; ws0.sheet_properties.tabColor = "EF4444"
sw(ws0, [90, 70])
ws0.cell(row=1, column=1, value="Chookoo 产品网站 - 内容填写说明").font = Font(name="Microsoft YaHei", size=14, bold=True, color="2F6BFF")
info = [("", ""), ("颜色含义", ""), ("深黄底色", "必填 - 空着会导致网页显示异常"), ("浅蓝底色", "仅审核 - 已有内容需确认"),
    ("", ""), ("工作表说明", ""),
    ("一、轮播图", "首页8张轮播幻灯片(产品/活动/公告)，中英文+图片"),
    ("二、产品卡片", "首页5张+列表页14张卡片，中英文标题/描述+图片"),
    ("三、产品详情", "14个产品详情页: 标签/亮点/特性/规格/图片"),
    ("四、宠物故事", "6个故事: 卡片摘要+详情页5段正文+3亮点+图片"),
    ("五、文章-公司介绍", "5篇(全是占位需大幅扩充)"),
    ("六、文章-合作伙伴", "5篇(全是占位需大幅扩充)"),
    ("七、文章-法律条款", "3篇(全是占位需法务起草)"),
    ("八、文章-App页面", "基本完成需审核数据+补充下载链接"),
    ("九、文章-帮助中心", "21篇已有详细文章仅审核确认"),
    ("", ""), ("图片尺寸速查", "")]
r = 3
for a, b in info:
    ws0.cell(row=r, column=1, value=a).font = bf if a else nf
    ws0.cell(row=r, column=2, value=b).font = nf; r += 1
iref = [("图片类型","建议源图尺寸","比例","格式","透明背景"),("轮播图主图","800x800px","1:1","PNG","是"),
    ("轮播图角标","200x200px","1:1","PNG","是"),("产品卡片图","400x400px","1:1","PNG","是"),
    ("展示区大图","800x1000px","4:5","PNG","是"),("详情页主图","800x800px","1:1","PNG","是"),
    ("故事封面","800x450px","16:9","JPG","否"),("故事头像","80x80px","1:1","JPG/PNG","否(圆形)"),
    ("二维码","240x240px","1:1","PNG","否")]
for i, vals in enumerate(iref):
    for c, v in enumerate(vals, 1):
        cl = ws0.cell(row=r, column=c, value=v); cl.font = hf if i==0 else nf
        if i==0: cl.fill = hfl
        cl.border = bd; cl.alignment = wa
    r += 1

# ========== Sheet 1: 轮播图 ==========
ws1 = wb.create_sheet("一、轮播图"); ws1.sheet_properties.tabColor = "06B6D4"
sw(ws1, [10, 22, 50, 8, 35, 35])
for c, h in enumerate(["编号","字段名","填写说明","必填","中文","英文"], 1): ws1.cell(row=1, column=c, value=h)
hdr(ws1, 1, 6); r = 2
for idx, stype, img in slides:
    sec(ws1, r, 6, f"幻灯片{idx}(类型:{stype})"); r += 1
    fields = [(f"S{idx}-1","类型","product/activity/notice","是",stype,stype),
        (f"S{idx}-2","中文主标题","产品名或活动标题","是","",""),
        (f"S{idx}-3","中文副标题","一句话卖点","是","",""),
        (f"S{idx}-4","英文主标题","English title","是","",""),
        (f"S{idx}-5","英文副标题","English subtitle","是","","")]
    if stype in ("activity","notice"):
        fields += [(f"S{idx}-6","中文详情","活动/公告内容","是","",""),(f"S{idx}-7","英文详情","English details","是","","")]
    for vals in fields: rw(ws1, r, vals, fill_cols={5,6}); r += 1
    rw(ws1, r, [f"S{idx}-IMG","主图","800x800px PNG透明","是","当前:",img or "(无)"])
    ws1.cell(row=r, column=5).font = xf; r += 1; r += 1

# ========== Sheet 2: 产品卡片 ==========
ws2 = wb.create_sheet("二、产品卡片"); ws2.sheet_properties.tabColor = "10B981"
sw(ws2, [10, 18, 48, 8, 35, 35])
sec(ws2, 1, 6, "首页产品卡片(5张)"); r = 2
for c, h in enumerate(["编号","字段名","填写说明","必填","中文","英文"], 1): ws2.cell(row=r, column=c, value=h)
hdr(ws2, r, 6); r += 1
for pid, img, name in hp:
    sec(ws2, r, 6, f"产品{pid}: {name}"); r += 1
    for vals in [(f"HP{pid}-1","名称","不超过8字","是",name,""),(f"HP{pid}-2","副标题","不超过20字","是","",""),
        (f"HP{pid}-3","特性1","不超过15字","是","",""),(f"HP{pid}-4","特性2","不超过15字","是","",""),
        (f"HP{pid}-5","特性3","不超过15字","是","",""),(f"HP{pid}-6","特性4","不超过15字","是","","")]:
        rw(ws2, r, vals, fill_cols={5,6}); r += 1
    rw(ws2, r, [f"HP{pid}-IMG","图片","400x400px PNG","是","当前:",img])
    ws2.cell(row=r, column=5).font = xf; r += 2
sec(ws2, r, 6, "列表页卡片(14张)"); r += 1
for c, h in enumerate(["编号","字段名","填写说明","必填","中文","英文"], 1): ws2.cell(row=r, column=c, value=h)
hdr(ws2, r, 6); r += 1
for pid, name, img, tags in lp:
    rw(ws2, r, [f"PL-{pid}","名称",pid,"是",name,""], fill_cols={5,6}); r += 1
    rw(ws2, r, [f"PL-{pid}D","描述","一句话","是","",""], fill_cols={5,6}); r += 1
    rw(ws2, r, [f"PL-{pid}T","标签","逗号分隔","是",tags,""], fill_cols={5,6}); r += 1
    rw(ws2, r, [f"PL-{pid}I","图片","400x400px PNG","是","当前:",img])
    ws2.cell(row=r, column=5).font = xf; r += 2

# ========== Sheet 3: 产品详情 ==========
ws3 = wb.create_sheet("三、产品详情"); ws3.sheet_properties.tabColor = "7C3AED"
sw(ws3, [10, 16, 42, 14, 28, 28])
for c, h in enumerate(["编号","字段名","填写说明","图标颜色","中文","英文"], 1): ws3.cell(row=1, column=c, value=h)
hdr(ws3, 1, 6); r = 2
for pid, name, img, tags in lp:
    sec(ws3, r, 6, f"产品:{name}(ID:{pid})"); r += 1
    rw(ws3, r, ["","标签","逗号分隔","","",tags,""], fill_cols={5,6}); r += 1
    rw(ws3, r, ["","副标题","不超过25字","","",""], fill_cols={5,6}); r += 1
    sec(ws3, r, 6, "亮点(4条)"); r += 1
    for i in range(1,5): rw(ws3, r, [f"H{i}",f"亮点{i}","不超过15字","","",""], fill_cols={5,6}); r += 1
    sec(ws3, r, 6, "特性(4组 blue=技术 green=环保 purple=智能 orange=安全)"); r += 1
    for i in range(1,5):
        rw(ws3, r, [f"F{i}","标题","不超过8字","填颜色","",""], fill_cols={4,5,6}); r += 1
        rw(ws3, r, [f"F{i}d","描述","不超过25字","","",""], fill_cols={5,6}); r += 1
    sec(ws3, r, 6, "规格表(行数不限)"); r += 1
    for c, h in enumerate(["","参数名(中文)","参数名(英文)","","参数值",""], 1):
        cl = ws3.cell(row=r, column=c, value=h); cl.font = bf
        cl.fill = PatternFill(start_color="E5E7EB", end_color="E5E7EB", fill_type="solid"); cl.border = bd
    r += 1
    for _ in range(8): rw(ws3, r, ["","","","","",""], fill_cols={2,3,5}); r += 1
    rw(ws3, r, ["","主图","800x800px PNG","","当前:",img])
    ws3.cell(row=r, column=5).font = xf; r += 2

# ========== Sheet 4: 宠物故事 ==========
ws4 = wb.create_sheet("四、宠物故事"); ws4.sheet_properties.tabColor = "EA580C"
sw(ws4, [10, 18, 48, 8, 40, 40])
sec(ws4, 1, 6, "故事卡片(6个,首页随机3个)"); r = 2
for c, h in enumerate(["编号","字段名","填写说明","必填","中文","英文"], 1): ws4.cell(row=r, column=c, value=h)
hdr(ws4, r, 6); r += 1
for sid, title, img, av, cat, pet, auth, loc, date in sd:
    sec(ws4, r, 6, f"故事:{title}"); r += 1
    for vals in [(f"{sid}-1","标题","不超过12字","是",title,""),(f"{sid}-2","摘要","2-3句不超过60字","是","",""),
        (f"{sid}-3","标签1","分类","是",cat,""),(f"{sid}-4","标签2","可选","否","",""),
        (f"{sid}-5","作者","如:小橘妈妈","是",auth,""),(f"{sid}-6","宠物","品种年龄","是",pet+" · ","")]:
        rw(ws4, r, vals, fill_cols={5,6}); r += 1
    rw(ws4, r, [f"{sid}-IMG","封面图","800x450px JPG","是","当前:",img])
    ws4.cell(row=r, column=5).font = xf; r += 1
    rw(ws4, r, [f"{sid}-AV","头像","80x80px","是","当前:",av])
    ws4.cell(row=r, column=5).font = xf; r += 2
sec(ws4, r, 6, "故事详情正文(每个5段+3亮点)"); r += 1
for c, h in enumerate(["编号","字段名","填写说明","必填","中文","英文"], 1): ws4.cell(row=r, column=c, value=h)
hdr(ws4, r, 6); r += 1
for sid, title, img, av, cat, pet, auth, loc, date in sd:
    sec(ws4, r, 6, f"正文:{title}"); r += 1
    rw(ws4, r, [f"{sid}-CAT","分类","","是",cat,""], fill_cols={5,6}); r += 1
    rw(ws4, r, [f"{sid}-LOC","地点","城市","是",loc,""], fill_cols={5,6}); r += 1
    rw(ws4, r, [f"{sid}-DATE","日期","如2024.03","是",date,""], fill_cols={5,6}); r += 1
    for i in range(1,6):
        rw(ws4, r, [f"{sid}-P{i}",f"第{i}段","3-5句话","是","",""], fill_cols={5,6}); r += 1
    sec(ws4, r, 6, "核心亮点(3条)"); r += 1
    for i in range(1,4): rw(ws4, r, [f"{sid}-HL{i}",f"亮点{i}","一句话","是","",""], fill_cols={5,6}); r += 1
    r += 1

# ========== Sheet 5-7: 文章系统 ==========
for st, color, articles, warn in article_pages:
    ws = wb.create_sheet(st); ws.sheet_properties.tabColor = color; sw(ws, [10, 20, 48, 8, 55, 55])
    for c, h in enumerate(["编号","字段名","填写说明","必填","中文","英文"], 1): ws.cell(row=1, column=c, value=h)
    hdr(ws, 1, 6)
    ws.merge_cells(start_row=2, start_column=1, end_row=2, end_column=6)
    ws.cell(row=2, column=1, value="注意: "+warn).font = rf; r = 3
    for aid, atitle, adesc in articles:
        sec(ws, r, 6, f"文章{aid}:{atitle}"); r += 1
        rw(ws, r, [f"A{aid}-1","标题","","是",atitle,""], fill_cols={5,6}); r += 1
        for i in range(1,6):
            rw(ws, r, [f"A{aid}-P{i}",f"第{i}段",adesc if i==1 else "3-5句话","是","",""], fill_cols={5,6}); r += 1
        r += 1

# ========== Sheet 8: App ==========
ws8 = wb.create_sheet("八、文章-App页面"); ws8.sheet_properties.tabColor = "3B82F6"
sw(ws8, [10, 22, 48, 8, 40, 40])
for c, h in enumerate(["编号","字段名","填写说明","必填","当前内容","需确认"], 1): ws8.cell(row=1, column=c, value=h)
hdr(ws8, 1, 6); r = 2
sec(ws8, r, 6, "App文案(基本完成请审核)"); r += 1
for vals in app_fields:
    ft = "rev" if "审核" in str(vals[3]) else "inp"
    fc = {6} if "审核" in str(vals[3]) else {6}
    rw(ws8, r, vals, fill_cols=fc, ft=ft); r += 1

# ========== Sheet 9: 帮助中心 ==========
ws9 = wb.create_sheet("九、文章-帮助中心"); ws9.sheet_properties.tabColor = "64748B"
sw(ws9, [10, 14, 42, 8, 55])
for c, h in enumerate(["编号","分类","标题","状态","审核确认"], 1): ws9.cell(row=1, column=c, value=h)
hdr(ws9, 1, 5); r = 2
sec(ws9, r, 5, "21篇已有详细文章仅需审核确认"); r += 1
for cat, info in help_articles:
    aid, atitle = info.split("|")
    rw(ws9, r, [aid, cat, atitle, "已有详细内容", ""], fill_cols={5}, ft="rev"); r += 1

# ========== 保存 ==========
for ws in wb.worksheets:
    ws.freeze_panes = "A2"; ws.row_dimensions[1].height = 28
wb.move_sheet("填写说明", offset=-(len(wb.sheetnames)-1))
output = "产品网站内容填写模板.xlsx"
wb.save(output)
print(f"OK - {output}")
print(f"Sheets ({len(wb.sheetnames)}): {', '.join(wb.sheetnames)}")
