<?php
global $woocommerce;

use WPDesk\PrintOrderLabels\PrintLabels;

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

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

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

$limit_col = (int) $plugin->get_setting_value( 'col_count', PrintLabels::COL_COUNT );
$limit_row = (int) $plugin->get_setting_value( 'row_count', PrintLabels::ROW_COUNT );

$page_orientation = (int) $plugin->get_setting_value( 'page_orientation', 0 );
$page_font_size   = (int) $plugin->get_setting_value( 'page_font_size', PrintLabels::FONT_SIZE );

$page_height = $hpage_width = (int) $plugin->get_setting_value( 'page_height', PrintLabels::PAGE_HEIGHT );
$page_width  = $hpage_height = (int) $plugin->get_setting_value( 'page_width', PrintLabels::PAGE_WIDTH );

if( $page_orientation === 1 ) {
	$page_height = $hpage_height;
	$page_width  = $hpage_width;
}

$row_count = 0;
$col_count = 0;
$last      = end( $params['post_ids'] );

if ( empty( $limit_row ) ) {
	$limit_row = 1;
}

if ( empty( $limit_col ) ) {
	$limit_col = 1;
}

if ( empty( $page_font_size ) ) {
	$page_font_size = 10;
}

$label_text = $plugin->get_setting_value( 'label_text', '<p style="text-align: center;"><strong>%shipping_formatted_address%</strong></p>' );

$label_text = nl2br( $label_text );

$col_width  = $page_width / $limit_col;
$col_height = $page_height / $limit_row;
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title><?php echo __( 'Address Labels', '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"/>
    <style media="print">
        body {
            font-size: <?php echo $page_font_size ?>pt;
            height: 100%;
        }
        .grid {
            width: <?php echo $col_width; ?>mm;
            height: <?php echo $col_height; ?>mm;
            overflow: auto;
            position: absolute;
			<?php if( (int) $plugin->get_setting_value( 'grid_border' ) === 1 ): ?>
			border: 1px solid #EEE;
			<?php endif; ?>

        }
        .inside {
			padding: <?php echo (int) $plugin->get_setting_value( 'row_padding' ); ?>px;
        }

    </style>
</head>
<body>
<?php
foreach ( $params['post_ids'] as $key => $order_id ) {

	if ( ! empty( $order_id ) ) {
		$pos_left = $col_count * $col_width;
		$pos_top  = $row_count * $col_height;

		if ( $limit_row === 1 && $limit_col === 1 ) {
			$pos_left = 0;
			$pos_top  = 0;
		}

		echo '<div class="grid" style="left: ' . $pos_left . 'mm; top: ' . $pos_top . 'mm; "><div class="inside">';
		$label_args = $self->get_labels_tags( $order_id );
		$label      = $label_text;
		foreach ( $label_args as $name => $value ) {
			$label = str_replace( '%' . $name . '%', $value, $label );
		}
		echo $label;
		unset( $label_args );
		echo '</div></div>';
	}

	$col_count ++;
	if ( $limit_col === $col_count ) {
		$col_count = 0;
		$row_count ++;
	}

	if ( $row_count === $limit_row ) {
		$row_count = 0;
		if ( $last !== $order_id ) {
			echo '<pagebreak>';
		}
	}

}
?>
</body>
</html>
