<?php
/**
 * Packing Slip PDF Template - Default
 *
 * Available variables:
 * @var array  $settings        Packing slip settings
 * @var array  $data            Order data (order_number, billing, shipping, items, etc.)
 * @var string $currency_symbol Currency symbol (unused in this template)
 *
 * @package Storeplugin\PDFInvoice
 */

defined('ABSPATH') || exit;

$nxtpdf_items = isset($data['items']) && is_array($data['items']) ? $data['items'] : [];
$nxtpdf_total_items = count($nxtpdf_items);
$nxtpdf_total_quantity = 0;
foreach ($nxtpdf_items as $nxtpdf_item) {
    $nxtpdf_total_quantity += (int) ($nxtpdf_item['quantity'] ?? 0);
}

$nxtpdf_show_image = !empty($settings['show_product_image']);
$nxtpdf_show_sku = !empty($settings['show_sku']);

$nxtpdf_show_shipping = !empty($settings['show_shipping_address']);

$nxtpdf_show_billing = !empty($settings['show_billing_address']);
$nxtpdf_address_columns = 1 + ($nxtpdf_show_shipping ? 1 : 0) + ($nxtpdf_show_billing ? 1 : 0);
$nxtpdf_address_width = (int) floor(100 / max(1, $nxtpdf_address_columns));

$nxtpdf_header_font_size = isset($settings['header_font_size']) ? (int) $settings['header_font_size'] : 10;
if ($nxtpdf_header_font_size < 1 || $nxtpdf_header_font_size > 72) {
    $nxtpdf_header_font_size = 10;
}

$nxtpdf_header_font_size_css = sprintf('%dpt', $nxtpdf_header_font_size);
$nxtpdf_header_title_font_size_css = sprintf('%.2fpt', round($nxtpdf_header_font_size * 2.6, 2));
$nxtpdf_header_company_name_font_size_css = sprintf('%.2fpt', round($nxtpdf_header_font_size * 1.2, 2));
$nxtpdf_header_company_details_font_size_css = sprintf('%.2fpt', round($nxtpdf_header_font_size * 0.95, 2));

$nxtpdf_header_color = sanitize_hex_color((string) ($settings['header_color'] ?? ''));
$nxtpdf_header_zone_color_style = '';
$nxtpdf_header_child_color_style = '';
if (is_string($nxtpdf_header_color) && $nxtpdf_header_color !== '') {
    $nxtpdf_header_zone_color_style = sprintf(' color: %s;', $nxtpdf_header_color);
    $nxtpdf_header_child_color_style = ' color: inherit;';
}

?>

<!-- Header -->
<table class="slip-header nxtpdf-header-zone" style="font-size: <?php echo esc_attr($nxtpdf_header_font_size_css); ?>;<?php echo esc_attr($nxtpdf_header_zone_color_style); ?>">
    <tr>
        <td width="65%" valign="top">
            <h1 class="slip-title" style="font-size: <?php echo esc_attr($nxtpdf_header_title_font_size_css); ?>;<?php echo esc_attr($nxtpdf_header_child_color_style); ?>"><?php echo esc_html($settings['packing_title'] ?? 'Packing Slip'); ?></h1>
            <?php if (!empty($settings['header_text'])): ?>
                <div class="subtitle" style="font-size: <?php echo esc_attr($nxtpdf_header_font_size_css); ?>;<?php echo esc_attr($nxtpdf_header_child_color_style); ?>"><?php echo nl2br(esc_html($settings['header_text'])); ?></div>
            <?php endif; ?>
            <div class="company-block">
                <?php if (!empty($settings['show_company_name']) && !empty($settings['company_name'])): ?>
                    <div class="company-name" style="font-size: <?php echo esc_attr($nxtpdf_header_company_name_font_size_css); ?>;<?php echo esc_attr($nxtpdf_header_child_color_style); ?>"><?php echo esc_html($settings['company_name']); ?></div>
                <?php endif; ?>
                <div class="company-details" style="font-size: <?php echo esc_attr($nxtpdf_header_company_details_font_size_css); ?>;<?php echo esc_attr($nxtpdf_header_child_color_style); ?>">
                    <?php if (!empty($settings['show_company_address'])): ?>
                        <?php if (!empty($settings['company_address_1'])): ?>
                            <?php echo esc_html($settings['company_address_1']); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($settings['company_address_2'])): ?>
                            <?php echo esc_html($settings['company_address_2']); ?><br>
                        <?php endif; ?>
                        <?php
                        $nxtpdf_city_line = array_filter([
                            $settings['company_city'] ?? '',
                            $settings['company_state'] ?? '',
                            $settings['company_postcode'] ?? '',
                        ]);
                        if (!empty($nxtpdf_city_line)): ?>
                            <?php echo esc_html(implode(', ', $nxtpdf_city_line)); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($settings['company_country'])): ?>
                            <?php echo esc_html($settings['company_country']); ?><br>
                        <?php endif; ?>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_company_phone']) && !empty($settings['company_phone'])): ?>
                        <?php echo esc_html($settings['company_phone']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_company_email']) && !empty($settings['company_email'])): ?>
                        <?php echo esc_html($settings['company_email']); ?>
                    <?php endif; ?>
                    <?php if (!empty($settings['additional_header_text'])): ?>
                        <br><?php echo nl2br(esc_html($settings['additional_header_text'])); ?>
                    <?php endif; ?>
                </div>
            </div>
        </td>
        <td width="35%" valign="top" class="logo-wrap">
            <?php if (!empty($settings['show_logo']) && !empty($settings['logo_url'])): ?>
                <img class="nxtpdf-logo-image" src="<?php echo esc_url($settings['logo_url']); ?>" alt="Logo">
            <?php endif; ?>
        </td>
    </tr>
</table>

<!-- Shipment Meta -->
<table class="meta-table">
    <tr>
        <td width="25%">
            <span class="meta-label"><?php esc_html_e('Order Number', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></span>
            <span class="meta-value">#<?php echo esc_html($data['order_number'] ?? ''); ?></span>
        </td>
        <td width="25%">
            <span class="meta-label"><?php esc_html_e('Packing Date', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></span>
            <span class="meta-value"><?php echo esc_html($data['invoice_date'] ?? ($data['order_date'] ?? '')); ?></span>
        </td>
        <td width="25%">
            <span class="meta-label"><?php esc_html_e('Line Items', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></span>
            <span class="meta-value"><?php echo esc_html((string) $nxtpdf_total_items); ?></span>
        </td>
        <td width="25%">
            <span class="meta-label"><?php esc_html_e('Total Quantity', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></span>
            <span class="meta-value"><?php echo esc_html((string) $nxtpdf_total_quantity); ?></span>
        </td>
    </tr>
</table>

<!-- Addresses -->
<table class="addresses-table">
    <tr>
        <?php if ($nxtpdf_show_shipping): ?>
        <td width="<?php echo esc_attr((string) $nxtpdf_address_width . '%'); ?>" valign="top">
            <div class="address-card">
                <div class="address-title"><?php echo esc_html($settings['ship_to_label'] ?? 'Ship To'); ?></div>
                <div class="address-content">
                    <strong><?php echo esc_html(($data['shipping']['first_name'] ?? '') . ' ' . ($data['shipping']['last_name'] ?? '')); ?></strong><br>
                    <?php if (!empty($data['shipping']['company'])): ?>
                        <?php echo esc_html($data['shipping']['company']); ?><br>
                    <?php endif; ?>
                    <?php echo esc_html($data['shipping']['address_1'] ?? ''); ?><br>
                    <?php if (!empty($data['shipping']['address_2'])): ?>
                        <?php echo esc_html($data['shipping']['address_2']); ?><br>
                    <?php endif; ?>
                    <?php
                    $nxtpdf_shipping_city_line = array_filter([
                        $data['shipping']['city'] ?? '',
                        $data['shipping']['state'] ?? '',
                        $data['shipping']['postcode'] ?? '',
                    ]);
                    if (!empty($nxtpdf_shipping_city_line)): ?>
                        <?php echo esc_html(implode(', ', $nxtpdf_shipping_city_line)); ?><br>
                    <?php endif; ?>
                    <?php echo esc_html($data['shipping']['country'] ?? ''); ?>
                </div>
            </div>
        </td>
        <?php endif; ?>

        <?php if ($nxtpdf_show_billing): ?>
        <td width="<?php echo esc_attr((string) $nxtpdf_address_width . '%'); ?>" valign="top">
            <div class="address-card">
                <div class="address-title"><?php echo esc_html($settings['bill_to_label'] ?? 'Bill To'); ?></div>
                <div class="address-content">
                    <strong><?php echo esc_html(($data['billing']['first_name'] ?? '') . ' ' . ($data['billing']['last_name'] ?? '')); ?></strong><br>
                    <?php if (!empty($data['billing']['company'])): ?>
                        <?php echo esc_html($data['billing']['company']); ?><br>
                    <?php endif; ?>
                    <?php echo esc_html($data['billing']['address_1'] ?? ''); ?><br>
                    <?php if (!empty($data['billing']['address_2'])): ?>
                        <?php echo esc_html($data['billing']['address_2']); ?><br>
                    <?php endif; ?>
                    <?php
                    $nxtpdf_billing_city_line = array_filter([
                        $data['billing']['city'] ?? '',
                        $data['billing']['state'] ?? '',
                        $data['billing']['postcode'] ?? '',
                    ]);
                    if (!empty($nxtpdf_billing_city_line)): ?>
                        <?php echo esc_html(implode(', ', $nxtpdf_billing_city_line)); ?><br>
                    <?php endif; ?>
                    <?php echo esc_html($data['billing']['country'] ?? ''); ?>
                    <?php if (!empty($settings['show_billing_email']) && !empty($data['billing']['email'])): ?>
                        <br><?php echo esc_html($data['billing']['email']); ?>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_billing_phone']) && !empty($data['billing']['phone'])): ?>
                        <br><?php echo esc_html($data['billing']['phone']); ?>
                    <?php endif; ?>
                </div>
            </div>
        </td>
        <?php endif; ?>
    </tr>
</table>

<!-- Item Lines -->
<table class="items-table">
    <thead>
        <tr>
            <th width="8%">#</th>
            <?php if ($nxtpdf_show_image): ?>
            <th width="12%"><?php esc_html_e('Image', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></th>
            <?php endif; ?>
            <th><?php echo esc_html($settings['item_label'] ?? 'Item'); ?></th>
            <?php if ($nxtpdf_show_sku): ?>
            <th width="14%"><?php esc_html_e('SKU', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></th>
            <?php endif; ?>
            <th width="14%" class="text-right"><?php echo esc_html($settings['qty_label'] ?? 'Qty'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($nxtpdf_items as $nxtpdf_index => $nxtpdf_item): ?>
        <tr>
            <td><?php echo esc_html((string) ($nxtpdf_index + 1)); ?></td>
            <?php if ($nxtpdf_show_image): ?>
            <td>
                <?php if (!empty($nxtpdf_item['image'])): ?>
                    <img src="<?php echo esc_url($nxtpdf_item['image']); ?>" width="40" height="40" alt="">
                <?php endif; ?>
            </td>
            <?php endif; ?>
            <td>
                <?php echo esc_html($nxtpdf_item['name'] ?? ''); ?>
                <?php if (!empty($settings['show_variations']) && !empty($nxtpdf_item['variations'])): ?>
                    <div class="small-muted">
                        <?php foreach ($nxtpdf_item['variations'] as $nxtpdf_var): ?>
                            <?php echo esc_html($nxtpdf_var['key'] ?? ''); ?>: <?php echo esc_html($nxtpdf_var['value'] ?? ''); ?><br>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
                <?php if (!empty($settings['show_item_meta']) && !empty($nxtpdf_item['meta'])): ?>
                    <div class="small-muted">
                        <?php foreach ($nxtpdf_item['meta'] as $nxtpdf_meta): ?>
                            <?php echo esc_html($nxtpdf_meta['key'] ?? ''); ?>: <?php echo esc_html($nxtpdf_meta['value'] ?? ''); ?><br>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </td>
            <?php if ($nxtpdf_show_sku): ?>
            <td><?php echo esc_html($nxtpdf_item['sku'] ?? ''); ?></td>
            <?php endif; ?>
            <td class="text-right"><?php echo esc_html((string) ($nxtpdf_item['quantity'] ?? 0)); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<table class="summary-table">
    <tr>
        <td class="label"><?php esc_html_e('Total line items', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></td>
        <td class="value"><?php echo esc_html((string) $nxtpdf_total_items); ?></td>
    </tr>
    <tr>
        <td class="label"><?php esc_html_e('Total quantity to pack', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></td>
        <td class="value"><?php echo esc_html((string) $nxtpdf_total_quantity); ?></td>
    </tr>
</table>

<?php if (!empty($settings['show_order_notes']) && !empty($data['notes'])): ?>
<div class="notes-box">
    <div class="notes-title"><?php esc_html_e('Packing Notes', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></div>
    <?php echo esc_html($data['notes']); ?>
</div>
<?php endif; ?>

<?php if (!empty($settings['terms_text']) || !empty($settings['return_policy_text'])): ?>
<div class="terms-section nxtpdf-terms-section">
    <?php if (!empty($settings['terms_text'])): ?>
        <?php if (!empty($settings['terms_title'])): ?>
            <div class="nxtpdf-terms-title">
                <?php echo esc_html($settings['terms_title']); ?>
            </div>
        <?php endif; ?>
        <div class="nxtpdf-terms-block">
            <?php echo nl2br(esc_html($settings['terms_text'])); ?>
        </div>
    <?php endif; ?>

    <?php if (!empty($settings['return_policy_text'])): ?>
        <div>
            <?php if (!empty($settings['return_policy_title'])): ?>
                <strong><?php echo esc_html($settings['return_policy_title']); ?>:</strong><br>
            <?php endif; ?>
            <?php echo nl2br(esc_html($settings['return_policy_text'])); ?>
        </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if (!empty($settings['footer_text']) || !empty($settings['show_page_numbers'])): ?>
<div class="slip-footer nxtpdf-footer-zone">
    <?php if (!empty($settings['footer_text'])): ?>
        <?php echo nl2br(esc_html($settings['footer_text'])); ?>
    <?php endif; ?>
    <?php if (!empty($settings['footer_text']) && !empty($settings['show_page_numbers'])): ?>
        <br>
    <?php endif; ?>
    <?php if (!empty($settings['show_page_numbers'])): ?>
        <?php echo esc_html__('Page {PAGENO} of {nbpg}', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?>
    <?php endif; ?>
</div>
<?php endif; ?>
