<!DOCTYPE HTML>
<html>
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    	<title><?php echo __( 'Order Details', 'woocommerce-print-orders-address-labels' ) ?></title>
    	<link href="<?php echo $this->get_plugin_url(); ?>/assets/css/reset.css" rel="stylesheet" type="text/css" media="screen,print" />
    	<link href="<?php echo $this->get_plugin_url(); ?>/assets/css/print.css" rel="stylesheet" type="text/css" media="print" />
    	<link href="<?php echo $this->get_plugin_url(); ?>/assets/css/front.css" rel="stylesheet" type="text/css" media="screen,print" />
    </head>
    <body>
                <?php
                    foreach($args['post_ids'] as $key => $order_id):
                        if( ! empty( $order_id ) ):

                    $order = new WC_Order( $order_id );
                    $products = $order->get_items();

                    // Get VAT Number from Flexible Invoices for WooCommerce (or other plugins that save it with the same meta_key)
                    $vat_number = wpdesk_get_order_meta( $order, '_billing_vat_number', true );
                ?>
                <table>
                    <tr>
                        <td colspan="2">
                            <h1><?php echo __( 'Order Details', 'woocommerce-print-orders-address-labels' ) ?></h1>
                            <h2><?php echo __( 'Order:', 'woocommerce-print-orders-address-labels' ) ?> <?php echo $order->get_order_number() ?> - <?php echo wpdesk_get_order_meta( $order, 'order_date', true ); ?></h2>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <h3><?php echo __( 'Shipping Address', 'woocommerce-print-orders-address-labels' ) ?></h3>
                            <?php echo $order->get_formatted_shipping_address(); ?>
                        </td>
                        <td>
                            <h3><?php echo __( 'Billing Details', 'woocommerce-print-orders-address-labels' ) ?></h3>
                            <?php echo $order->get_formatted_billing_address(); ?>							
                            <?php if ( ! empty( $vat_number ) ): ?>
                               <p><?php _e( 'VAT Number:', 'woocommerce-print-orders-address-labels' ); ?> <?php echo $vat_number; ?></p>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <b><?php echo __( 'Phone:', 'woocommerce-print-orders-address-labels' ) ?></b> <?php echo wpdesk_get_order_meta( $order, '_billing_phone', true ); ?><br />
                            <b><?php echo __( 'Email:', 'woocommerce-print-orders-address-labels' ) ?></b> <?php echo wpdesk_get_order_meta( $order, '_billing_email', true ); ?><br />
                            <b><?php echo __( 'Shipping Method:', 'woocommerce-print-orders-address-labels' ) ?></b> <?php echo $order->get_shipping_to_display(); ?><br />
							<b>Metoda płatności:</b> <?php echo $order->get_payment_method_title(); ?><br />
							<b>Faktura:</b> <?php echo $order->get_meta( '_billing_prosze_o_fakture'); ?><br />
							
                            <?php echo wpdesk_get_order_meta( $order, '_customer_note', true); ?>
                        </td>
                    </tr>
                </table>
                <?php //print_r($products) ?>
    			<table class="product">
    			    <thead>
    			        <tr>
    			            <td>Miniaturka</td>
    			            <td><?php echo __( 'Product', 'woocommerce-print-orders-address-labels' ) ?></td>
    			            <td><?php echo __( 'SKU', 'woocommerce-print-orders-address-labels' ) ?></td>
    			            <td><?php echo __( 'Quantity', 'woocommerce-print-orders-address-labels' ) ?></td>
    			            <td><?php echo __( 'Price', 'woocommerce-print-orders-address-labels' ) ?></td>
    			        </tr>
    			    </thead>
    			    <?php $i = 0; ?>
    				<?php foreach($products as $pkey => $item): ?>
    				    <?php $product = $order->get_product_from_item($item); ?>
                        <?php $attributes = $product->get_attributes(); ?>
    				    <tr<?php if($i % 2 == 0): ?> class="color"<?php endif; ?>>
    				        <td><?php 
							$sprawdzam = $product->get_id();
							if (empty($sprawdzam))  { echo $product->get_image(array( 72, 72)) } ?></td>
    				        <td>
        				        <?php echo $item['name'] ?>

        				        <?php
                                global $wpdb;

                				if ( $metadata = wpdesk_get_order_item_meta_data( $order, $pkey, true ) ) {
                    				echo '<div style="font-size: 0.7em">';
                					foreach ( $metadata as $meta ) {

                						// Skip hidden core fields
                						if ( in_array( $meta['meta_key'], apply_filters( 'woocommerce_hidden_order_itemmeta', array(
                							'_qty',
                							'_tax_class',
                							'_product_id',
                							'_variation_id',
                							'_line_subtotal',
                							'_line_subtotal_tax',
                							'_line_total',
                							'_line_tax',
                						) ) ) ) {
                							continue;
                						}

                						// Skip serialised meta
                						if ( is_serialized( $meta['meta_value'] ) ) {
                							continue;
                						}

                						// Get attribute data
                						if ( taxonomy_exists( $meta['meta_key'] ) ) {
                							$term           = get_term_by( 'slug', $meta['meta_value'], $meta['meta_key'] );
                							$attribute_name = str_replace( 'pa_', '', wc_clean( $meta['meta_key'] ) );
                							$attribute      = $wpdb->get_var(
                								$wpdb->prepare( "
                										SELECT attribute_label
                										FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
                										WHERE attribute_name = %s;
                									",
                									$attribute_name
                								)
                							);

                							$meta['meta_key']   = ( ! is_wp_error( $attribute ) && $attribute ) ? $attribute : $attribute_name;
                							$meta['meta_value'] = ( isset( $term->name ) ) ? $term->name : $meta['meta_value'];
                						}

                						echo '' .  urldecode( $meta['meta_key'] )  . ': <b>' .   urldecode( $meta['meta_value'] )   . '</b><br />';
                					}
                					echo '</div>';
                				}
                				?>
        				    </td>
    				        <td><?php echo $product->get_sku() ?></td>
    				        <td><?php echo $item['qty'] ?></td>
                            <td><?php echo wc_price( $order->get_item_total( $item, true ) ); ?></td>
    				    </tr>
                        <?php $i++ ?>
    				<?php endforeach; ?>
<tr<?php if($i % 2 == 0): ?><?php endif; ?>><td>Suma zamówienia wraz z przesyłką</td><td></td><td></td><td><?php echo $order->get_item_count(); ?></td><td><?php echo $order->get_total(); ?></td></tr>
</table>
<?php endif; ?>
    			<?php if($key < (count($args['post_ids'])-1)): ?>
        			<?php
	                    if ( $this->get_setting_value('page_print') !== '1' ): ?>
        			    <pagebreak />
        			<?php endif; ?>
    			<?php endif; ?>
    			<?php endforeach; ?>
    </body>
</html>