#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
生成出货单数据表格图片
"""

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import FancyBboxPatch
import matplotlib
matplotlib.rcParams['font.family'] = 'DejaVu Sans'
matplotlib.rcParams['axes.unicode_minus'] = False

# 数据
items = [
    ("ZD260429050001", 0.528, 204), ("ZD260429050002", 0.578, 204), ("ZD260429050003", 0.628, 204),
    ("ZD260429050004", 0.678, 204), ("ZD260429050005", 0.728, 204), ("ZD260429050006", 0.778, 204),
    ("ZD260429050007", 0.828, 204), ("ZD260429050008", 0.878, 204), ("ZD260429050009", 0.928, 204),
    ("ZD260429050010", 0.978, 204), ("ZD260429050011", 1.028, 204), ("ZD260429050012", 1.078, 204),
    ("ZD260429050013", 1.128, 204), ("ZD260429050014", 1.178, 204), ("ZD260429050015", 1.228, 204),
    ("ZD260429050016", 1.278, 204), ("ZD260429050017", 1.328, 204), ("ZD260429050018", 1.378, 204),
    ("ZD260429050019", 1.428, 204), ("ZD260429050020", 1.478, 204), ("ZD260429050021", 1.528, 614),
    ("ZD260429050022", 2.028, 614), ("ZD260429050023", 3.000, 614)
]

# 创建图表
fig, ax = plt.subplots(figsize=(14, 16))
ax.set_xlim(0, 10)
ax.set_ylim(0, 28)
ax.axis('off')

# 标题
ax.text(5, 27, 'Shipment Data Summary', fontsize=22, ha='center', fontweight='bold')
ax.text(5, 26.3, 'CK26053015 | 2026-05-30 | ZhiYao Star | ZH-10X10SJ', fontsize=12, ha='center', color='gray')

# 表头
header_y = 25
headers = ['No.', 'Item Code', 'Length (m)', 'Qty (pcs)']
col_widths = [1, 4, 2.5, 2.5]
col_starts = [0.5, 1.5, 5.5, 8]

# 绘制表头背景
header_box = FancyBboxPatch((0.3, header_y-0.4), 9.4, 0.8, 
                             boxstyle="round,pad=0.02", 
                             facecolor='#4472C4', edgecolor='none', alpha=0.9)
ax.add_patch(header_box)

for i, (header, x) in enumerate(zip(headers, col_starts)):
    ax.text(x, header_y, header, fontsize=12, ha='left' if i==1 else 'center', 
            fontweight='bold', color='white')

# 绘制数据行
row_height = 0.9
start_y = header_y - 1

for idx, (code, length, qty) in enumerate(items):
    y = start_y - idx * row_height
    
    # 交替行背景
    if idx % 2 == 0:
        row_bg = FancyBboxPatch((0.3, y-0.35), 9.4, row_height-0.1, 
                                 boxstyle="round,pad=0.01", 
                                 facecolor='#F2F2F2', edgecolor='none', alpha=0.5)
        ax.add_patch(row_bg)
    
    # 高亮614数量的行
    if qty == 614:
        highlight = FancyBboxPatch((0.3, y-0.35), 9.4, row_height-0.1, 
                                    boxstyle="round,pad=0.01", 
                                    facecolor='#FCE4D6', edgecolor='#ED7D31', alpha=0.6, linewidth=2)
        ax.add_patch(highlight)
    
    # 数据
    ax.text(col_starts[0], y, str(idx+1), fontsize=10, ha='center', va='center')
    ax.text(col_starts[1], y, code, fontsize=10, ha='left', va='center', family='monospace')
    ax.text(col_starts[2], y, f'{length:.3f}', fontsize=10, ha='center', va='center', family='monospace')
    ax.text(col_starts[3], y, str(qty), fontsize=10, ha='center', va='center', fontweight='bold' if qty==614 else 'normal')

# 汇总区域
summary_y = start_y - len(items) * row_height - 1

# 汇总框
summary_box = FancyBboxPatch((0.5, summary_y-2), 9, 2.5, 
                              boxstyle="round,pad=0.1", 
                              facecolor='#FFF2CC', edgecolor='#D6B656', linewidth=2)
ax.add_patch(summary_box)

ax.text(5, summary_y-0.3, 'SUMMARY', fontsize=14, ha='center', fontweight='bold', color='#B7950B')
ax.text(2.5, summary_y-1, 'Total Items:', fontsize=11, ha='right')
ax.text(3, summary_y-1, '23', fontsize=14, ha='left', fontweight='bold')
ax.text(5.5, summary_y-1, 'Total Quantity:', fontsize=11, ha='right')
ax.text(6, summary_y-1, '5,922 pcs', fontsize=14, ha='left', fontweight='bold', color='#4472C4')

ax.text(2.5, summary_y-1.8, '204 pcs items:', fontsize=10, ha='right', color='gray')
ax.text(3, summary_y-1.8, '20', fontsize=11, ha='left', color='gray')
ax.text(5.5, summary_y-1.8, '614 pcs items:', fontsize=10, ha='right', color='gray')
ax.text(6, summary_y-1.8, '3', fontsize=11, ha='left', color='gray')

plt.tight_layout()
plt.savefig('/mnt/data/we-smart/users/limaolin/exports/shipment_table.png', 
            dpi=150, bbox_inches='tight', facecolor='white')
print("Table saved: /mnt/data/we-smart/users/limaolin/exports/shipment_table.png")
