<?php
if (!defined('ABSPATH')) {
    exit;
}
/**
 * Printable shipping / packing slip.
 *
 * @var WC_Order $order
 */
if (!isset($order) || !is_a($order, 'WC_Order')) {
    exit;
}

$oid = (int) $order->get_id();
$fields = function_exists('zerti_wc_order_collect_display_customer_fields')
    ? zerti_wc_order_collect_display_customer_fields($order)
    : [];
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?> dir="rtl">
<head>
    <meta charset="<?php bloginfo('charset'); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title><?php
        /* translators: %s: order ID */
        echo esc_html(sprintf(__('Shipping label for order %s', 'zerti'), $oid));
    ?></title>
    <?php wp_head(); ?>
</head>
<body>
    <p class="no-print zerti-print-toolbar">
        <button type="button"><?php esc_html_e('Print / save PDF', 'zerti'); ?></button>
    </p>
    <h1><?php esc_html_e('Shipping label', 'zerti'); ?> — <?php echo esc_html(function_exists('zerti_to_persian_digits') ? zerti_to_persian_digits((string) $oid) : (string) $oid); ?></h1>
    <?php if ($fields !== []) : ?>
        <dl>
            <?php foreach ($fields as $row) : ?>
                <dt><?php echo esc_html((string) $row['label']); ?></dt>
                <dd><?php echo esc_html((string) $row['value']); ?></dd>
            <?php endforeach; ?>
        </dl>
    <?php endif; ?>
    <table>
        <thead>
            <tr>
                <th><?php esc_html_e('Row', 'zerti'); ?></th>
                <th><?php esc_html_e('Product', 'zerti'); ?></th>
                <th><?php esc_html_e('Quantity', 'zerti'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $row_num = 0;
            foreach ($order->get_items('line_item') as $item) :
                if (!is_object($item) || !method_exists($item, 'get_name')) {
                    continue;
                }
                $row_num++;
                $qty = method_exists($item, 'get_quantity') ? (float) $item->get_quantity() : 0;
                ?>
                <tr>
                    <td><?php echo esc_html(function_exists('zerti_to_persian_digits') ? zerti_to_persian_digits((string) $row_num) : (string) $row_num); ?></td>
                    <td><?php echo esc_html((string) $item->get_name()); ?></td>
                    <td><?php echo esc_html(function_exists('zerti_to_persian_digits') ? zerti_to_persian_digits((string) $qty) : (string) $qty); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php wp_footer(); ?>
</body>
</html>
