<?php
/**
 * WooCommerce Packing Slip Template
 * Modern Layout - Optimized for Dompdf with Bangla Unicode Support
 */

if (!defined('ABSPATH')) exit;

// $order and $settings are expected to be passed to this template
if (!isset($order) || !$order) {
    echo esc_html( 'No order data provided.' );
    return;
}

$active_order = $order;
$template_settings = $settings;

// Accent Color
$accent_color = $template_settings['wc_invoice_color'] ?? '#3b82f6';

// Shop Info
$shop_name = $template_settings['wc_invoice_shop_name'] ?? get_bloginfo('name');
$shop_address_1 = $template_settings['wc_invoice_shop_address_1'] ?? '';
$shop_address_2 = $template_settings['wc_invoice_shop_address_2'] ?? '';
$shop_city = $template_settings['wc_invoice_shop_city'] ?? '';
$shop_postcode = $template_settings['wc_invoice_shop_postcode'] ?? '';
$shop_country = $template_settings['wc_invoice_shop_country'] ?? '';
$shop_phone = $template_settings['wc_invoice_shop_phone'] ?? '';
$shop_email = $template_settings['wc_invoice_shop_email'] ?? '';

$items = is_string($active_order['item_data']) ? json_decode($active_order['item_data'], true) : $active_order['item_data'];
$items = is_array($items) ? $items : [];
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Packing Slip - <?php echo esc_html($active_order['order_no'] ?? $active_order['id']); ?></title>
    <style>
        @font-face {
            font-family: 'Hind Siliguri';
            font-style: normal;
            font-weight: 400;
            src: url(https://fonts.gstatic.com/s/hindsiliguri/v12/ijwaNjc6uy9eOTvAmZcjTmajGS6TjS0.ttf) format('truetype');
        }
        @font-face {
            font-family: 'Hind Siliguri';
            font-style: normal;
            font-weight: 700;
            src: url(https://fonts.gstatic.com/s/hindsiliguri/v12/ijwbNjc6uy9eOTvAmZcjTmajGf6fuzS_mQ.ttf) format('truetype');
        }
        @page {
            margin: 15mm 10mm;
        }
        * {
            box-sizing: border-box;
        }
        body { 
            font-family: 'Hind Siliguri', 'DejaVu Sans', sans-serif;
            font-size: 12px; 
            color: #374151; 
            margin: 0; 
            padding: 0;
            line-height: 1.4;
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        .container {
            padding: 0;
            max-width: 100%;
        }
        .accent-bar {
            height: 6px; 
            background-color: <?php echo esc_attr($accent_color); ?>;
            width: 100%;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }
        td, th {
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        .header-table {
            margin-bottom: 30px;
            table-layout: fixed;
        }
        .shop-name {
            font-size: 22px;
            font-weight: bold;
            color: <?php echo esc_attr($accent_color); ?>;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 4px;
        }
        .shop-info {
            font-size: 10px; 
            color: #6b7280;
            line-height: 1.3;
        }
        .document-type {
            font-size: 28px; 
            font-weight: 300;
            color: #d1d5db;
            text-align: right;
            text-transform: uppercase;
            vertical-align: middle;
        }
        .info-grid {
            margin-bottom: 30px;
            border-top: 1px solid #f3f4f6;
            border-bottom: 1px solid #f3f4f6;
            padding: 15px 0;
            table-layout: fixed;
        }
        .info-block {
            vertical-align: top;
            padding: 0 8px;
        }
        .label {
            font-size: 9px;
            font-weight: bold;
            text-transform: uppercase;
            color: #9ca3af;
            letter-spacing: 1px;
            margin-bottom: 8px;
            display: block;
        }
        .value {
            font-size: 12px; 
            color: #111827;
        }
        .items-table {
            width: 100%;
            margin-top: 10px;
            table-layout: fixed;
        }
        .items-table th {
            text-align: left;
            padding: 8px 10px;
            font-size: 10px;
            font-weight: bold;
            background: #f9fafb;
            color: #6b7280;
            border-bottom: 2px solid #e5e7eb;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .items-table td {
            padding: 12px 10px; 
            border-bottom: 1px solid #f3f4f6;
            vertical-align: top;
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        .qty-badge {
            display: inline-block;
            color: #111827;
            font-weight: bold;
            font-size: 13px; 
            text-align: center;
        }
        .notes-section {
            margin-top: 25px;
            padding: 12px 20px;
            background: #f9fafb;
            border-left: 4px solid <?php echo esc_attr($accent_color); ?>;
        }
        .footer {
            margin-top: 40px;
            padding-top: 15px;
            font-size: 10px;
            color: #9ca3af;
            text-align: center;
        }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        .font-bold { font-weight: bold; }
    </style>
</head>
<body>
    <div class="accent-bar"></div>
    
    <div class="container">
        <!-- Header -->
        <table class="header-table">
            <tr>
                <td width="60%">
                    <div class="shop-name"><?php echo esc_html($shop_name); ?></div>
                    <div class="shop-info">
                        <?php echo esc_html($shop_address_1); ?><?php echo $shop_address_2 ? ', ' . esc_html($shop_address_2) : ''; ?><br>
                        <?php echo esc_html($shop_city); ?><?php echo $shop_postcode ? ', ' . esc_html($shop_postcode) : ''; ?><br>
                        <?php if ($shop_phone) echo 'Tel: ' . esc_html($shop_phone) . ' | '; ?><?php if ($shop_email) echo esc_html($shop_email); ?>
                    </div>
                </td>
                <td width="40%" class="document-type">
                    Packing Slip
                </td>
            </tr>
        </table>

        <!-- Info Grid -->
        <table class="info-grid">
            <tr>
                <td width="40%" class="info-block" style="padding-left: 0;">
                    <span class="label">Ship To</span>
                    <div class="value font-bold" style="font-size: 13px; margin-bottom: 2px;">
                        <?php echo esc_html($active_order['contact_name']); ?>
                    </div>
                    <div class="value" style=" color: #4b5563;">
                        <?php echo nl2br(esc_html($active_order['contact_address'])); ?>
                    </div>
                </td>
                <td width="30%" class="info-block">
                    <span class="label">Order Details</span>
                    <div class="value">
                        <span style="color: #9ca3af;">Number:</span> #<?php echo esc_html($active_order['order_no'] ?? $active_order['id']); ?>
                    </div>
                    <div class="value" style="margin-top: 4px;">
                        <span style="color: #9ca3af;">Date:</span> <?php echo esc_html( date('M d, Y', strtotime($active_order['issued_at'])) ); ?>
                    </div>
                </td>
                <td width="30%" class="info-block" style="padding-right: 0;">
                    <span class="label">Delivery Method</span>
                    <div class="value">
                        <?php echo esc_html($active_order['shipping_method'] ?? 'Standard Delivery'); ?>
                    </div>
                </td>
            </tr>
        </table>

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th width="8%">#</th>
                    <th width="72%">Item Description</th>
                    <th width="20%" class="text-center">Qty</th>
                </tr>
            </thead>
            <tbody>
                <?php $i = 1; foreach ($items as $item): ?>
                <tr>
                    <td style="color: #d1d5db; font-weight: bold;"><?php echo esc_html( str_pad( $i++, 2, '0', STR_PAD_LEFT ) ); ?></td>
                    <td>
                        <div class="font-bold" style="font-size: 12px; color: #111827;"><?php echo esc_html($item['name']); ?></div>
                        <?php if (!empty($item['sku'])): ?>
                        <div style="font-size: 11px; color: #9ca3af; margin-top: 4px;">SKU: <?php echo esc_html($item['sku']); ?></div>
                        <?php endif; ?>
                        <?php if (!empty($item['meta'])): ?>
                        <div style="font-size: 11px; color: <?php echo esc_attr($accent_color); ?>; margin-top: 6px;">
                            <?php echo esc_html($item['meta']); ?>
                        </div>
                        <?php endif; ?>
                    </td>
                    <td class="text-center">
                        <span class="qty-badge"><?php echo esc_html( $item['quantity'] ); ?></span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php if (!empty($active_order['notes'])): ?>
        <div class="notes-section">
            <span class="label">Customer Notes</span>
            <div class="value" style="margin-top: 8px; font-style: italic; color: #4b5563;">
                <?php echo nl2br(esc_html($active_order['notes'])); ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Footer -->
        <div class="footer">
            <p>This packing slip confirms the contents of your package. If any items are missing or damaged, please contact us.</p>
            <p style="margin-top: 20px; font-weight: bold; color: #6b7280; font-size: 13px;">
                Thank you for choosing <?php echo esc_html($shop_name); ?>!
            </p>
        </div>
    </div>
</body>
</html>