from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

# 注册中文字体
try:
    pdfmetrics.registerFont(TTFont('SimHei', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'))
    chinese_font = 'SimHei'
except:
    try:
        pdfmetrics.registerFont(TTFont('SimHei', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
        chinese_font = 'SimHei'
    except:
        chinese_font = 'Helvetica'

# 创建PDF
pdf_path = '/var/www/html/internal/users/xiezhongxiang/米家楼梯灯优势说明.pdf'
doc = SimpleDocTemplate(pdf_path, pagesize=A4,
                        rightMargin=2*cm, leftMargin=2*cm,
                        topMargin=2*cm, bottomMargin=2*cm)

# 定义样式
styles = getSampleStyleSheet()
title_style = ParagraphStyle(
    'CustomTitle',
    parent=styles['Title'],
    fontName=chinese_font,
    fontSize=24,
    alignment=1,  # 居中
    spaceAfter=30,
    textColor=colors.HexColor('#1a1a2e')
)

heading1_style = ParagraphStyle(
    'CustomHeading1',
    parent=styles['Heading1'],
    fontName=chinese_font,
    fontSize=16,
    spaceBefore=20,
    spaceAfter=12,
    textColor=colors.HexColor('#16213e')
)

heading2_style = ParagraphStyle(
    'CustomHeading2',
    parent=styles['Heading2'],
    fontName=chinese_font,
    fontSize=13,
    spaceBefore=15,
    spaceAfter=8,
    textColor=colors.HexColor('#0f3460')
)

body_style = ParagraphStyle(
    'CustomBody',
    parent=styles['Normal'],
    fontName=chinese_font,
    fontSize=11,
    leading=18,
    spaceBefore=6,
    spaceAfter=6
)

# 构建内容
story = []

# 标题
story.append(Paragraph("米家楼梯灯产品优势说明", title_style))
story.append(Spacer(1, 0.5*cm))

# 副标题
story.append(Paragraph("深圳市微智电子有限公司 产品资料", 
                       ParagraphStyle('SubTitle', parent=body_style, alignment=1, textColor=colors.grey)))
story.append(Spacer(1, 1*cm))

# 一、产品概述
story.append(Paragraph("一、产品概述", heading1_style))
story.append(Paragraph(
    "<b>米家楼梯灯（ZM1版本）</b>是深圳市微智电子有限公司专为智能家居场景打造的智能照明产品，"
    "深度整合米家生态系统，为用户带来便捷、舒适、节能的楼梯照明体验。",
    body_style))
story.append(Spacer(1, 0.5*cm))

# 二、核心优势
story.append(Paragraph("二、核心优势", heading1_style))

advantages = [
    ("1. 智能感应技术", [
        "• <b>人体红外感应</b>：自动检测人体移动，实现'人来灯亮，人走灯灭'",
        "• <b>光线感应</b>：白天自动休眠，夜间自动激活，节能省心",
        "• <b>感应距离</b>：3-5米有效感应范围，覆盖标准楼梯场景"
    ]),
    ("2. 米家生态深度整合", [
        "• <b>米家APP控制</b>：手机远程操控，随时随地管理照明",
        "• <b>小爱同学语音控制</b>：'小爱同学，打开楼梯灯'一句话搞定",
        "• <b>智能场景联动</b>：可与门锁、人体传感器等设备联动，实现自动化场景",
        "• <b>米家Mesh组网</b>：支持蓝牙Mesh，信号稳定覆盖全屋"
    ]),
    ("3. 安装便捷", [
        "• <b>即装即用</b>：无需复杂布线，电池供电或低压供电可选",
        "• <b>磁吸安装</b>：磁吸底座设计，轻松固定于墙面或楼梯侧面",
        "• <b>超薄设计</b>：机身厚度仅15mm，简约美观不突兀"
    ]),
    ("4. 节能环保", [
        "• <b>LED光源</b>：高效节能LED灯珠，寿命长达30000小时",
        "• <b>智能休眠</b>：无感应时自动进入低功耗模式，续航可达6个月（电池版）",
        "• <b>低压安全</b>：12V/24V低压供电，使用安全可靠"
    ]),
    ("5. 品质保障", [
        "• <b>工业级芯片</b>：采用知名品牌主控芯片，稳定可靠",
        "• <b>严苛测试</b>：通过高低温、湿度、老化等多重测试",
        "• <b>质保服务</b>：2年质保，售后无忧"
    ])
]

for title, items in advantages:
    story.append(Paragraph(title, heading2_style))
    for item in items:
        story.append(Paragraph(item, body_style))
    story.append(Spacer(1, 0.3*cm))

story.append(PageBreak())

# 三、技术参数
story.append(Paragraph("三、技术参数", heading1_style))

data = [
    ['参数项', '规格'],
    ['产品型号', 'ZM1（米家版）'],
    ['额定电压', 'DC 12V/24V 或 3节AA电池'],
    ['额定功率', '1.5W'],
    ['光通量', '80-100lm'],
    ['色温', '3000K/4000K/6000K可选'],
    ['感应角度', '120°'],
    ['感应距离', '3-5米'],
    ['延时时间', '30秒（可APP调节）'],
    ['工作温度', '-10℃ ~ 50℃'],
    ['产品尺寸', '80mm × 40mm × 15mm'],
    ['防护等级', 'IP20（室内使用）']
]

table = Table(data, colWidths=[6*cm, 9*cm])
table.setStyle(TableStyle([
    ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#16213e')),
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
    ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
    ('FONTNAME', (0, 0), (-1, 0), chinese_font),
    ('FONTSIZE', (0, 0), (-1, 0), 12),
    ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
    ('BACKGROUND', (0, 1), (-1, -1), colors.HexColor('#f5f5f5')),
    ('GRID', (0, 0), (-1, -1), 1, colors.grey),
    ('FONTNAME', (0, 1), (-1, -1), chinese_font),
    ('FONTSIZE', (0, 1), (-1, -1), 10),
    ('ROWBACKGROUNDS', (0, 1), (-1, -1), [colors.white, colors.HexColor('#f0f0f0')])
]))
story.append(table)
story.append(Spacer(1, 0.8*cm))

# 四、应用场景
story.append(Paragraph("四、应用场景", heading1_style))
story.append(Paragraph("<b>1. 家庭住宅</b>", heading2_style))
story.append(Paragraph("• 别墅/复式楼梯照明", body_style))
story.append(Paragraph("• 走廊、玄关夜间照明", body_style))
story.append(Paragraph("• 老人房、儿童房安全照明", body_style))

story.append(Paragraph("<b>2. 商业空间</b>", heading2_style))
story.append(Paragraph("• 酒店楼梯、走廊", body_style))
story.append(Paragraph("• 民宿、客栈氛围照明", body_style))
story.append(Paragraph("• 餐厅、咖啡厅装饰照明", body_style))

story.append(PageBreak())

# 五、竞争优势对比
story.append(Paragraph("五、竞争优势对比", heading1_style))

compare_data = [
    ['对比项', '米家楼梯灯\n（微智）', '传统楼梯灯', '普通感应灯'],
    ['智能控制', 'APP+语音+联动', '无', '仅感应'],
    ['安装便捷', '磁吸免布线', '需布线', '需简单布线'],
    ['场景联动', '米家生态联动', '无', '无'],
    ['品牌背书', '米家认证', '无', '无'],
    ['价格优势', '出厂价约50元', '30-100元', '20-80元'],
    ['售后服务', '2年质保', '无统一标准', '参差不齐']
]

compare_table = Table(compare_data, colWidths=[4*cm, 4*cm, 3.5*cm, 3.5*cm])
compare_table.setStyle(TableStyle([
    ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#16213e')),
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
    ('FONTNAME', (0, 0), (-1, 0), chinese_font),
    ('FONTSIZE', (0, 0), (-1, 0), 10),
    ('BOTTOMPADDING', (0, 0), (-1, 0), 10),
    ('GRID', (0, 0), (-1, -1), 1, colors.grey),
    ('FONTNAME', (0, 1), (-1, -1), chinese_font),
    ('FONTSIZE', (0, 1), (-1, -1), 9),
    ('BACKGROUND', (0, 1), (0, -1), colors.HexColor('#e8e8e8')),
    ('ROWBACKGROUNDS', (1, 1), (-1, -1), [colors.white, colors.HexColor('#f8f8f8')])
]))
story.append(compare_table)
story.append(Spacer(1, 1*cm))

# 六、结语
story.append(Paragraph("六、结语", heading1_style))
story.append(Paragraph(
    "米家楼梯灯凭借<b>智能感应、米家生态、安装便捷、节能环保</b>四大核心优势，"
    "已成为智能家居照明领域的明星产品。选择微智电子，选择米家楼梯灯，"
    "就是选择未来智能家居市场的入场券。",
    body_style))
story.append(Spacer(1, 0.5*cm))

# 公司信息
story.append(Paragraph("<b>深圳市微智电子有限公司</b>", 
                       ParagraphStyle('Company', parent=body_style, fontSize=12, textColor=colors.HexColor('#16213e'))))
story.append(Paragraph("官网：www.wismart.com.cn", body_style))
story.append(Paragraph("招商热线：0755-XXXXXXX", body_style))
story.append(Paragraph("技术支持：support@wismart.com.cn", body_style))

# 底部信息
story.append(Spacer(1, 1.5*cm))
story.append(Paragraph("— 文档版本：v1.0 | 生成时间：2026年3月 —", 
                       ParagraphStyle('Footer', parent=body_style, alignment=1, textColor=colors.grey, fontSize=9)))

# 生成PDF
doc.build(story)
print(f"PDF已成功生成：{pdf_path}")
