<?php
global $woocommerce;

use PrintOrdersVendor\WPDesk\Library\WPDeskOrder\Abstracts\OrderItem;
use PrintOrdersVendor\WPDesk\Library\WPDeskOrder\Abstracts\ProductOrderItem;
use PrintOrdersVendor\WPDesk\Library\WPDeskOrder\Abstracts\Totals;

$params = isset( $params ) ? $params : [];

/**
 * @var $plugin \WPDesk\PrintOrderLabels\Plugin
 */
$plugin = isset( $params['plugin'] ) ? $params['plugin'] : '';

/**
 * @var $self \WPDesk\PrintOrderLabels\PrintLabels
 */
$self = isset( $params['self'] ) ? $params['self'] : '';

/**
 * @var array
 */
$orders = isset( $params['order_data'] ) ? $params['order_data'] : '';

$hidden_order_itemmeta = apply_filters(
		'woocommerce_hidden_order_itemmeta',
		array(
				'_qty',
				'_tax_class',
				'_product_id',
				'_variation_id',
				'_line_subtotal',
				'_line_subtotal_tax',
				'_line_total',
				'_line_tax',
				'method_id',
				'cost',
				'_reduced_stock',
				'flexible_coupon_recipient_name',
				'flexible_coupon_recipient_email',
				'flexible_coupon_recipient_message',
		)
);
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title><?php echo esc_html__( 'Order Details', 'woocommerce-print-orders-address-labels' ) ?></title>
	<link href="<?php echo $plugin->get_plugin_url(); ?>assets/css/reset.css" rel="stylesheet" type="text/css" media="screen,print"/>
	<link href="<?php echo $plugin->get_plugin_url(); ?>assets/css/print.css" rel="stylesheet" type="text/css" media="screen,print"/>
	<link href="<?php echo $plugin->get_plugin_url(); ?>assets/css/front.css" rel="stylesheet" type="text/css" media="screen,print"/>
</head>
<body class="packing-list">
<?php
foreach ($orders as $key => $order ):
		/**
		 * @var $totals Totals
		 */
		$totals = isset( $order['totals'] ) ? $order['totals'] : '';
		?>
		<table>
			<tr>
				<td colspan="2">
					<h1 class="header1"><?php echo esc_html__( 'Order Details', 'woocommerce-print-orders-address-labels' ) ?></h1>
					<h2><?php echo esc_html__( 'Order:', 'woocommerce-print-orders-address-labels' ) ?> <?php echo $order['number'] ?>
						- <?php echo $order['date']; ?></h2>
				</td>
			</tr>
			<tr>
				<td>
					<h3><?php echo esc_html__( 'Shipping Address', 'woocommerce-print-orders-address-labels' ) ?></h3>
					<?php echo $order['shipping_address']; ?>
				</td>
				<td>
					<h3><?php echo esc_html__( 'Billing Details', 'woocommerce-print-orders-address-labels' ) ?></h3>
					<?php echo $order['billing_address']; ?>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<strong><?php echo esc_html__( 'Phone:', 'woocommerce-print-orders-address-labels' ) ?></strong> <?php echo $order['customer_phone']; ?>
					<br/>
					<strong><?php echo esc_html__( 'Email:', 'woocommerce-print-orders-address-labels' ) ?></strong> <?php echo $order['customer_email']; ?>
					<br/>
				</td>
			</tr>
		</table>
		<table class="product">
			<thead>
			<tr>
        <td>Miniaturka</td>
				<td><?php echo esc_html__( 'Product', 'woocommerce-print-orders-address-labels' ) ?></td>
				<td><?php echo esc_html__( 'SKU', 'woocommerce-print-orders-address-labels' ) ?></td>
				<td><?php echo esc_html__( 'Quantity', 'woocommerce-print-orders-address-labels' ) ?></td>
				<td><?php echo esc_html__( 'Net price', 'woocommerce-print-orders-address-labels' ) ?></td>
				<td><?php echo esc_html__( 'Tax', 'woocommerce-print-orders-address-labels' ) ?></td>
				<td><?php echo esc_html__( 'Gross Price', 'woocommerce-print-orders-address-labels' ) ?></td>
			</tr>
			</thead>
			<tbody>
			<?php
			$i = 0;
			?>
			<?php foreach ( $order['items'] as $pkey => $item ): ?>
				<?php
				/**
				 * @var $item OrderItem
				 */
				$attributes = [];
				$sku = '';
				if ( $item instanceof ProductOrderItem ) {
					$product = wc_get_product( $item->get_product_id() );
					if ( $product ) {
						$attributes = $product->get_attributes();
					}
					$sku = $item->get_sku();
				}
				?>
				<?php ?>
				<tr<?php if ( $i % 2 == 0 ): ?> class="color"<?php endif; ?>>
    				<td>
						<?php if ( 'shipping' !== $item->get_type() ): ?>
							<?php echo $product->get_image(array( 72, 72)) ?>
						<?php endif; ?>
					</td>
					<td>
						<?php if ( 'shipping' === $item->get_type() ): ?>
							<?php echo esc_html__( 'Shipping:', 'woocommerce-print-orders-address-labels' ) ?>
						<?php endif; ?>
						<?php echo $item->get_name(); ?>
						<?php
						if ( ! empty( $item->get_meta_data() ) ) {
							echo '<div style="font-size: 0.7em">';
							foreach ( $item->get_meta_data() as $meta_key => $meta ) {
								if ( in_array( $meta->display_key, $hidden_order_itemmeta ) ) {
									continue;
								}
								if ( is_object( $meta ) ) {
									echo esc_html( $meta->display_key ) . ': <strong>' . esc_html( $meta->value ) . '</strong><br />';
								}

							}
							echo '</div>';
						}
						?>
					</td>
					<td><?php echo $sku; ?></td>
					<td><?php echo $item->get_qty() ?></td>
					<td><?php echo wc_price( $item->get_net_price_r(), ['currency' => $item->get_currency_slug() ] ); ?></td>
					<td><?php echo wc_price( $item->get_vat_price_r(), ['currency' => $item->get_currency_slug() ] ); ?></td>
					<td><?php echo wc_price( $item->get_gross_price_r(), ['currency' => $item->get_currency_slug() ] ); ?></td>
				</tr>
				<?php $i ++ ?>
			<?php endforeach; ?>

			</tbody>
			<tfoot>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td><?php echo wc_price( $totals->get_net_price_r(), ['currency' => $totals->get_currency_slug() ] ); ?></td>
				<td><?php echo wc_price( $totals->get_vat_price_r(), ['currency' => $totals->get_currency_slug() ] ); ?></td>
				<td><?php echo wc_price( $totals->get_gross_price_r(), ['currency' => $totals->get_currency_slug() ] ); ?></td>
			</tr>
			</tfoot>
		</table>
		<table class="additional-data">
			<tbody>
			<?php if ( $order['customer_note'] ): ?>
				<tr>
					<td width="200"><?php echo esc_html__( 'Customer notes:', 'woocommerce-print-orders-address-labels' ) ?></td>
					<td><?php echo $order['customer_note']; ?></td>
				</tr>
			<?php endif; ?>

			<?php
			if ( ! empty( $order['meta_fields'] ) ) {
				foreach ( $order['meta_fields'] as $field_key => $field ) {
				?>
					<tr>
						<td width="200"><strong><?php echo esc_html( $field['name'] ); ?></strong></td>
						<td><?php echo esc_html( $field['value'] ); ?></td>
					</tr>
				<?php
				}
			}
			?>
			</tbody>
		</table>

	<?php if ( $key < ( count( $orders ) - 1 ) && $plugin->get_setting_value( 'page_print' ) !== '1' ): ?>
		<pagebreak/>
	<?php endif; ?>

<?php endforeach; ?>
</body>
</html>
