<?php
defined( 'ABSPATH' ) || exit;
$edpw_border_style = ! empty( $border ) ? 'border:' . $border . ';' : '';
$edpw_padding      = ! empty( $padding ) ? 'padding:' . $padding . ';' : '';
$edpw_color        = ! empty( $color ) ? 'color:' . $color . ';' : '';
?>
<div class="email-product-list"
     style="padding: 15px 25px;<?php echo esc_attr( ! empty( $background_color ) ? 'background-color:' . $background_color . ';' : '' ); ?>
     <?php echo esc_attr( $edpw_color ); ?>
     <?php echo ! empty( $font_family ) ? esc_attr( 'font-family:' . $font_family . ';' ) : ''; ?>
     <?php echo ! empty( $font_size ) ? esc_attr( 'font-size:' . $font_size . ';' ) : ''; ?>
     <?php echo ! empty( $text_align ) ? esc_attr( 'text-align:' . $text_align . ';' ) : ''; ?>
     <?php echo ! empty( $line_height ) ? esc_attr( 'line-height:' . $line_height . ';' ) : ''; ?>
     <?php echo ! empty( $font_weight ) ? esc_attr( 'font-weight:' . $font_weight . ';' ) : ''; ?>">
	<?php if ( $show_payment_instruction == 'yes' || ( $show_payment_instruction == 'only_customer' && ! $sent_to_admin ) ) {
		if ( is_object( $order ) && ! empty( $email ) && is_object( $email ) ) {
			//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
			do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email );
		}
	} ?>
    <table class="email_builder_table_items" cellspacing="0" cellpadding="6"
           style="width: 100% !important; <?php echo esc_attr( $edpw_border_style ); ?>"
           border="1"
           dir="<?php echo esc_attr( $direction ); ?>"
           width="100%">
        <thead>
        <tr style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
            <th class="td" scope="col"
                style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>"><?php esc_html_e( 'Product', 'email-editor-plus' ); ?></th>
            <th class="td" scope="col"
                style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>"><?php esc_html_e( 'Quantity', 'email-editor-plus' ); ?></th>
            <th class="td" scope="col"
                style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>"><?php esc_html_e( 'Price', 'email-editor-plus' ); ?></th>
        </tr>
        </thead>
        <tbody>
		<?php
		foreach ( $items as $edpw_item_id => $edpw_item ) {
			//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
			if ( ! apply_filters( 'woocommerce_order_item_visible', true, $edpw_item ) ) {
				continue;
			}
			$edpw_product = $edpw_item->get_product();
			//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
			$edpw_order_item_class = apply_filters( 'woocommerce_order_item_class', 'order_item', $edpw_item, $order );
			?>
            <tr class="<?php echo esc_attr( $edpw_order_item_class ); ?>"
                style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
                <td class="td"
                    style="text-align:<?php echo esc_attr( $text_align ); ?>;vertical-align:middle;word-wrap:break-word;<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">

					<?php
					// Show title/image etc
					if ( $show_image && is_object( $edpw_product ) ) {
						$edpw_image_id = $edpw_product->get_image_id();

						if ( $edpw_image_id ) {
							$edpw_image_html = wp_get_attachment_image(
								$edpw_image_id,
								array( $product_image_width, $product_image_height ),
								false,
								array(
									'alt'   => __( 'Product image', 'email-editor-plus' ),
									'style' => 'vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;',
								)
							);
						} else {
							$edpw_image_html = wc_placeholder_img( array(
								'alt'    => __( 'Product image', 'email-editor-plus' ),
								'style'  => 'vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;',
								'width'  => $product_image_width,
								'height' => $product_image_height,
							) );
						}
						//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
						echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $edpw_image_html, $edpw_item ) );
					}

					// Product name
					//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
					echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $edpw_item->get_name(), $edpw_item, false ) );

					// SKU
					if ( $show_sku && is_object( $edpw_product ) && ( $edpw_sku = $edpw_product->get_sku() ) ) {
						echo wp_kses_post( ' (#' . $edpw_sku . ')' );
					}

					// allow other plugins to add additional product information here
					//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
					do_action( 'woocommerce_order_item_meta_start', $edpw_item_id, $edpw_item, $order, $plain_text );
					wc_display_item_meta( $edpw_item, [ 'label_before' => '<strong class="wc-item-meta-label" style="float: ' . esc_attr( $text_align ) . '; margin-' . esc_attr( $margin_side ) . ': .25em; clear: both">' ] );
					if ( $show_download_links ) {
						wc_display_item_downloads( $edpw_item );
					}
					// allow other plugins to add additional product information here
					//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
					do_action( 'woocommerce_order_item_meta_end', $edpw_item_id, $edpw_item, $order, $plain_text );
					?>
                </td>
                <td class="td"
                    style="text-align:<?php echo esc_attr( $text_align ); ?>;vertical-align:middle;<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
					<?php
					$edpw_qty          = $edpw_item->get_quantity();
					$edpw_refunded_qty = $order->get_qty_refunded_for_item( $edpw_item_id );

					if ( $edpw_refunded_qty ) {
						$edpw_qty_display = '<del>' . esc_html( $edpw_qty ) . '</del> <ins>' . esc_html( $edpw_qty - ( $edpw_refunded_qty * - 1 ) ) . '</ins>';
					} else {
						$edpw_qty_display = esc_html( $edpw_qty );
					}
					//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
					echo wp_kses_post( apply_filters( 'woocommerce_email_order_item_quantity', $edpw_qty_display, $edpw_item ) ); ?>
                </td>
                <td class="td"
                    style="text-align:<?php echo esc_attr( $text_align ); ?>;vertical-align:middle;<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
					<?php echo wp_kses_post( $order->get_formatted_line_subtotal( $edpw_item ) ); ?>
                </td>
            </tr>
			<?php
			if ( $show_purchase_note && is_object( $edpw_product ) && ( $edpw_purchase_note = $edpw_product->get_purchase_note() ) ) { ?>
                <tr style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
                    <td colspan="3"
                        style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle;<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
						<?php echo wp_kses_post( wpautop( do_shortcode( $edpw_purchase_note ) ) ); ?>
                    </td>
                </tr>
			<?php }
		}
		?>
        </tbody>
        <tfoot>
		<?php
		if ( $totals ) {
			$edpw_i = 0;
			foreach ( $totals as $edpw_total ) {
				$edpw_i ++;
				?>
            <tr style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?>">
                <th class="td" scope="row" colspan="2"
                    style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?><?php if ( $edpw_i === 1 ) {
					    echo 'border-top-width: 1px;';
				    } ?>"><?php echo wp_kses_post( $edpw_total['label'] ); ?></th>
                <td class="td"
                    style="<?php echo esc_attr( $edpw_border_style ); ?><?php echo esc_attr( $edpw_padding ); ?><?php echo esc_attr( $edpw_color ); ?><?php if ( $edpw_i === 1 ) {
					    echo 'border-top-width: 1px;';
				    } ?>"><?php echo wp_kses_post( $edpw_total['value'] ); ?></td>
                </tr><?php
			}
		}
		?>
        </tfoot>
    </table>
	<?php
	//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
	do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>
</div>
