<?php
/**
 * Template to print barcodes
 *
 * @package YITH\Barcodes\Templates\Print
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$show_images            = get_option( 'tool_print_barcodes_show_image', 'no' );
$show_name              = get_option( 'tool_print_barcodes_show_name', 'yes' );
$show_price             = get_option( 'tool_print_barcodes_show_price', 'no' );
$show_sku               = get_option( 'tool_print_barcodes_show_sku', 'yes' );
$show_short_description = get_option( 'tool_print_barcodes_show_short_description', 'no' );
$number_of_columns      = get_option( 'tool_print_barcodes_number_of_columns', '2' );

if ( $print_all_stock ) {
	$index     = 0;
	$aux_array = array();

	foreach ( $item_ids as $product_id ) {
		$product = wc_get_product( $product_id );

		if ( $product->is_in_stock() ) {
			$quantity = $product->get_stock_quantity();

			if ( empty( $quantity ) || $quantity < 0 ) {
				$quantity = 1;
			}

			$item_ids = array_fill( $index, $quantity, $product_id );

			$aux_array[ $product_id ] = $item_ids;

			$index += $quantity;
		}
	}

	$item_ids = call_user_func_array( 'array_merge', $aux_array );
}

if ( 'browser' === $context ) {
	?>
	<style>
		table {
			border: 0.5px solid grey;
			border-collapse: collapse;
			border-spacing: 25px;
		}
		tr {
			border-bottom: 0.5px solid grey;
		}
		td.main-barcode-container{
			border-right: 0.5px solid grey;
			padding: 20px;
		}
		.ywbc-barcode-display-container{
			text-align: center;
		}
		.product-image{
			padding: 20px;
		}
	</style>
	<?php
}


if ( isset( $item_ids ) && is_array( $item_ids ) ) {
	?>

	<table autosize="1" style="page-break-inside:avoid">

	<?php

	foreach ( array_chunk( $item_ids, $number_of_columns ) as $row ) {
		?>
		<tr class="table-row">
			<?php foreach ( $row as $product_id ) { ?>
				<?php

				$product = wc_get_product( $product_id );

				if ( is_object( $product ) ) {
					$upload_dir = wp_upload_dir();
					$image_path = $product->get_image_id() ? current(
						wp_get_attachment_image_src(
							$product->get_image_id(),
							'thumbnail'
						)
					) : wc_placeholder_img_src( 'thumbnail' );
				} else {
					$image_path = wc_placeholder_img_src( 'thumbnail' );
				}

				apply_filters( 'ywbc_print_barcode_image_path', preg_replace( '/^https(?=:\/\/)/i', 'http', $image_path ) );

				?>

				<?php if ( 'yes' === $show_images ) : ?>
					<?php if ( $image_path ) : ?>
						<td class="image-container" >
							<img class="product-image" src="<?php echo esc_url( $image_path ); ?>" />
						</td>
					<?php endif; ?>
				<?php endif; ?>

				<td class="image-container">
					<?php if ( 'yes' === $show_name ) : ?>
						<div><?php echo wp_kses_post( $product->get_name() ); ?></div>
					<?php endif; ?>

					<?php if ( 'yes' === $show_sku ) : ?>
						<div><?php echo wp_kses_post( $product->get_sku() ); ?></div>
					<?php endif; ?>

					<?php if ( 'yes' === $show_price ) : ?>
						<div><?php echo wp_kses_post( $product->get_price_html() ); ?></div>
					<?php endif; ?>

					<?php if ( 'yes' === $show_short_description ) : ?>
						<div><?php echo wp_kses_post( $product->get_short_description() ); ?></div>
					<?php endif; ?>
				</td>
				<td  class="main-barcode-container">
					<?php YITH_YWBC()->show_barcode( $product_id, '1', apply_filters( 'yith_ywbc_inline_css_barcodes_template', '' ), '', 'pdf' ); ?>
				</td>
			<?php } ?>
		</tr>
		<?php
	}

	?>
	</table>
	<?php
}
